daemon_unix.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //go:build !windows
  2. // +build !windows
  3. package main
  4. import (
  5. "context"
  6. "fmt"
  7. "net"
  8. "os"
  9. "os/signal"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. "github.com/containerd/containerd/runtime/v1/linux"
  14. "github.com/docker/docker/daemon"
  15. "github.com/docker/docker/daemon/config"
  16. "github.com/docker/docker/libcontainerd/supervisor"
  17. "github.com/docker/docker/libnetwork/portallocator"
  18. "github.com/docker/docker/pkg/homedir"
  19. "github.com/pkg/errors"
  20. "github.com/sirupsen/logrus"
  21. "golang.org/x/sys/unix"
  22. )
  23. func getDefaultDaemonConfigDir() (string, error) {
  24. if !honorXDG {
  25. return "/etc/docker", nil
  26. }
  27. // NOTE: CLI uses ~/.docker while the daemon uses ~/.config/docker, because
  28. // ~/.docker was not designed to store daemon configurations.
  29. // In future, the daemon directory may be renamed to ~/.config/moby-engine (?).
  30. configHome, err := homedir.GetConfigHome()
  31. if err != nil {
  32. return "", nil
  33. }
  34. return filepath.Join(configHome, "docker"), nil
  35. }
  36. func getDefaultDaemonConfigFile() (string, error) {
  37. dir, err := getDefaultDaemonConfigDir()
  38. if err != nil {
  39. return "", err
  40. }
  41. return filepath.Join(dir, "daemon.json"), nil
  42. }
  43. // setDefaultUmask sets the umask to 0022 to avoid problems
  44. // caused by custom umask
  45. func setDefaultUmask() error {
  46. desiredUmask := 0022
  47. unix.Umask(desiredUmask)
  48. if umask := unix.Umask(desiredUmask); umask != desiredUmask {
  49. return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
  50. }
  51. return nil
  52. }
  53. func getDaemonConfDir(_ string) (string, error) {
  54. return getDefaultDaemonConfigDir()
  55. }
  56. func (cli *DaemonCli) getPlatformContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
  57. opts := []supervisor.DaemonOpt{
  58. supervisor.WithOOMScore(cli.Config.OOMScoreAdjust),
  59. supervisor.WithPlugin("linux", &linux.Config{
  60. Shim: config.DefaultShimBinary,
  61. Runtime: config.DefaultRuntimeBinary,
  62. RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
  63. ShimDebug: cli.Config.Debug,
  64. }),
  65. }
  66. return opts, nil
  67. }
  68. // setupConfigReloadTrap configures the SIGHUP signal to reload the configuration.
  69. func (cli *DaemonCli) setupConfigReloadTrap() {
  70. c := make(chan os.Signal, 1)
  71. signal.Notify(c, unix.SIGHUP)
  72. go func() {
  73. for range c {
  74. cli.reloadConfig()
  75. }
  76. }()
  77. }
  78. // getSwarmRunRoot gets the root directory for swarm to store runtime state
  79. // For example, the control socket
  80. func (cli *DaemonCli) getSwarmRunRoot() string {
  81. return filepath.Join(cli.Config.ExecRoot, "swarm")
  82. }
  83. // allocateDaemonPort ensures that there are no containers
  84. // that try to use any port allocated for the docker server.
  85. func allocateDaemonPort(addr string) error {
  86. host, port, err := net.SplitHostPort(addr)
  87. if err != nil {
  88. return err
  89. }
  90. intPort, err := strconv.Atoi(port)
  91. if err != nil {
  92. return err
  93. }
  94. var hostIPs []net.IP
  95. if parsedIP := net.ParseIP(host); parsedIP != nil {
  96. hostIPs = append(hostIPs, parsedIP)
  97. } else if hostIPs, err = net.LookupIP(host); err != nil {
  98. return fmt.Errorf("failed to lookup %s address in host specification", host)
  99. }
  100. pa := portallocator.Get()
  101. for _, hostIP := range hostIPs {
  102. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  103. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  104. }
  105. }
  106. return nil
  107. }
  108. func newCgroupParent(config *config.Config) string {
  109. cgroupParent := "docker"
  110. useSystemd := daemon.UsingSystemd(config)
  111. if useSystemd {
  112. cgroupParent = "system.slice"
  113. }
  114. if config.CgroupParent != "" {
  115. cgroupParent = config.CgroupParent
  116. }
  117. if useSystemd {
  118. cgroupParent = cgroupParent + ":" + "docker" + ":"
  119. }
  120. return cgroupParent
  121. }
  122. func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) error, error) {
  123. var waitForShutdown func(time.Duration) error
  124. if cli.Config.ContainerdAddr == "" {
  125. systemContainerdAddr, ok, err := systemContainerdRunning(honorXDG)
  126. if err != nil {
  127. return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
  128. }
  129. if !ok {
  130. logrus.Debug("Containerd not running, starting daemon managed containerd")
  131. opts, err := cli.getContainerdDaemonOpts()
  132. if err != nil {
  133. return nil, errors.Wrap(err, "failed to generate containerd options")
  134. }
  135. r, err := supervisor.Start(ctx, filepath.Join(cli.Config.Root, "containerd"), filepath.Join(cli.Config.ExecRoot, "containerd"), opts...)
  136. if err != nil {
  137. return nil, errors.Wrap(err, "failed to start containerd")
  138. }
  139. logrus.Debug("Started daemon managed containerd")
  140. cli.Config.ContainerdAddr = r.Address()
  141. // Try to wait for containerd to shutdown
  142. waitForShutdown = r.WaitTimeout
  143. } else {
  144. cli.Config.ContainerdAddr = systemContainerdAddr
  145. }
  146. }
  147. return waitForShutdown, nil
  148. }