runtime_unix.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "github.com/containerd/cgroups"
  11. "github.com/containerd/containerd/runtime/linux/runctypes"
  12. v2runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
  13. "github.com/docker/docker/api/types"
  14. "github.com/docker/docker/daemon/config"
  15. "github.com/docker/docker/errdefs"
  16. "github.com/docker/docker/pkg/ioutils"
  17. "github.com/pkg/errors"
  18. "github.com/sirupsen/logrus"
  19. )
  20. const (
  21. defaultRuntimeName = "runc"
  22. linuxShimV1 = "io.containerd.runtime.v1.linux"
  23. linuxShimV2 = "io.containerd.runc.v2"
  24. )
  25. func configureRuntimes(conf *config.Config) {
  26. if conf.DefaultRuntime == "" {
  27. conf.DefaultRuntime = config.StockRuntimeName
  28. }
  29. if conf.Runtimes == nil {
  30. conf.Runtimes = make(map[string]types.Runtime)
  31. }
  32. conf.Runtimes[config.LinuxV1RuntimeName] = types.Runtime{Path: defaultRuntimeName, Shim: defaultV1ShimConfig(conf, defaultRuntimeName)}
  33. conf.Runtimes[config.LinuxV2RuntimeName] = types.Runtime{Path: defaultRuntimeName, Shim: defaultV2ShimConfig(conf, defaultRuntimeName)}
  34. conf.Runtimes[config.StockRuntimeName] = conf.Runtimes[config.LinuxV2RuntimeName]
  35. }
  36. func defaultV2ShimConfig(conf *config.Config, runtimePath string) *types.ShimConfig {
  37. return &types.ShimConfig{
  38. Binary: linuxShimV2,
  39. Opts: &v2runcoptions.Options{
  40. BinaryName: runtimePath,
  41. Root: filepath.Join(conf.ExecRoot, "runtime-"+defaultRuntimeName),
  42. SystemdCgroup: UsingSystemd(conf),
  43. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  44. },
  45. }
  46. }
  47. func defaultV1ShimConfig(conf *config.Config, runtimePath string) *types.ShimConfig {
  48. return &types.ShimConfig{
  49. Binary: linuxShimV1,
  50. Opts: &runctypes.RuncOptions{
  51. Runtime: runtimePath,
  52. RuntimeRoot: filepath.Join(conf.ExecRoot, "runtime-"+defaultRuntimeName),
  53. SystemdCgroup: UsingSystemd(conf),
  54. },
  55. }
  56. }
  57. func (daemon *Daemon) loadRuntimes() error {
  58. return daemon.initRuntimes(daemon.configStore.Runtimes)
  59. }
  60. func (daemon *Daemon) initRuntimes(runtimes map[string]types.Runtime) (err error) {
  61. runtimeDir := filepath.Join(daemon.configStore.Root, "runtimes")
  62. // Remove old temp directory if any
  63. os.RemoveAll(runtimeDir + "-old")
  64. tmpDir, err := ioutils.TempDir(daemon.configStore.Root, "gen-runtimes")
  65. if err != nil {
  66. return errors.Wrap(err, "failed to get temp dir to generate runtime scripts")
  67. }
  68. defer func() {
  69. if err != nil {
  70. if err1 := os.RemoveAll(tmpDir); err1 != nil {
  71. logrus.WithError(err1).WithField("dir", tmpDir).
  72. Warn("failed to remove tmp dir")
  73. }
  74. return
  75. }
  76. if err = os.Rename(runtimeDir, runtimeDir+"-old"); err != nil {
  77. return
  78. }
  79. if err = os.Rename(tmpDir, runtimeDir); err != nil {
  80. err = errors.Wrap(err, "failed to setup runtimes dir, new containers may not start")
  81. return
  82. }
  83. if err = os.RemoveAll(runtimeDir + "-old"); err != nil {
  84. logrus.WithError(err).WithField("dir", tmpDir).
  85. Warn("failed to remove old runtimes dir")
  86. }
  87. }()
  88. for name, rt := range runtimes {
  89. if len(rt.Args) > 0 {
  90. script := filepath.Join(tmpDir, name)
  91. content := fmt.Sprintf("#!/bin/sh\n%s %s $@\n", rt.Path, strings.Join(rt.Args, " "))
  92. if err := os.WriteFile(script, []byte(content), 0700); err != nil {
  93. return err
  94. }
  95. }
  96. if rt.Shim == nil {
  97. rt.Shim = defaultV2ShimConfig(daemon.configStore, rt.Path)
  98. }
  99. }
  100. return nil
  101. }
  102. // rewriteRuntimePath is used for runtimes which have custom arguments supplied.
  103. // This is needed because the containerd API only calls the OCI runtime binary, there is no options for extra arguments.
  104. // To support this case, the daemon wraps the specified runtime in a script that passes through those arguments.
  105. func (daemon *Daemon) rewriteRuntimePath(name, p string, args []string) (string, error) {
  106. if len(args) == 0 {
  107. return p, nil
  108. }
  109. // Check that the runtime path actually exists here so that we can return a well known error.
  110. if _, err := exec.LookPath(p); err != nil {
  111. return "", errors.Wrap(err, "error while looking up the specified runtime path")
  112. }
  113. return filepath.Join(daemon.configStore.Root, "runtimes", name), nil
  114. }
  115. func (daemon *Daemon) getRuntime(name string) (*types.Runtime, error) {
  116. rt := daemon.configStore.GetRuntime(name)
  117. if rt == nil {
  118. return nil, errdefs.InvalidParameter(errors.Errorf("runtime not found in config: %s", name))
  119. }
  120. if len(rt.Args) > 0 {
  121. p, err := daemon.rewriteRuntimePath(name, rt.Path, rt.Args)
  122. if err != nil {
  123. return nil, err
  124. }
  125. rt.Path = p
  126. rt.Args = nil
  127. }
  128. if rt.Shim == nil {
  129. rt.Shim = defaultV2ShimConfig(daemon.configStore, rt.Path)
  130. }
  131. if rt.Shim.Binary == linuxShimV1 {
  132. if cgroups.Mode() == cgroups.Unified {
  133. return nil, errdefs.InvalidParameter(errors.Errorf("runtime %q is not supported while cgroups v2 (unified hierarchy) is being used", name))
  134. }
  135. logrus.Warnf("Configured runtime %q is deprecated and will be removed in the next release", name)
  136. }
  137. return rt, nil
  138. }