client_daemon_linux.go 2.6 KB

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