Browse code

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 <dwalsh@redhat.com>

Dan Walsh authored on 2016/05/27 03:39:46
Showing 3 changed files
... ...
@@ -11,7 +11,6 @@ import (
11 11
 	"github.com/docker/docker/volume"
12 12
 	"github.com/docker/engine-api/types"
13 13
 	containertypes "github.com/docker/engine-api/types/container"
14
-	"github.com/opencontainers/runc/libcontainer/label"
15 14
 )
16 15
 
17 16
 var (
... ...
@@ -148,11 +147,6 @@ func (daemon *Daemon) registerMountPoints(container *container.Container, hostCo
148 148
 			}
149 149
 		}
150 150
 
151
-		if label.RelabelNeeded(bind.Mode) {
152
-			if err := label.Relabel(bind.Source, container.MountLabel, label.IsShared(bind.Mode)); err != nil {
153
-				return err
154
-			}
155
-		}
156 151
 		binds[bind.Destination] = true
157 152
 		mountPoints[bind.Destination] = bind
158 153
 	}
... ...
@@ -20,7 +20,7 @@ func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, er
20 20
 		if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
21 21
 			return nil, err
22 22
 		}
23
-		path, err := m.Setup()
23
+		path, err := m.Setup(c.MountLabel)
24 24
 		if err != nil {
25 25
 			return nil, err
26 26
 		}
... ...
@@ -4,9 +4,11 @@ import (
4 4
 	"fmt"
5 5
 	"os"
6 6
 	"strings"
7
+	"syscall"
7 8
 
8 9
 	"github.com/docker/docker/pkg/stringid"
9 10
 	"github.com/docker/docker/pkg/system"
11
+	"github.com/opencontainers/runc/libcontainer/label"
10 12
 )
11 13
 
12 14
 // DefaultDriverName is the driver name used for the driver
... ...
@@ -73,7 +75,7 @@ type MountPoint struct {
73 73
 
74 74
 // Setup sets up a mount point by either mounting the volume if it is
75 75
 // configured, or creating the source directory if supplied.
76
-func (m *MountPoint) Setup() (string, error) {
76
+func (m *MountPoint) Setup(mountLabel string) (string, error) {
77 77
 	if m.Volume != nil {
78 78
 		if m.ID == "" {
79 79
 			m.ID = stringid.GenerateNonCryptoID()
... ...
@@ -84,12 +86,15 @@ func (m *MountPoint) Setup() (string, error) {
84 84
 		return "", fmt.Errorf("Unable to setup mount point, neither source nor volume defined")
85 85
 	}
86 86
 	// system.MkdirAll() produces an error if m.Source exists and is a file (not a directory),
87
-	// so first check if the path does not exist
88
-	if _, err := os.Stat(m.Source); err != nil {
89
-		if !os.IsNotExist(err) {
90
-			return "", err
87
+	if err := system.MkdirAll(m.Source, 0755); err != nil {
88
+		if perr, ok := err.(*os.PathError); ok {
89
+			if perr.Err != syscall.ENOTDIR {
90
+				return "", err
91
+			}
91 92
 		}
92
-		if err := system.MkdirAll(m.Source, 0755); err != nil {
93
+	}
94
+	if label.RelabelNeeded(m.Mode) {
95
+		if err := label.Relabel(m.Source, mountLabel, label.IsShared(m.Mode)); err != nil {
93 96
 			return "", err
94 97
 		}
95 98
 	}