client_linux.go 3.6 KB

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