runtime_unix.go 5.6 KB

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