2017-11-01 19:37:53 -04:00
|
|
|
// +build !windows
|
2015-05-15 19:34:26 -04:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-06-15 09:36:19 -04:00
|
|
|
"fmt"
|
2016-04-09 02:49:33 -04:00
|
|
|
"net"
|
2015-05-15 19:34:26 -04:00
|
|
|
"os"
|
2015-12-10 18:35:10 -05:00
|
|
|
"os/signal"
|
2016-03-24 14:42:03 -04:00
|
|
|
"path/filepath"
|
2016-04-09 02:49:33 -04:00
|
|
|
"strconv"
|
2015-05-15 19:34:26 -04:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
"github.com/containerd/containerd/linux"
|
2016-04-12 11:21:20 -04:00
|
|
|
"github.com/docker/docker/cmd/dockerd/hack"
|
2015-05-15 19:34:26 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2016-03-18 14:50:19 -04:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2017-09-22 09:52:41 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2016-04-09 02:49:33 -04:00
|
|
|
"github.com/docker/libnetwork/portallocator"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2015-05-15 19:34:26 -04:00
|
|
|
)
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
const defaultDaemonConfigFile = "/etc/docker/daemon.json"
|
|
|
|
|
2015-06-15 09:36:19 -04:00
|
|
|
// setDefaultUmask sets the umask to 0022 to avoid problems
|
|
|
|
// caused by custom umask
|
|
|
|
func setDefaultUmask() error {
|
|
|
|
desiredUmask := 0022
|
2017-05-23 10:22:32 -04:00
|
|
|
unix.Umask(desiredUmask)
|
|
|
|
if umask := unix.Umask(desiredUmask); umask != desiredUmask {
|
2015-06-15 09:36:19 -04:00
|
|
|
return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-30 15:39:19 -04:00
|
|
|
|
2016-11-04 15:42:21 -04:00
|
|
|
func getDaemonConfDir(_ string) string {
|
2015-07-30 15:39:19 -04:00
|
|
|
return "/etc/docker"
|
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
func (cli *DaemonCli) getPlatformRemoteOptions() ([]libcontainerd.RemoteOption, error) {
|
|
|
|
// On older kernel, letting putting the containerd-shim in its own
|
|
|
|
// namespace will effectively prevent operations such as unlink, rename
|
|
|
|
// and remove on mountpoints that were present at the time the shim
|
|
|
|
// namespace was created. This would led to a famous EBUSY will trying to
|
|
|
|
// remove shm mounts.
|
|
|
|
var noNewNS bool
|
|
|
|
if !kernel.CheckKernelVersion(3, 18, 0) {
|
|
|
|
noNewNS = true
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
|
|
|
|
opts := []libcontainerd.RemoteOption{
|
2016-07-11 18:26:23 -04:00
|
|
|
libcontainerd.WithOOMScore(cli.Config.OOMScoreAdjust),
|
2017-09-22 09:52:41 -04:00
|
|
|
libcontainerd.WithPlugin("linux", &linux.Config{
|
|
|
|
Shim: daemon.DefaultShimBinary,
|
|
|
|
Runtime: daemon.DefaultRuntimeBinary,
|
|
|
|
RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
|
|
|
|
ShimDebug: cli.Config.Debug,
|
|
|
|
ShimNoMountNS: noNewNS,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
if cli.Config.Debug {
|
|
|
|
opts = append(opts, libcontainerd.WithLogLevel("debug"))
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
if cli.Config.ContainerdAddr != "" {
|
|
|
|
opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
|
|
|
|
} else {
|
|
|
|
opts = append(opts, libcontainerd.WithStartDaemon(true))
|
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
|
|
|
|
return opts, nil
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2016-03-24 14:42:03 -04:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
// setupConfigReloadTrap configures the USR2 signal to reload the configuration.
|
|
|
|
func (cli *DaemonCli) setupConfigReloadTrap() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, unix.SIGHUP)
|
|
|
|
go func() {
|
|
|
|
for range c {
|
|
|
|
cli.reloadConfig()
|
|
|
|
}
|
|
|
|
}()
|
2016-03-24 14:42:03 -04:00
|
|
|
}
|
2016-04-09 02:49:33 -04:00
|
|
|
|
2016-08-19 16:06:28 -04:00
|
|
|
// getSwarmRunRoot gets the root directory for swarm to store runtime state
|
|
|
|
// For example, the control socket
|
|
|
|
func (cli *DaemonCli) getSwarmRunRoot() string {
|
|
|
|
return filepath.Join(cli.Config.ExecRoot, "swarm")
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:49:33 -04:00
|
|
|
// allocateDaemonPort ensures that there are no containers
|
|
|
|
// that try to use any port allocated for the docker server.
|
|
|
|
func allocateDaemonPort(addr string) error {
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
intPort, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var hostIPs []net.IP
|
|
|
|
if parsedIP := net.ParseIP(host); parsedIP != nil {
|
|
|
|
hostIPs = append(hostIPs, parsedIP)
|
|
|
|
} else if hostIPs, err = net.LookupIP(host); err != nil {
|
|
|
|
return fmt.Errorf("failed to lookup %s address in host specification", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
pa := portallocator.Get()
|
|
|
|
for _, hostIP := range hostIPs {
|
|
|
|
if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
|
|
|
|
return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-22 20:16:14 -04:00
|
|
|
|
|
|
|
// notifyShutdown is called after the daemon shuts down but before the process exits.
|
|
|
|
func notifyShutdown(err error) {
|
|
|
|
}
|
2016-04-12 11:21:20 -04:00
|
|
|
|
|
|
|
func wrapListeners(proto string, ls []net.Listener) []net.Listener {
|
2016-05-21 23:40:15 -04:00
|
|
|
switch proto {
|
|
|
|
case "unix":
|
|
|
|
ls[0] = &hack.MalformedHostHeaderOverride{ls[0]}
|
|
|
|
case "fd":
|
|
|
|
for i := range ls {
|
|
|
|
ls[i] = &hack.MalformedHostHeaderOverride{ls[i]}
|
2016-04-12 11:21:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|