github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/libcontainerd/remote/client_linux.go (about)

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