2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2015-04-27 16:25:38 +00:00
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-04-27 16:25:38 +00: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 07:44:01 +00:00
|
|
|
"fmt"
|
2015-04-27 16:25:38 +00:00
|
|
|
"os"
|
2015-05-19 20:05:25 +00:00
|
|
|
"sort"
|
2015-12-22 00:45:31 +00:00
|
|
|
"strconv"
|
2016-09-06 13:49:10 +00:00
|
|
|
"strings"
|
2015-04-27 16:25:38 +00:00
|
|
|
|
2023-08-26 13:24:46 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2018-10-10 10:20:13 +00:00
|
|
|
mounttypes "github.com/docker/docker/api/types/mount"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2018-04-17 20:50:28 +00:00
|
|
|
volumemounts "github.com/docker/docker/volume/mounts"
|
2023-04-05 11:32:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-04-27 16:25:38 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 23:10:56 +00: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 18:50:19 +00:00
|
|
|
func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, error) {
|
|
|
|
var mounts []container.Mount
|
2016-06-06 09:57:11 +00:00
|
|
|
// TODO: tmpfs mounts should be part of Mountpoints
|
|
|
|
tmpfsMounts := make(map[string]bool)
|
2016-09-22 20:14:15 +00:00
|
|
|
tmpfsMountInfo, err := c.TmpfsMounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, m := range tmpfsMountInfo {
|
2016-06-06 09:57:11 +00:00
|
|
|
tmpfsMounts[m.Destination] = true
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
for _, m := range c.MountPoints {
|
2016-06-06 09:57:11 +00:00
|
|
|
if tmpfsMounts[m.Destination] {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
|
2016-01-12 22:18:57 +00:00
|
|
|
return nil, err
|
2015-12-09 19:39:31 +00: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 07:44:01 +00: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 20:50:28 +00: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 07:44:01 +00: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-11-16 06:20:33 +00:00
|
|
|
path, err := m.Setup(c.MountLabel, daemon.idMapping.RootPair(), checkfunc)
|
2015-05-19 20:05:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
if !c.TrySetNetworkMount(m.Destination, path) {
|
|
|
|
mnt := container.Mount{
|
2015-07-14 00:54:00 +00:00
|
|
|
Source: path,
|
|
|
|
Destination: m.Destination,
|
|
|
|
Writable: m.RW,
|
2016-08-15 16:13:18 +00:00
|
|
|
Propagation: string(m.Propagation),
|
2015-10-23 20:57:57 +00:00
|
|
|
}
|
2018-10-10 10:20:13 +00:00
|
|
|
if m.Spec.Type == mounttypes.TypeBind && m.Spec.BindOptions != nil {
|
2023-04-05 11:32:03 +00:00
|
|
|
if !m.Spec.ReadOnly && m.Spec.BindOptions.ReadOnlyNonRecursive {
|
|
|
|
return nil, errors.New("mount options conflict: !ReadOnly && BindOptions.ReadOnlyNonRecursive")
|
|
|
|
}
|
|
|
|
if !m.Spec.ReadOnly && m.Spec.BindOptions.ReadOnlyForceRecursive {
|
|
|
|
return nil, errors.New("mount options conflict: !ReadOnly && BindOptions.ReadOnlyForceRecursive")
|
|
|
|
}
|
|
|
|
if m.Spec.BindOptions.ReadOnlyNonRecursive && m.Spec.BindOptions.ReadOnlyForceRecursive {
|
|
|
|
return nil, errors.New("mount options conflict: ReadOnlyNonRecursive && BindOptions.ReadOnlyForceRecursive")
|
|
|
|
}
|
2018-10-10 10:20:13 +00:00
|
|
|
mnt.NonRecursive = m.Spec.BindOptions.NonRecursive
|
2023-04-05 11:32:03 +00:00
|
|
|
mnt.ReadOnlyNonRecursive = m.Spec.BindOptions.ReadOnlyNonRecursive
|
|
|
|
mnt.ReadOnlyForceRecursive = m.Spec.BindOptions.ReadOnlyForceRecursive
|
2018-10-10 10:20:13 +00:00
|
|
|
}
|
2015-12-22 00:45:31 +00:00
|
|
|
if m.Volume != nil {
|
2023-08-26 13:24:46 +00:00
|
|
|
daemon.LogVolumeEvent(m.Volume.Name(), events.ActionMount, map[string]string{
|
2015-12-22 00:45:31 +00:00
|
|
|
"driver": m.Volume.DriverName(),
|
2016-03-18 18:50:19 +00:00
|
|
|
"container": c.ID,
|
2015-12-22 00:45:31 +00:00
|
|
|
"destination": m.Destination,
|
|
|
|
"read/write": strconv.FormatBool(m.RW),
|
2016-08-15 16:13:18 +00:00
|
|
|
"propagation": string(m.Propagation),
|
2023-08-26 16:25:27 +00:00
|
|
|
})
|
2015-12-22 00:45:31 +00:00
|
|
|
}
|
2015-10-23 20:57:57 +00:00
|
|
|
mounts = append(mounts, mnt)
|
2015-07-14 00:54:00 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
2015-05-19 20:05:25 +00:00
|
|
|
|
|
|
|
mounts = sortMounts(mounts)
|
2016-03-18 18:50:19 +00:00
|
|
|
netMounts := c.NetworkMounts()
|
2015-10-08 15:51:41 +00: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-11-16 06:20:33 +00:00
|
|
|
rootIDs := daemon.idMapping.RootPair()
|
2019-08-09 12:10:07 +00:00
|
|
|
for _, mnt := range netMounts {
|
2017-07-23 02:11:09 +00: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
|
2019-08-09 12:10:07 +00:00
|
|
|
if strings.Index(mnt.Source, daemon.repository) == 0 {
|
|
|
|
if err := os.Chown(mnt.Source, rootIDs.UID, rootIDs.GID); err != nil {
|
2017-07-23 02:11:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-08 15:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(mounts, netMounts...), nil
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 23:10:56 +00: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 18:50:19 +00:00
|
|
|
func sortMounts(m []container.Mount) []container.Mount {
|
2015-05-19 20:05:25 +00:00
|
|
|
sort.Sort(mounts(m))
|
|
|
|
return m
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-09-10 02:23:06 +00: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 20:50:28 +00:00
|
|
|
func setBindModeIfNull(bind *volumemounts.MountPoint) {
|
2015-09-10 02:23:06 +00:00
|
|
|
if bind.Mode == "" {
|
|
|
|
bind.Mode = "z"
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|