浏览代码

Merge pull request #33330 from coolljt0725/fix_sock_is_dir

Don't create source directory while the daemon is being shutdown, fix #30348
Akihiro Suda 8 年之前
父节点
当前提交
cd2255a296
共有 6 个文件被更改,包括 43 次插入4 次删除
  1. 5 0
      cmd/dockerd/daemon.go
  2. 11 0
      daemon/daemon.go
  3. 2 1
      daemon/monitor.go
  4. 13 1
      daemon/volumes_unix.go
  5. 1 1
      daemon/volumes_windows.go
  6. 11 1
      volume/volume.go

+ 5 - 0
cmd/dockerd/daemon.go

@@ -154,6 +154,8 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
 	api := apiserver.New(serverConfig)
 	api := apiserver.New(serverConfig)
 	cli.api = api
 	cli.api = api
 
 
+	var hosts []string
+
 	for i := 0; i < len(cli.Config.Hosts); i++ {
 	for i := 0; i < len(cli.Config.Hosts); i++ {
 		var err error
 		var err error
 		if cli.Config.Hosts[i], err = dopts.ParseHost(cli.Config.TLS, cli.Config.Hosts[i]); err != nil {
 		if cli.Config.Hosts[i], err = dopts.ParseHost(cli.Config.TLS, cli.Config.Hosts[i]); err != nil {
@@ -185,6 +187,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
 			}
 			}
 		}
 		}
 		logrus.Debugf("Listener created for HTTP on %s (%s)", proto, addr)
 		logrus.Debugf("Listener created for HTTP on %s (%s)", proto, addr)
+		hosts = append(hosts, protoAddrParts[1])
 		api.Accept(addr, ls...)
 		api.Accept(addr, ls...)
 	}
 	}
 
 
@@ -212,6 +215,8 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
 		return fmt.Errorf("Error starting daemon: %v", err)
 		return fmt.Errorf("Error starting daemon: %v", err)
 	}
 	}
 
 
