daemon_unix.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "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 (cli *DaemonCli) getPlatformContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
  53. opts := []supervisor.DaemonOpt{
  54. supervisor.WithOOMScore(cli.Config.OOMScoreAdjust),
  55. supervisor.WithPlugin("linux", &linux.Config{
  56. Shim: daemon.DefaultShimBinary,
  57. Runtime: daemon.DefaultRuntimeBinary,
  58. RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
  59. ShimDebug: cli.Config.Debug,
  60. }),
  61. }
  62. return opts, nil
  63. }
  64. // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
  65. func (cli *DaemonCli) setupConfigReloadTrap() {
  66. c := make(chan os.Signal, 1)
  67. signal.Notify(c, unix.SIGHUP)
  68. go func() {
  69. for range c {
  70. cli.reloadConfig()
  71. }
  72. }()
  73. }
  74. // getSwarmRunRoot gets the root directory for swarm to store runtime state
  75. // For example, the control socket
  76. func (cli *DaemonCli) getSwarmRunRoot() string {
  77. return filepath.Join(cli.Config.ExecRoot, "swarm")
  78. }
  79. // allocateDaemonPort ensures that there are no containers
  80. // that try to use any port allocated for the docker server.
  81. func allocateDaemonPort(addr string) error {
  82. host, port, err := net.SplitHostPort(addr)
  83. if err != nil {
  84. return err
  85. }
  86. intPort, err := strconv.Atoi(port)
  87. if err != nil {
  88. return err
  89. }
  90. var hostIPs []net.IP
  91. if parsedIP := net.ParseIP(host); parsedIP != nil {
  92. hostIPs = append(hostIPs, parsedIP)
  93. } else if hostIPs, err = net.LookupIP(host); err != nil {
  94. return fmt.Errorf("failed to lookup %s address in host specification", host)
  95. }
  96. pa := portallocator.Get()
  97. for _, hostIP := range hostIPs {
  98. if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
  99. return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
  100. }
  101. }
  102. return nil
  103. }
  104. func wrapListeners(proto string, ls []net.Listener) []net.Listener {
  105. switch proto {
  106. case "unix":
  107. ls[0] = &hack.MalformedHostHeaderOverride{Listener: ls[0]}
  108. case "fd":
  109. for i := range ls {
  110. ls[i] = &hack.MalformedHostHeaderOverride{Listener: ls[i]}
  111. }
  112. }
  113. return ls
  114. }
  115. func newCgroupParent(config *config.Config) string {
  116. cgroupParent := "docker"
  117. useSystemd := daemon.UsingSystemd(config)
  118. if useSystemd {
  119. cgroupParent = "system.slice"
  120. }
  121. if config.CgroupParent != "" {
  122. cgroupParent = config.CgroupParent
  123. }
  124. if useSystemd {
  125. cgroupParent = cgroupParent + ":" + "docker" + ":"
  126. }
  127. return cgroupParent
  128. }
  129. func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) error, error) {
  130. var waitForShutdown func(time.Duration) error
  131. if cli.Config.ContainerdAddr == "" {
  132. systemContainerdAddr, ok, err := systemContainerdRunning(honorXDG)
  133. if err != nil {
  134. return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
  135. }
  136. if !ok {
  137. opts, err := cli.getContainerdDaemonOpts()
  138. if err != nil {
  139. return nil, errors.Wrap(err, "failed to generate containerd options")
  140. }
  141. r, err := supervisor.Start(ctx, filepath.Join(cli.Config.Root, "containerd"), filepath.Join(cli.Config.ExecRoot, "containerd"), opts...)
  142. if err != nil {
  143. return nil, errors.Wrap(err, "failed to start containerd")
  144. }
  145. cli.Config.ContainerdAddr = r.Address()
  146. // Try to wait for containerd to shutdown
  147. waitForShutdown = r.WaitTimeout
  148. } else {
  149. cli.Config.ContainerdAddr = systemContainerdAddr
  150. }
  151. }
  152. return waitForShutdown, nil
  153. }