daemon_unix.go 4.0 KB

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