daemon_unix.go 4.4 KB

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