client_daemon_linux.go 2.7 KB

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