runtime_unix.go 5.6 KB

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