runtime_unix.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // Remove old temp directory if any
  48. os.RemoveAll(runtimeDir + "-old")
  49. tmpDir, err := os.MkdirTemp(daemon.configStore.Root, "gen-runtimes")
  50. if err != nil {
  51. return errors.Wrap(err, "failed to get temp dir to generate runtime scripts")
  52. }
  53. defer func() {
  54. if err != nil {
  55. if err1 := os.RemoveAll(tmpDir); err1 != nil {
  56. logrus.WithError(err1).WithField("dir", tmpDir).
  57. Warn("failed to remove tmp dir")
  58. }
  59. return
  60. }
  61. if err = os.Rename(runtimeDir, runtimeDir+"-old"); err != nil {
  62. if err = os.RemoveAll(runtimeDir); err != nil {
  63. return
  64. }
  65. }
  66. if err = os.Rename(tmpDir, runtimeDir); err != nil {
  67. err = errors.Wrap(err, "failed to setup runtimes dir, new containers may not start")
  68. return
  69. }
  70. if err = os.RemoveAll(runtimeDir + "-old"); err != nil {
  71. logrus.WithError(err).WithField("dir", tmpDir).
  72. Warn("failed to remove old runtimes dir")
  73. }
  74. }()
  75. for name, rt := range runtimes {
  76. if len(rt.Args) > 0 {
  77. script := filepath.Join(tmpDir, name)
  78. content := fmt.Sprintf("#!/bin/sh\n%s %s $@\n", rt.Path, strings.Join(rt.Args, " "))
  79. if err := os.WriteFile(script, []byte(content), 0700); err != nil {
  80. return err
  81. }
  82. }
  83. if rt.Shim == nil {
  84. rt.Shim = defaultV2ShimConfig(daemon.configStore, rt.Path)
  85. }
  86. }
  87. return nil
  88. }
  89. // rewriteRuntimePath is used for runtimes which have custom arguments supplied.
  90. // This is needed because the containerd API only calls the OCI runtime binary, there is no options for extra arguments.
  91. // To support this case, the daemon wraps the specified runtime in a script that passes through those arguments.
  92. func (daemon *Daemon) rewriteRuntimePath(name, p string, args []string) (string, error) {
  93. if len(args) == 0 {
  94. return p, nil
  95. }
  96. // Check that the runtime path actually exists here so that we can return a well known error.
  97. if _, err := exec.LookPath(p); err != nil {
  98. return "", errors.Wrap(err, "error while looking up the specified runtime path")
  99. }
  100. return filepath.Join(daemon.configStore.Root, "runtimes", name), nil
  101. }
  102. func (daemon *Daemon) getRuntime(name string) (*types.Runtime, error) {
  103. rt := daemon.configStore.GetRuntime(name)
  104. if rt == nil {
  105. if !config.IsPermissibleC8dRuntimeName(name) {
  106. return nil, errdefs.InvalidParameter(errors.Errorf("unknown or invalid runtime name: %s", name))
  107. }
  108. return &types.Runtime{Shim: &types.ShimConfig{Binary: name}}, nil
  109. }
  110. if len(rt.Args) > 0 {
  111. p, err := daemon.rewriteRuntimePath(name, rt.Path, rt.Args)
  112. if err != nil {
  113. return nil, err
  114. }
  115. rt.Path = p
  116. rt.Args = nil
  117. }
  118. if rt.Shim == nil {
  119. rt.Shim = defaultV2ShimConfig(daemon.configStore, rt.Path)
  120. }
  121. return rt, nil
  122. }