github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libcontainerd/remote/client_linux.go (about) 1 package remote // import "github.com/Prakhar-Agarwal-byte/moby/libcontainerd/remote" 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/containerd/containerd" 11 "github.com/containerd/containerd/cio" 12 "github.com/containerd/containerd/containers" 13 "github.com/containerd/log" 14 libcontainerdtypes "github.com/Prakhar-Agarwal-byte/moby/libcontainerd/types" 15 "github.com/Prakhar-Agarwal-byte/moby/pkg/idtools" 16 specs "github.com/opencontainers/runtime-spec/specs-go" 17 ) 18 19 func summaryFromInterface(i interface{}) (*libcontainerdtypes.Summary, error) { 20 return &libcontainerdtypes.Summary{}, nil 21 } 22 23 func (t *task) UpdateResources(ctx context.Context, resources *libcontainerdtypes.Resources) error { 24 return t.Update(ctx, containerd.WithResources(resources)) 25 } 26 27 func hostIDFromMap(id uint32, mp []specs.LinuxIDMapping) int { 28 for _, m := range mp { 29 if id >= m.ContainerID && id <= m.ContainerID+m.Size-1 { 30 return int(m.HostID + id - m.ContainerID) 31 } 32 } 33 return 0 34 } 35 36 func getSpecUser(ociSpec *specs.Spec) (int, int) { 37 var ( 38 uid int 39 gid int 40 ) 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 50 return uid, gid 51 } 52 53 // WithBundle creates the bundle for the container 54 func WithBundle(bundleDir string, ociSpec *specs.Spec) containerd.NewContainerOpts { 55 return func(ctx context.Context, client *containerd.Client, c *containers.Container) error { 56 if c.Labels == nil { 57 c.Labels = make(map[string]string) 58 } 59 uid, gid := getSpecUser(ociSpec) 60 if uid == 0 && gid == 0 { 61 c.Labels[DockerContainerBundlePath] = bundleDir 62 return idtools.MkdirAllAndChownNew(bundleDir, 0o755, idtools.Identity{UID: 0, GID: 0}) 63 } 64 65 p := string(filepath.Separator) 66 components := strings.Split(bundleDir, string(filepath.Separator)) 67 for _, d := range components[1:] { 68 p = filepath.Join(p, d) 69 fi, err := os.Stat(p) 70 if err != nil && !os.IsNotExist(err) { 71 return err 72 } 73 if os.IsNotExist(err) || fi.Mode()&1 == 0 { 74 p = fmt.Sprintf("%s.%d.%d", p, uid, gid) 75 if err := idtools.MkdirAndChown(p, 0o700, idtools.Identity{UID: uid, GID: gid}); err != nil && !os.IsExist(err) { 76 return err 77 } 78 } 79 } 80 if c.Labels == nil { 81 c.Labels = make(map[string]string) 82 } 83 c.Labels[DockerContainerBundlePath] = p 84 return nil 85 } 86 } 87 88 func withLogLevel(_ log.Level) containerd.NewTaskOpts { 89 panic("Not implemented") 90 } 91 92 func newFIFOSet(bundleDir, processID string, withStdin, withTerminal bool) *cio.FIFOSet { 93 config := cio.Config{ 94 Terminal: withTerminal, 95 Stdout: filepath.Join(bundleDir, processID+"-stdout"), 96 } 97 paths := []string{config.Stdout} 98 99 if withStdin { 100 config.Stdin = filepath.Join(bundleDir, processID+"-stdin") 101 paths = append(paths, config.Stdin) 102 } 103 if !withTerminal { 104 config.Stderr = filepath.Join(bundleDir, processID+"-stderr") 105 paths = append(paths, config.Stderr) 106 } 107 closer := func() error { 108 for _, path := range paths { 109 if err := os.RemoveAll(path); err != nil { 110 log.G(context.TODO()).Warnf("libcontainerd: failed to remove fifo %v: %v", path, err) 111 } 112 } 113 return nil 114 } 115 116 return cio.NewFIFOSet(config, closer) 117 } 118 119 func (c *client) newDirectIO(ctx context.Context, fifos *cio.FIFOSet) (*cio.DirectIO, error) { 120 return cio.NewDirectIO(ctx, fifos) 121 }