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