daemon_unix.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/cmd/dockerd/hack"
  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/pkg/homedir"
  18. "github.com/docker/libnetwork/portallocator"
  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: daemon.DefaultShimBinary,
  61. Runtime: daemon.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 wrapListeners(proto string, ls []net.Listener) []net.Listener {
  109. switch proto {
  110. case "unix":
  111. ls[0] = &hack.MalformedHostHeaderOverride{Listener: ls[0]}
  112. case "fd":
  113. for i := range ls {
  114. ls[i] = &hack.MalformedHostHeaderOverride{Listener: ls[i]}
  115. }
  116. }
  117. return ls
  118. }
  119. func newCgroupParent(config *config.Config) string {
  120. cgroupParent := "docker"
  121. useSystemd := daemon.UsingSystemd(config)
  122. if useSystemd {
  123. cgroupParent = "system.slice"
  124. }
  125. if config.CgroupParent != "" {
  126. cgroupParent = config.CgroupParent
  127. }
  128. if useSystemd {
  129. cgroupParent = cgroupParent + ":" + "docker" + ":"
  130. }
  131. return cgroupParent
  132. }
  133. func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) error, error) {
  134. var waitForShutdown func(time.Duration) error
  135. if cli.Config.ContainerdAddr == "" {
  136. systemContainerdAddr, ok, err := systemContainerdRunning(honorXDG)
  137. if err != nil {
  138. return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
  139. }
  140. if !ok {
  141. logrus.Debug("Containerd not running, starting daemon managed containerd")
  142. opts, err := cli.getContainerdDaemonOpts()
  143. if err != nil {
  144. return nil, errors.Wrap(err, "failed to generate containerd options")
  145. }
  146. r, err := supervisor.Start(ctx, filepath.Join(cli.Config.Root, "containerd"), filepath.Join(cli.Config.ExecRoot, "containerd"), opts...)
  147. if err != nil {
  148. return nil, errors.Wrap(err, "failed to start containerd")
  149. }
  150. logrus.Debug("Started daemon managed containerd")
  151. cli.Config.ContainerdAddr = r.Address()
  152. // Try to wait for containerd to shutdown
  153. waitForShutdown = r.WaitTimeout
  154. } else {
  155. cli.Config.ContainerdAddr = systemContainerdAddr
  156. }
  157. }
  158. return waitForShutdown, nil
  159. }