client_linux.go 3.3 KB

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