client_linux.go 3.5 KB

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