+	d.StoreHosts(hosts)
+
 	// validate after NewDaemon has restored enabled plugins. Dont change order.
 	// validate after NewDaemon has restored enabled plugins. Dont change order.
 	if err := validateAuthzPlugins(cli.Config.AuthorizationPlugins, pluginStore); err != nil {
 	if err := validateAuthzPlugins(cli.Config.AuthorizationPlugins, pluginStore); err != nil {
 		return fmt.Errorf("Error validating authorization plugin: %v", err)
 		return fmt.Errorf("Error validating authorization plugin: %v", err)

+ 11 - 0
daemon/daemon.go

@@ -116,6 +116,17 @@ type Daemon struct {
 
 
 	diskUsageRunning int32
 	diskUsageRunning int32
 	pruneRunning     int32
 	pruneRunning     int32
+	hosts            map[string]bool // hosts stores the addresses the daemon is listening on
+}
+
+// StoreHosts stores the addresses the daemon is listening on
+func (daemon *Daemon) StoreHosts(hosts []string) {
+	if daemon.hosts == nil {
+		daemon.hosts = make(map[string]bool)
+	}
+	for _, h := range hosts {
+		daemon.hosts[h] = true
+	}
 }
 }
 
 
 // HasExperimental returns whether the experimental features of the daemon are enabled or not
 // HasExperimental returns whether the experimental features of the daemon are enabled or not

+ 2 - 1
daemon/monitor.go

@@ -46,7 +46,8 @@ func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
 		c.StreamConfig.Wait()
 		c.StreamConfig.Wait()
 		c.Reset(false)
 		c.Reset(false)
 
 
-		restart, wait, err := c.RestartManager().ShouldRestart(e.ExitCode, c.HasBeenManuallyStopped, time.Since(c.StartedAt))
+		// If daemon is being shutdown, don't let the container restart
+		restart, wait, err := c.RestartManager().ShouldRestart(e.ExitCode, daemon.IsShuttingDown() || c.HasBeenManuallyStopped, time.Since(c.StartedAt))
 		if err == nil && restart {
 		if err == nil && restart {
 			c.RestartCount++
 			c.RestartCount++
 			c.SetRestarting(platformConstructExitStatus(e))
 			c.SetRestarting(platformConstructExitStatus(e))

+ 13 - 1
daemon/volumes_unix.go

@@ -6,6 +6,7 @@ package daemon
 
 
 import (
 import (
 	"encoding/json"
 	"encoding/json"
+	"fmt"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
 	"sort"
 	"sort"
@@ -42,8 +43,19 @@ func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, er
 		if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
 		if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
 			return nil, err
 			return nil, err
 		}
 		}
+		// If the daemon is being shutdown, we should not let a container start if it is trying to
+		// mount the socket the daemon is listening on. During daemon shutdown, the socket
+		// (/var/run/docker.sock by default) doesn't exist anymore causing the call to m.Setup to
+		// create at directory instead. This in turn will prevent the daemon to restart.
+		checkfunc := func(m *volume.MountPoint) error {
+			if _, exist := daemon.hosts[m.Source]; exist && daemon.IsShuttingDown() {
+				return fmt.Errorf("Could not mount %q to container while the daemon is shutting down", m.Source)
+			}
+			return nil
+		}
+
 		rootUID, rootGID := daemon.GetRemappedUIDGID()
 		rootUID, rootGID := daemon.GetRemappedUIDGID()
-		path, err := m.Setup(c.MountLabel, rootUID, rootGID)
+		path, err := m.Setup(c.MountLabel, rootUID, rootGID, checkfunc)
 		if err != nil {
 		if err != nil {
 			return nil, err
 			return nil, err
 		}
 		}

+ 1 - 1
daemon/volumes_windows.go

@@ -24,7 +24,7 @@ func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, er
 		if err := daemon.lazyInitializeVolume(c.ID, mount); err != nil {
 		if err := daemon.lazyInitializeVolume(c.ID, mount); err != nil {
 			return nil, err
 			return nil, err
 		}
 		}
-		s, err := mount.Setup(c.MountLabel, 0, 0)
+		s, err := mount.Setup(c.MountLabel, 0, 0, nil)
 		if err != nil {
 		if err != nil {
 			return nil, err
 			return nil, err
 		}
 		}

+ 11 - 1
volume/volume.go

@@ -149,7 +149,9 @@ func (m *MountPoint) Cleanup() error {
 
 
 // Setup sets up a mount point by either mounting the volume if it is
 // Setup sets up a mount point by either mounting the volume if it is
 // configured, or creating the source directory if supplied.
 // configured, or creating the source directory if supplied.
-func (m *MountPoint) Setup(mountLabel string, rootUID, rootGID int) (path string, err error) {
+// The, optional, checkFun parameter allows doing additional checking
+// before creating the source directory on the host.
+func (m *MountPoint) Setup(mountLabel string, rootUID, rootGID int, checkFun func(m *MountPoint) error) (path string, err error) {
 	defer func() {
 	defer func() {
 		if err == nil {
 		if err == nil {
 			if label.RelabelNeeded(m.Mode) {
 			if label.RelabelNeeded(m.Mode) {
@@ -184,6 +186,14 @@ func (m *MountPoint) Setup(mountLabel string, rootUID, rootGID int) (path string
 
 
 	// system.MkdirAll() produces an error if m.Source exists and is a file (not a directory),
 	// system.MkdirAll() produces an error if m.Source exists and is a file (not a directory),
 	if m.Type == mounttypes.TypeBind {
 	if m.Type == mounttypes.TypeBind {
+		// Before creating the source directory on the host, invoke checkFun if it's not nil. One of
+		// the use case is to forbid creating the daemon socket as a directory if the daemon is in
+		// the process of shutting down.
+		if checkFun != nil {
+			if err := checkFun(m); err != nil {
+				return "", err
+			}
+		}
 		// idtools.MkdirAllNewAs() produces an error if m.Source exists and is a file (not a directory)
 		// idtools.MkdirAllNewAs() produces an error if m.Source exists and is a file (not a directory)
 		// also, makes sure that if the directory is created, the correct remapped rootUID/rootGID will own it
 		// also, makes sure that if the directory is created, the correct remapped rootUID/rootGID will own it
 		if err := idtools.MkdirAllNewAs(m.Source, 0755, rootUID, rootGID); err != nil {
 		if err := idtools.MkdirAllNewAs(m.Source, 0755, rootUID, rootGID); err != nil {