github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/cmd/dockerd/daemon_unix.go (about)

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