daemon_unix.go 4.6 KB

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