1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/daemon/volumes_linux_test.go
Brian Goff 589a0afa8c Use rslave propagation for mounts from daemon root
By default, if a user requests a bind mount it uses private propagation.
When the source path is a path within the daemon root this, along with
some other propagation values that the user can use, causes issues when
the daemon tries to remove a mountpoint because a container will then
have a private reference to that mount which prevents removal.

Unmouting with MNT_DETATCH can help this scenario on newer kernels, but
ultimately this is just covering up the problem and doesn't actually
free up the underlying resources until all references are destroyed.

This change does essentially 2 things:

1. Change the default propagation when unspecified to `rslave` when the
source path is within the daemon root path or a parent of the daemon
root (because everything is using rbinds).
2. Creates a validation error on create when the user tries to specify
an unacceptable propagation mode for these paths...
basically the only two acceptable modes are `rslave` and `rshared`.

In cases where we have used the new default propagation but the
underlying filesystem is not setup to handle it (fs must hvae at least
rshared propagation) instead of erroring out like we normally would,
this falls back to the old default mode of `private`, which preserves
backwards compatibility.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2018-02-07 14:27:09 -05:00

56 lines
1.9 KiB
Go

package daemon
import (
"path/filepath"
"testing"
"github.com/docker/docker/api/types/mount"
)
func TestBindDaemonRoot(t *testing.T) {
t.Parallel()
d := &Daemon{root: "/a/b/c/daemon"}
for _, test := range []struct {
desc string
opts *mount.BindOptions
needsProp bool
err bool
}{
{desc: "nil propagation settings", opts: nil, needsProp: true, err: false},
{desc: "empty propagation settings", opts: &mount.BindOptions{}, needsProp: true, err: false},
{desc: "private propagation", opts: &mount.BindOptions{Propagation: mount.PropagationPrivate}, err: true},
{desc: "rprivate propagation", opts: &mount.BindOptions{Propagation: mount.PropagationRPrivate}, err: true},
{desc: "slave propagation", opts: &mount.BindOptions{Propagation: mount.PropagationSlave}, err: true},
{desc: "rslave propagation", opts: &mount.BindOptions{Propagation: mount.PropagationRSlave}, err: false, needsProp: false},
{desc: "shared propagation", opts: &mount.BindOptions{Propagation: mount.PropagationShared}, err: true},
{desc: "rshared propagation", opts: &mount.BindOptions{Propagation: mount.PropagationRSlave}, err: false, needsProp: false},
} {
t.Run(test.desc, func(t *testing.T) {
test := test
for desc, source := range map[string]string{
"source is root": d.root,
"source is subpath": filepath.Join(d.root, "a", "b"),
"source is parent": filepath.Dir(d.root),
"source is /": "/",
} {
t.Run(desc, func(t *testing.T) {
mount := mount.Mount{
Type: mount.TypeBind,
Source: source,
BindOptions: test.opts,
}
needsProp, err := d.validateBindDaemonRoot(mount)
if (err != nil) != test.err {
t.Fatalf("expected err=%v, got: %v", test.err, err)
}
if test.err {
return
}
if test.needsProp != needsProp {
t.Fatalf("expected needsProp=%v, got: %v", test.needsProp, needsProp)
}
})
}
})
}
}