runtime_unix.go 4.2 KB

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