github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/cmd/dockerd/daemon_unix.go (about)

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