From ab8e2e8aee6e5df68f497e9dc0b4d2417c3e1bf5 Mon Sep 17 00:00:00 2001 From: Dan Walsh Date: Thu, 26 May 2016 14:39:46 -0400 Subject: [PATCH] Need to create bind mount volume if it does not exist. In order to be consistent on creation of volumes for bind mounts we need to create the source directory if it does not exist and the user specified he wants it relabeled. Can not do this lower down the stack, since we are not passing in the mode fields. Signed-off-by: Dan Walsh Upstream-commit: 322cc99c6962ecb56be3107061eb7f61364d05f8 Component: engine --- components/engine/daemon/volumes.go | 6 ------ components/engine/daemon/volumes_unix.go | 2 +- components/engine/volume/volume.go | 17 +++++++++++------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/components/engine/daemon/volumes.go b/components/engine/daemon/volumes.go index d3fb2c9645..48359b614e 100644 --- a/components/engine/daemon/volumes.go +++ b/components/engine/daemon/volumes.go @@ -11,7 +11,6 @@ import ( "github.com/docker/docker/volume" "github.com/docker/engine-api/types" containertypes "github.com/docker/engine-api/types/container" - "github.com/opencontainers/runc/libcontainer/label" ) var ( @@ -148,11 +147,6 @@ func (daemon *Daemon) registerMountPoints(container *container.Container, hostCo } } - if label.RelabelNeeded(bind.Mode) { - if err := label.Relabel(bind.Source, container.MountLabel, label.IsShared(bind.Mode)); err != nil { - return err - } - } binds[bind.Destination] = true mountPoints[bind.Destination] = bind } diff --git a/components/engine/daemon/volumes_unix.go b/components/engine/daemon/volumes_unix.go index 078fd10bf0..5b2cd4bbfa 100644 --- a/components/engine/daemon/volumes_unix.go +++ b/components/engine/daemon/volumes_unix.go @@ -20,7 +20,7 @@ func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, er if err := daemon.lazyInitializeVolume(c.ID, m); err != nil { return nil, err } - path, err := m.Setup() + path, err := m.Setup(c.MountLabel) if err != nil { return nil, err } diff --git a/components/engine/volume/volume.go b/components/engine/volume/volume.go index daa6a8cbd3..8999e11484 100644 --- a/components/engine/volume/volume.go +++ b/components/engine/volume/volume.go @@ -4,9 +4,11 @@ import ( "fmt" "os" "strings" + "syscall" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/system" + "github.com/opencontainers/runc/libcontainer/label" ) // DefaultDriverName is the driver name used for the driver @@ -73,7 +75,7 @@ type MountPoint struct { // Setup sets up a mount point by either mounting the volume if it is // configured, or creating the source directory if supplied. -func (m *MountPoint) Setup() (string, error) { +func (m *MountPoint) Setup(mountLabel string) (string, error) { if m.Volume != nil { if m.ID == "" { m.ID = stringid.GenerateNonCryptoID() @@ -84,12 +86,15 @@ func (m *MountPoint) Setup() (string, error) { return "", fmt.Errorf("Unable to setup mount point, neither source nor volume defined") } // system.MkdirAll() produces an error if m.Source exists and is a file (not a directory), - // so first check if the path does not exist - if _, err := os.Stat(m.Source); err != nil { - if !os.IsNotExist(err) { - return "", err + if err := system.MkdirAll(m.Source, 0755); err != nil { + if perr, ok := err.(*os.PathError); ok { + if perr.Err != syscall.ENOTDIR { + return "", err + } } - if err := system.MkdirAll(m.Source, 0755); err != nil { + } + if label.RelabelNeeded(m.Mode) { + if err := label.Relabel(m.Source, mountLabel, label.IsShared(m.Mode)); err != nil { return "", err } }