runtime_unix.go 4.0 KB

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