runtime_unix.go 3.9 KB

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