client_linux.go 3.3 KB

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