daemon_unix.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 (cli *DaemonCli) getPlatformContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
  52. opts := []supervisor.DaemonOpt{
  53. // TODO(thaJeztah) change this to use /proc/self/oom_score_adj instead,
  54. // which would allow us to set the correct score even if dockerd's score
  55. // was set through other means (such as systemd or "manually").
  56. supervisor.WithOOMScore(cli.Config.OOMScoreAdjust), //nolint:staticcheck // ignore SA1019 (WithOOMScore is deprecated); will be removed in the next release.
  57. }
  58. if cli.Config.OOMScoreAdjust != 0 {
  59. logrus.Warn(`DEPRECATED: The "oom-score-adjust" config parameter and the dockerd "--oom-score-adjust" option will be removed in the next release.`)
  60. }
  61. return opts, nil
  62. }
  63. // setupConfigReloadTrap configures the SIGHUP signal to reload the configuration.
  64. func (cli *DaemonCli) setupConfigReloadTrap() {
  65. c := make(chan os.Signal, 1)
  66. signal.Notify(c, unix.SIGHUP)
  67. go func() {
  68. for range c {
  69. cli.reloadConfig()
  70. }
  71. }()
  72. }
  73. // getSwarmRunRoot gets the root directory for swarm to store runtime state
  74. // For example, the control socket
  75. func (cli *DaemonCli) getSwarmRunRoot() string {
  76. return filepath.Join(cli.Config.ExecRoot, "swarm")
  77. }
  78. // allocateDaemonPort ensures that there are no containers
  79. // that try to use any port allocated for the docker server.
  80. func allocateDaemonPort(addr string) error {
  81. host, port, err := net.SplitHostPort(addr)
  82. if err != nil {
  83. return errors.Wrap(err, "error parsing tcp address")
  84. }
  85. intPort, err := strconv.Atoi(port)
  86. if err != nil {
  87. return errors.Wrap(err, "error parsing tcp address")
  88. }
  89. var hostIPs []net.IP
  90. if parsedIP := net.ParseIP(host); parsedIP != nil {
  91. hostIPs = append(hostIPs, parsedIP)
  92. } else if hostIPs, err = net.LookupIP(host); err != nil {
  93. return errors.Errorf("failed to lookup %s address in host specification", host)
  94. }
  95. pa := portallocator.Get()
  96. for _, hostIP := range hostIPs {
  97. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  98. return errors.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  99. }
  100. }
  101. return nil
  102. }
  103. func newCgroupParent(config *config.Config) string {
  104. cgroupParent := "docker"
  105. useSystemd := daemon.UsingSystemd(config)
  106. if useSystemd {
  107. cgroupParent = "system.slice"
  108. }
  109. if config.CgroupParent != "" {
  110. cgroupParent = config.CgroupParent
  111. }
  112. if useSystemd {
  113. cgroupParent = cgroupParent + ":" + "docker" + ":"
  114. }
  115. return cgroupParent
  116. }
  117. func (cli *DaemonCli) initContainerd(ctx context.Context) (func(time.Duration) error, error) {
  118. if cli.ContainerdAddr != "" {
  119. // use system containerd at the given address.
  120. return nil, nil
  121. }
  122. systemContainerdAddr, ok, err := systemContainerdRunning(honorXDG)
  123. if err != nil {
  124. return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
  125. }
  126. if ok {
  127. // detected a system containerd at the given address.
  128. cli.ContainerdAddr = systemContainerdAddr
  129. return nil, nil
  130. }
  131. logrus.Info("containerd not running, starting managed containerd")
  132. opts, err := cli.getContainerdDaemonOpts()
  133. if err != nil {
  134. return nil, errors.Wrap(err, "failed to generate containerd options")
  135. }
  136. r, err := supervisor.Start(ctx, filepath.Join(cli.Root, "containerd"), filepath.Join(cli.ExecRoot, "containerd"), opts...)
  137. if err != nil {
  138. return nil, errors.Wrap(err, "failed to start containerd")
  139. }
  140. cli.ContainerdAddr = r.Address()
  141. // Try to wait for containerd to shutdown
  142. return r.WaitTimeout, nil
  143. }