runtime_unix.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/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, Shim: 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, rt := range runtimes {
  81. if len(rt.Args) > 0 {
  82. script := filepath.Join(tmpDir, name)
  83. content := fmt.Sprintf("#!/bin/sh\n%s %s $@\n", rt.Path, strings.Join(rt.Args, " "))
  84. if err := os.WriteFile(script, []byte(content), 0700); err != nil {
  85. return err
  86. }
  87. }
  88. if rt.Shim == nil {
  89. rt.Shim = defaultV2ShimConfig(daemon.configStore, rt.Path)
  90. }
  91. }
  92. return nil
  93. }
  94. // rewriteRuntimePath is used for runtimes which have custom arguments supplied.
  95. // This is needed because the containerd API only calls the OCI runtime binary, there is no options for extra arguments.
  96. // To support this case, the daemon wraps the specified runtime in a script that passes through those arguments.
  97. func (daemon *Daemon) rewriteRuntimePath(name, p string, args []string) (string, error) {
  98. if len(args) == 0 {
  99. return p, nil
  100. }
  101. // Check that the runtime path actually exists here so that we can return a well known error.
  102. if _, err := exec.LookPath(p); err != nil {
  103. return "", errors.Wrap(err, "error while looking up the specified runtime path")
  104. }
  105. return filepath.Join(daemon.configStore.Root, "runtimes", name), nil
  106. }
  107. func (daemon *Daemon) getRuntime(name string) (*types.Runtime, error) {
  108. rt := daemon.configStore.GetRuntime(name)
  109. if rt == nil {
  110. if !config.IsPermissibleC8dRuntimeName(name) {
  111. return nil, errdefs.InvalidParameter(errors.Errorf("unknown or invalid runtime name: %s", name))
  112. }
  113. return &types.Runtime{Shim: &types.ShimConfig{Binary: name}}, nil
  114. }
  115. if len(rt.Args) > 0 {
  116. p, err := daemon.rewriteRuntimePath(name, rt.Path, rt.Args)
  117. if err != nil {
  118. return nil, err
  119. }
  120. rt.Path = p
  121. rt.Args = nil
  122. }
  123. if rt.Shim == nil {
  124. rt.Shim = defaultV2ShimConfig(daemon.configStore, rt.Path)
  125. }
  126. return rt, nil
  127. }