runtime_unix.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //go:build !windows
  2. package daemon
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. v2runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
  12. "github.com/docker/docker/api/types"
  13. "github.com/docker/docker/daemon/config"
  14. "github.com/docker/docker/errdefs"
  15. "github.com/docker/docker/libcontainerd/shimopts"
  16. "github.com/opencontainers/runtime-spec/specs-go/features"
  17. "github.com/pkg/errors"
  18. "github.com/sirupsen/logrus"
  19. )
  20. const (
  21. defaultRuntimeName = "runc"
  22. linuxShimV2 = "io.containerd.runc.v2"
  23. )
  24. func configureRuntimes(conf *config.Config) {
  25. if conf.DefaultRuntime == "" {
  26. conf.DefaultRuntime = config.StockRuntimeName
  27. }
  28. if conf.Runtimes == nil {
  29. conf.Runtimes = make(map[string]types.Runtime)
  30. }
  31. conf.Runtimes[config.LinuxV2RuntimeName] = types.Runtime{Path: defaultRuntimeName, ShimConfig: defaultV2ShimConfig(conf, defaultRuntimeName)}
  32. conf.Runtimes[config.StockRuntimeName] = conf.Runtimes[config.LinuxV2RuntimeName]
  33. }
  34. func defaultV2ShimConfig(conf *config.Config, runtimePath string) *types.ShimConfig {
  35. return &types.ShimConfig{
  36. Binary: linuxShimV2,
  37. Opts: &v2runcoptions.Options{
  38. BinaryName: runtimePath,
  39. Root: filepath.Join(conf.ExecRoot, "runtime-"+defaultRuntimeName),
  40. SystemdCgroup: UsingSystemd(conf),
  41. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  42. },
  43. }
  44. }
  45. func (daemon *Daemon) loadRuntimes() error {
  46. return daemon.initRuntimes(daemon.configStore.Runtimes)
  47. }
  48. func (daemon *Daemon) initRuntimes(runtimes map[string]types.Runtime) (err error) {
  49. runtimeDir := filepath.Join(daemon.configStore.Root, "runtimes")
  50. runtimeOldDir := runtimeDir + "-old"
  51. // Remove old temp directory if any
  52. os.RemoveAll(runtimeOldDir)
  53. tmpDir, err := os.MkdirTemp(daemon.configStore.Root, "gen-runtimes")
  54. if err != nil {
  55. return errors.Wrap(err, "failed to get temp dir to generate runtime scripts")
  56. }
  57. defer func() {
  58. if err != nil {
  59. if err1 := os.RemoveAll(tmpDir); err1 != nil {
  60. logrus.WithError(err1).WithField("dir", tmpDir).
  61. Warn("failed to remove tmp dir")
  62. }
  63. return
  64. }
  65. if err = os.Rename(runtimeDir, runtimeOldDir); err != nil {
  66. logrus.WithError(err).WithField("dir", runtimeDir).
  67. Warn("failed to rename runtimes dir to old. Will try to removing it")
  68. if err = os.RemoveAll(runtimeDir); err != nil {
  69. logrus.WithError(err).WithField("dir", runtimeDir).
  70. Warn("failed to remove old runtimes dir")
  71. return
  72. }
  73. }
  74. if err = os.Rename(tmpDir, runtimeDir); err != nil {
  75. err = errors.Wrap(err, "failed to setup runtimes dir, new containers may not start")
  76. return
  77. }
  78. if err = os.RemoveAll(runtimeOldDir); err != nil {
  79. logrus.WithError(err).WithField("dir", runtimeOldDir).
  80. Warn("failed to remove old runtimes dir")
  81. }
  82. }()
  83. for name := range runtimes {
  84. rt := runtimes[name]
  85. if rt.Path == "" && rt.Type == "" {
  86. return errors.Errorf("runtime %s: either a runtimeType or a path must be configured", name)
  87. }
  88. if rt.Path != "" {
  89. if rt.Type != "" {
  90. return errors.Errorf("runtime %s: cannot configure both path and runtimeType for the same runtime", name)
  91. }
  92. if len(rt.Options) > 0 {
  93. return errors.Errorf("runtime %s: options cannot be used with a path runtime", name)
  94. }
  95. if len(rt.Args) > 0 {
  96. script := filepath.Join(tmpDir, name)
  97. content := fmt.Sprintf("#!/bin/sh\n%s %s $@\n", rt.Path, strings.Join(rt.Args, " "))
  98. if err := os.WriteFile(script, []byte(content), 0700); err != nil {
  99. return err
  100. }
  101. }
  102. rt.ShimConfig = defaultV2ShimConfig(daemon.configStore, daemon.rewriteRuntimePath(name, rt.Path, rt.Args))
  103. var featuresStderr bytes.Buffer
  104. featuresCmd := exec.Command(rt.Path, append(rt.Args, "features")...)
  105. featuresCmd.Stderr = &featuresStderr
  106. if featuresB, err := featuresCmd.Output(); err != nil {
  107. logrus.WithError(err).Warnf("Failed to run %v: %q", featuresCmd.Args, featuresStderr.String())
  108. } else {
  109. var features features.Features
  110. if jsonErr := json.Unmarshal(featuresB, &features); jsonErr != nil {
  111. logrus.WithError(err).Warnf("Failed to unmarshal the output of %v as a JSON", featuresCmd.Args)
  112. } else {
  113. rt.Features = &features
  114. }
  115. }
  116. } else {
  117. if len(rt.Args) > 0 {
  118. return errors.Errorf("runtime %s: args cannot be used with a runtimeType runtime", name)
  119. }
  120. // Unlike implicit runtimes, there is no restriction on configuring a shim by path.
  121. rt.ShimConfig = &types.ShimConfig{Binary: rt.Type}
  122. if len(rt.Options) > 0 {
  123. // It has to be a pointer type or there'll be a panic in containerd/typeurl when we try to start the container.
  124. rt.ShimConfig.Opts, err = shimopts.Generate(rt.Type, rt.Options)
  125. if err != nil {
  126. return errors.Wrapf(err, "runtime %v", name)
  127. }
  128. }
  129. }
  130. runtimes[name] = rt
  131. }
  132. return nil
  133. }
  134. // rewriteRuntimePath is used for runtimes which have custom arguments supplied.
  135. // This is needed because the containerd API only calls the OCI runtime binary, there is no options for extra arguments.
  136. // To support this case, the daemon wraps the specified runtime in a script that passes through those arguments.
  137. func (daemon *Daemon) rewriteRuntimePath(name, p string, args []string) string {
  138. if len(args) == 0 {
  139. return p
  140. }
  141. return filepath.Join(daemon.configStore.Root, "runtimes", name)
  142. }
  143. func (daemon *Daemon) getRuntime(name string) (shim string, opts interface{}, err error) {
  144. rt := daemon.configStore.GetRuntime(name)
  145. if rt == nil {
  146. if !config.IsPermissibleC8dRuntimeName(name) {
  147. return "", nil, errdefs.InvalidParameter(errors.Errorf("unknown or invalid runtime name: %s", name))
  148. }
  149. return name, nil, nil
  150. }
  151. if len(rt.Args) > 0 {
  152. // Check that the path of the runtime which the script wraps actually exists so
  153. // that we can return a well known error which references the configured path
  154. // instead of the wrapper script's.
  155. if _, err := exec.LookPath(rt.Path); err != nil {
  156. return "", nil, errors.Wrap(err, "error while looking up the specified runtime path")
  157. }
  158. }
  159. if rt.ShimConfig == nil {
  160. // Should never happen as daemon.initRuntimes always sets
  161. // ShimConfig and config reloading is synchronized.
  162. err := errdefs.System(errors.Errorf("BUG: runtime %s: rt.ShimConfig == nil", name))
  163. logrus.Error(err)
  164. return "", nil, err
  165. }
  166. return rt.ShimConfig.Binary, rt.ShimConfig.Opts, nil
  167. }