|
| 1 | +// +build !windows |
| 2 | + |
| 3 | +package daemon |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/containerd/containerd/runtime/linux/runctypes" |
| 14 | + v2runcoptions "github.com/containerd/containerd/runtime/v2/runc/options" |
| 15 | + "github.com/docker/docker/api/types" |
| 16 | + "github.com/docker/docker/daemon/config" |
| 17 | + "github.com/docker/docker/pkg/ioutils" |
| 18 | + "github.com/opencontainers/runc/libcontainer/cgroups" |
| 19 | + "github.com/pkg/errors" |
| 20 | + "github.com/sirupsen/logrus" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + defaultRuntimeName = "runc" |
| 25 | + |
| 26 | + linuxShimV1 = "io.containerd.runtime.v1.linux" |
| 27 | + linuxShimV2 = "io.containerd.runc.v2" |
| 28 | +) |
| 29 | + |
| 30 | +func configureRuntimes(conf *config.Config) { |
| 31 | + if conf.DefaultRuntime == "" { |
| 32 | + conf.DefaultRuntime = config.StockRuntimeName |
| 33 | + } |
| 34 | + if conf.Runtimes == nil { |
| 35 | + conf.Runtimes = make(map[string]types.Runtime) |
| 36 | + } |
| 37 | + conf.Runtimes[config.StockRuntimeName] = types.Runtime{Path: defaultRuntimeName, Shim: getShimConfig(conf, defaultRuntimeName)} |
| 38 | + conf.Runtimes[config.LinuxV1RuntimeName] = types.Runtime{Path: defaultRuntimeName, Shim: defaultV1ShimConfig(conf, defaultRuntimeName)} |
| 39 | + conf.Runtimes[config.LinuxV2RuntimeName] = types.Runtime{Path: defaultRuntimeName, Shim: defaultV2ShimConfig(conf, defaultRuntimeName)} |
| 40 | +} |
| 41 | + |
| 42 | +func getShimConfig(conf *config.Config, runtimePath string) *types.ShimConfig { |
| 43 | + if cgroups.IsCgroup2UnifiedMode() { |
| 44 | + return defaultV2ShimConfig(conf, runtimePath) |
| 45 | + } |
| 46 | + return defaultV1ShimConfig(conf, runtimePath) |
| 47 | +} |
| 48 | + |
| 49 | +func defaultV2ShimConfig(conf *config.Config, runtimePath string) *types.ShimConfig { |
| 50 | + return &types.ShimConfig{ |
| 51 | + Binary: linuxShimV2, |
| 52 | + Opts: &v2runcoptions.Options{ |
| 53 | + BinaryName: runtimePath, |
| 54 | + Root: filepath.Join(conf.ExecRoot, "runtime-"+defaultRuntimeName), |
| 55 | + SystemdCgroup: UsingSystemd(conf), |
| 56 | + NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "", |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func defaultV1ShimConfig(conf *config.Config, runtimePath string) *types.ShimConfig { |
| 62 | + return &types.ShimConfig{ |
| 63 | + Binary: linuxShimV1, |
| 64 | + Opts: &runctypes.RuncOptions{ |
| 65 | + Runtime: runtimePath, |
| 66 | + RuntimeRoot: filepath.Join(conf.ExecRoot, "runtime-"+defaultRuntimeName), |
| 67 | + SystemdCgroup: UsingSystemd(conf), |
| 68 | + }, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func (daemon *Daemon) loadRuntimes() error { |
| 73 | + return daemon.initRuntimes(daemon.configStore.Runtimes) |
| 74 | +} |
| 75 | + |
| 76 | +func (daemon *Daemon) initRuntimes(runtimes map[string]types.Runtime) (err error) { |
| 77 | + runtimeDir := filepath.Join(daemon.configStore.Root, "runtimes") |
| 78 | + // Remove old temp directory if any |
| 79 | + os.RemoveAll(runtimeDir + "-old") |
| 80 | + tmpDir, err := ioutils.TempDir(daemon.configStore.Root, "gen-runtimes") |
| 81 | + if err != nil { |
| 82 | + return errors.Wrap(err, "failed to get temp dir to generate runtime scripts") |
| 83 | + } |
| 84 | + defer func() { |
| 85 | + if err != nil { |
| 86 | + if err1 := os.RemoveAll(tmpDir); err1 != nil { |
| 87 | + logrus.WithError(err1).WithField("dir", tmpDir). |
| 88 | + Warn("failed to remove tmp dir") |
| 89 | + } |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if err = os.Rename(runtimeDir, runtimeDir+"-old"); err != nil { |
| 94 | + return |
| 95 | + } |
| 96 | + if err = os.Rename(tmpDir, runtimeDir); err != nil { |
| 97 | + err = errors.Wrap(err, "failed to setup runtimes dir, new containers may not start") |
| 98 | + return |
| 99 | + } |
| 100 | + if err = os.RemoveAll(runtimeDir + "-old"); err != nil { |
| 101 | + logrus.WithError(err).WithField("dir", tmpDir). |
| 102 | + Warn("failed to remove old runtimes dir") |
| 103 | + } |
| 104 | + }() |
| 105 | + |
| 106 | + for name, rt := range runtimes { |
| 107 | + if len(rt.Args) == 0 { |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + script := filepath.Join(tmpDir, name) |
| 112 | + content := fmt.Sprintf("#!/bin/sh\n%s %s $@\n", rt.Path, strings.Join(rt.Args, " ")) |
| 113 | + if err := ioutil.WriteFile(script, []byte(content), 0700); err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +// rewriteRuntimePath is used for runtimes which have custom arguments supplied. |
| 121 | +// This is needed because the containerd API only calls the OCI runtime binary, there is no options for extra arguments. |
| 122 | +// To support this case, the daemon wraps the specified runtime in a script that passes through those arguments. |
| 123 | +func (daemon *Daemon) rewriteRuntimePath(name, p string, args []string) (string, error) { |
| 124 | + if len(args) == 0 { |
| 125 | + return p, nil |
| 126 | + } |
| 127 | + |
| 128 | + // Check that the runtime path actually exists here so that we can return a well known error. |
| 129 | + if _, err := exec.LookPath(p); err != nil { |
| 130 | + return "", errors.Wrap(err, "error while looking up the specified runtime path") |
| 131 | + } |
| 132 | + |
| 133 | + return filepath.Join(daemon.configStore.Root, "runtimes", name), nil |
| 134 | +} |
0 commit comments