client_linux.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package remote // import "github.com/docker/docker/libcontainerd/remote"
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/containerd/containerd"
  9. "github.com/containerd/containerd/cio"
  10. "github.com/containerd/containerd/containers"
  11. "github.com/containerd/containerd/log"
  12. libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
  13. "github.com/docker/docker/pkg/idtools"
  14. specs "github.com/opencontainers/runtime-spec/specs-go"
  15. "github.com/sirupsen/logrus"
  16. )
  17. func summaryFromInterface(i interface{}) (*libcontainerdtypes.Summary, error) {
  18. return &libcontainerdtypes.Summary{}, nil
  19. }
  20. func (t *task) UpdateResources(ctx context.Context, resources *libcontainerdtypes.Resources) error {
  21. return t.Update(ctx, containerd.WithResources(resources))
  22. }
  23. func hostIDFromMap(id uint32, mp []specs.LinuxIDMapping) int {
  24. for _, m := range mp {
  25. if id >= m.ContainerID && id <= m.ContainerID+m.Size-1 {
  26. return int(m.HostID + id - m.ContainerID)
  27. }
  28. }
  29. return 0
  30. }
  31. func getSpecUser(ociSpec *specs.Spec) (int, int) {
  32. var (
  33. uid int
  34. gid int
  35. )
  36. for _, ns := range ociSpec.Linux.Namespaces {
  37. if ns.Type == specs.UserNamespace {
  38. uid = hostIDFromMap(0, ociSpec.Linux.UIDMappings)
  39. gid = hostIDFromMap(0, ociSpec.Linux.GIDMappings)
  40. break
  41. }
  42. }
  43. return uid, gid
  44. }
  45. // WithBundle creates the bundle for the container
  46. func WithBundle(bundleDir string, ociSpec *specs.Spec) containerd.NewContainerOpts {
  47. return func(ctx context.Context, client *containerd.Client, c *containers.Container) error {
  48. if c.Labels == nil {
  49. c.Labels = make(map[string]string)
  50. }
  51. uid, gid := getSpecUser(ociSpec)
  52. if uid == 0 && gid == 0 {
  53. c.Labels[DockerContainerBundlePath] = bundleDir
  54. return idtools.MkdirAllAndChownNew(bundleDir, 0o755, idtools.Identity{UID: 0, GID: 0})
  55. }
  56. p := string(filepath.Separator)
  57. components := strings.Split(bundleDir, string(filepath.Separator))
  58. for _, d := range components[1:] {
  59. p = filepath.Join(p, d)
  60. fi, err := os.Stat(p)
  61. if err != nil && !os.IsNotExist(err) {
  62. return err
  63. }
  64. if os.IsNotExist(err) || fi.Mode()&1 == 0 {
  65. p = fmt.Sprintf("%s.%d.%d", p, uid, gid)
  66. if err := idtools.MkdirAndChown(p, 0o700, idtools.Identity{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
  67. return err
  68. }
  69. }
  70. }
  71. if c.Labels == nil {
  72. c.Labels = make(map[string]string)
  73. }
  74. c.Labels[DockerContainerBundlePath] = p
  75. return nil
  76. }
  77. }
  78. func withLogLevel(_ logrus.Level) containerd.NewTaskOpts {
  79. panic("Not implemented")
  80. }
  81. func newFIFOSet(bundleDir, processID string, withStdin, withTerminal bool) *cio.FIFOSet {
  82. config := cio.Config{
  83. Terminal: withTerminal,
  84. Stdout: filepath.Join(bundleDir, processID+"-stdout"),
  85. }
  86. paths := []string{config.Stdout}
  87. if withStdin {
  88. config.Stdin = filepath.Join(bundleDir, processID+"-stdin")
  89. paths = append(paths, config.Stdin)
  90. }
  91. if !withTerminal {
  92. config.Stderr = filepath.Join(bundleDir, processID+"-stderr")
  93. paths = append(paths, config.Stderr)
  94. }
  95. closer := func() error {
  96. for _, path := range paths {
  97. if err := os.RemoveAll(path); err != nil {
  98. log.G(context.TODO()).Warnf("libcontainerd: failed to remove fifo %v: %v", path, err)
  99. }
  100. }
  101. return nil
  102. }
  103. return cio.NewFIFOSet(config, closer)
  104. }
  105. func (c *client) newDirectIO(ctx context.Context, fifos *cio.FIFOSet) (*cio.DirectIO, error) {
  106. return cio.NewDirectIO(ctx, fifos)
  107. }