client_linux.go 3.4 KB

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