2015-04-27 12:25:38 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-04-27 12:25:38 -04:00
|
|
|
|
|
|
|
import (
|
Don't create source directory while the daemon is being shutdown, fix #30348
If a container mount the socket the daemon is listening on into
container while the daemon is being shutdown, the socket will
not exist on the host, then daemon will assume it's a directory
and create it on the host, this will cause the daemon can't start
next time.
fix issue https://github.com/moby/moby/issues/30348
To reproduce this issue, you can add following code
```
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
+ "time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/container"
@@ -666,7 +667,8 @@ func (daemon *Daemon) createSpec(c *container.Container) (*libcontainerd.Spec, e
if err := daemon.setupIpcDirs(c); err != nil {
return nil, err
}
-
+ fmt.Printf("===please stop the daemon===\n")
+ time.Sleep(time.Second * 2)
ms, err := daemon.setupMounts(c)
if err != nil {
return nil, err
```
step1 run a container which has `--restart always` and `-v /var/run/docker.sock:/sock`
```
$ docker run -ti --restart always -v /var/run/docker.sock:/sock busybox
/ #
```
step2 exit the the container
```
/ # exit
```
and kill the daemon when you see
```
===please stop the daemon===
```
in the daemon log
The daemon can't restart again and fail with `can't create unix socket /var/run/docker.sock: is a directory`.
Signed-off-by: Lei Jitang <leijitang@huawei.com>
2017-05-22 03:44:01 -04:00
|
|
|
"fmt"
|
2015-04-27 12:25:38 -04:00
|
|
|
"os"
|
2015-05-19 16:05:25 -04:00
|
|
|
"sort"
|
2015-12-21 19:45:31 -05:00
|
|
|
"strconv"
|
2016-09-06 09:49:10 -04:00
|
|
|
"strings"
|
2015-04-27 12:25:38 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2016-06-07 03:45:21 -04:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
|
|
|
"github.com/docker/docker/pkg/mount"
|
2018-04-17 16:50:28 -04:00
|
|
|
volumemounts "github.com/docker/docker/volume/mounts"
|
2015-04-27 12:25:38 -04:00
|
|
|
)
|
|
|
|
|
2015-07-30 19:10:56 -04:00
|
|
|
// setupMounts iterates through each of the mount points for a container and
|
|
|
|
// calls Setup() on each. It also looks to see if is a network mount such as
|
|
|
|
// /etc/resolv.conf, and if it is not, appends it to the array of mounts.
|
2016-03-18 14:50:19 -04:00
|
|
|
func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, error) {
|
|
|
|
var mounts []container.Mount
|
2016-06-06 05:57:11 -04:00
|
|
|
// TODO: tmpfs mounts should be part of Mountpoints
|
|
|
|
tmpfsMounts := make(map[string]bool)
|
2016-09-22 16:14:15 -04:00
|
|
|
tmpfsMountInfo, err := c.TmpfsMounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, m := range tmpfsMountInfo {
|
2016-06-06 05:57:11 -04:00
|
|
|
tmpfsMounts[m.Destination] = true
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
for _, m := range c.MountPoints {
|
2016-06-06 05:57:11 -04:00
|
|
|
if tmpfsMounts[m.Destination] {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
|
2016-01-12 17:18:57 -05:00
|
|
|
return nil, err
|
2015-12-09 14:39:31 -05:00
|
|
|
}
|
Don't create source directory while the daemon is being shutdown, fix #30348
If a container mount the socket the daemon is listening on into
container while the daemon is being shutdown, the socket will
not exist on the host, then daemon will assume it's a directory
and create it on the host, this will cause the daemon can't start
next time.
fix issue https://github.com/moby/moby/issues/30348
To reproduce this issue, you can add following code
```
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
+ "time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/container"
@@ -666,7 +667,8 @@ func (daemon *Daemon) createSpec(c *container.Container) (*libcontainerd.Spec, e
if err := daemon.setupIpcDirs(c); err != nil {
return nil, err
}
-
+ fmt.Printf("===please stop the daemon===\n")
+ time.Sleep(time.Second * 2)
ms, err := daemon.setupMounts(c)
if err != nil {
return nil, err
```
step1 run a container which has `--restart always` and `-v /var/run/docker.sock:/sock`
```
$ docker run -ti --restart always -v /var/run/docker.sock:/sock busybox
/ #
```
step2 exit the the container
```
/ # exit
```
and kill the daemon when you see
```
===please stop the daemon===
```
in the daemon log
The daemon can't restart again and fail with `can't create unix socket /var/run/docker.sock: is a directory`.
Signed-off-by: Lei Jitang <leijitang@huawei.com>
2017-05-22 03:44:01 -04:00
|
|
|
// 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.
|
2018-04-17 16:50:28 -04:00
|
|
|
checkfunc := func(m *volumemounts.MountPoint) error {
|
Don't create source directory while the daemon is being shutdown, fix #30348
If a container mount the socket the daemon is listening on into
container while the daemon is being shutdown, the socket will
not exist on the host, then daemon will assume it's a directory
and create it on the host, this will cause the daemon can't start
next time.
fix issue https://github.com/moby/moby/issues/30348
To reproduce this issue, you can add following code
```
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
+ "time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/container"
@@ -666,7 +667,8 @@ func (daemon *Daemon) createSpec(c *container.Container) (*libcontainerd.Spec, e
if err := daemon.setupIpcDirs(c); err != nil {
return nil, err
}
-
+ fmt.Printf("===please stop the daemon===\n")
+ time.Sleep(time.Second * 2)
ms, err := daemon.setupMounts(c)
if err != nil {
return nil, err
```
step1 run a container which has `--restart always` and `-v /var/run/docker.sock:/sock`
```
$ docker run -ti --restart always -v /var/run/docker.sock:/sock busybox
/ #
```
step2 exit the the container
```
/ # exit
```
and kill the daemon when you see
```
===please stop the daemon===
```
in the daemon log
The daemon can't restart again and fail with `can't create unix socket /var/run/docker.sock: is a directory`.
Signed-off-by: Lei Jitang <leijitang@huawei.com>
2017-05-22 03:44:01 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:56:23 -04:00
|
|
|
path, err := m.Setup(c.MountLabel, daemon.idMappings.RootPair(), checkfunc)
|
2015-05-19 16:05:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
if !c.TrySetNetworkMount(m.Destination, path) {
|
|
|
|
mnt := container.Mount{
|
2015-07-13 20:54:00 -04:00
|
|
|
Source: path,
|
|
|
|
Destination: m.Destination,
|
|
|
|
Writable: m.RW,
|
2016-08-15 12:13:18 -04:00
|
|
|
Propagation: string(m.Propagation),
|
2015-10-23 16:57:57 -04:00
|
|
|
}
|
2015-12-21 19:45:31 -05:00
|
|
|
if m.Volume != nil {
|
|
|
|
attributes := map[string]string{
|
|
|
|
"driver": m.Volume.DriverName(),
|
2016-03-18 14:50:19 -04:00
|
|
|
"container": c.ID,
|
2015-12-21 19:45:31 -05:00
|
|
|
"destination": m.Destination,
|
|
|
|
"read/write": strconv.FormatBool(m.RW),
|
2016-08-15 12:13:18 -04:00
|
|
|
"propagation": string(m.Propagation),
|
2015-12-21 19:45:31 -05:00
|
|
|
}
|
|
|
|
daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
|
|
|
|
}
|
2015-10-23 16:57:57 -04:00
|
|
|
mounts = append(mounts, mnt)
|
2015-07-13 20:54:00 -04:00
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
|
|
|
|
mounts = sortMounts(mounts)
|
2016-03-18 14:50:19 -04:00
|
|
|
netMounts := c.NetworkMounts()
|
2015-10-08 11:51:41 -04:00
|
|
|
// if we are going to mount any of the network files from container
|
|
|
|
// metadata, the ownership must be set properly for potential container
|
|
|
|
// remapped root (user namespaces)
|
2017-05-31 17:56:23 -04:00
|
|
|
rootIDs := daemon.idMappings.RootPair()
|
2015-10-08 11:51:41 -04:00
|
|
|
for _, mount := range netMounts {
|
2017-07-22 22:11:09 -04:00
|
|
|
// we should only modify ownership of network files within our own container
|
|
|
|
// metadata repository. If the user specifies a mount path external, it is
|
|
|
|
// up to the user to make sure the file has proper ownership for userns
|
|
|
|
if strings.Index(mount.Source, daemon.repository) == 0 {
|
|
|
|
if err := os.Chown(mount.Source, rootIDs.UID, rootIDs.GID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(mounts, netMounts...), nil
|
2015-04-29 18:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 19:10:56 -04:00
|
|
|
// sortMounts sorts an array of mounts in lexicographic order. This ensure that
|
|
|
|
// when mounting, the mounts don't shadow other mounts. For example, if mounting
|
|
|
|
// /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
|
2016-03-18 14:50:19 -04:00
|
|
|
func sortMounts(m []container.Mount) []container.Mount {
|
2015-05-19 16:05:25 -04:00
|
|
|
sort.Sort(mounts(m))
|
|
|
|
return m
|
|
|
|
}
|
2015-04-29 18:53:35 -04:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
// setBindModeIfNull is platform specific processing to ensure the
|
|
|
|
// shared mode is set to 'z' if it is null. This is called in the case
|
|
|
|
// of processing a named volume and not a typical bind.
|
2018-04-17 16:50:28 -04:00
|
|
|
func setBindModeIfNull(bind *volumemounts.MountPoint) {
|
2015-09-09 22:23:06 -04:00
|
|
|
if bind.Mode == "" {
|
|
|
|
bind.Mode = "z"
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-06 09:49:10 -04:00
|
|
|
|
2016-06-07 03:45:21 -04:00
|
|
|
func (daemon *Daemon) mountVolumes(container *container.Container) error {
|
|
|
|
mounts, err := daemon.setupMounts(container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mounts {
|
|
|
|
dest, err := container.GetResourcePath(m.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stat os.FileInfo
|
|
|
|
stat, err = os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := "rbind,ro"
|
|
|
|
if m.Writable {
|
|
|
|
opts = "rbind,rw"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.Mount(m.Source, dest, bindMountType, opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// mountVolumes() seems to be called for temporary mounts
|
|
|
|
// outside the container. Soon these will be unmounted with
|
|
|
|
// lazy unmount option and given we have mounted the rbind,
|
|
|
|
// all the submounts will propagate if these are shared. If
|
|
|
|
// daemon is running in host namespace and has / as shared
|
|
|
|
// then these unmounts will propagate and unmount original
|
|
|
|
// mount as well. So make all these mounts rprivate.
|
|
|
|
// Do not use propagation property of volume as that should
|
|
|
|
// apply only when mounting happen inside the container.
|
|
|
|
if err := mount.MakeRPrivate(dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|