runtime_unix.go 4.1 KB

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