Browse code

Move remount as private to the graph drivers

If this is at the root directory for the daemon you could unmount
somones filesystem when you stop docker and this is actually only needed
for the palces that the graph drivers mount the container's root
filesystems.
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/06/06 04:50:53
Showing 5 changed files
... ...
@@ -27,7 +27,6 @@ import (
27 27
 	"github.com/dotcloud/docker/image"
28 28
 	"github.com/dotcloud/docker/pkg/graphdb"
29 29
 	"github.com/dotcloud/docker/pkg/label"
30
-	"github.com/dotcloud/docker/pkg/mount"
31 30
 	"github.com/dotcloud/docker/pkg/namesgenerator"
32 31
 	"github.com/dotcloud/docker/pkg/networkfs/resolvconf"
33 32
 	"github.com/dotcloud/docker/pkg/selinux"
... ...
@@ -102,21 +101,6 @@ func (daemon *Daemon) Install(eng *engine.Engine) error {
102 102
 	return eng.Register("container_inspect", daemon.ContainerInspect)
103 103
 }
104 104
 
105
-// Mountpoints should be private to the container
106
-func remountPrivate(mountPoint string) error {
107
-	mounted, err := mount.Mounted(mountPoint)
108
-	if err != nil {
109
-		return err
110
-	}
111
-
112
-	if !mounted {
113
-		if err := mount.Mount(mountPoint, mountPoint, "none", "bind,rw"); err != nil {
114
-			return err
115
-		}
116
-	}
117
-	return mount.ForceMount("", mountPoint, "none", "private")
118
-}
119
-
120 105
 // List returns an array of all containers registered in the daemon.
121 106
 func (daemon *Daemon) List() []*Container {
122 107
 	return daemon.containers.List()
... ...
@@ -786,10 +770,6 @@ func NewDaemonFromDirectory(config *daemonconfig.Config, eng *engine.Engine) (*D
786 786
 	}
787 787
 	utils.Debugf("Using graph driver %s", driver)
788 788
 
789
-	if err := remountPrivate(config.Root); err != nil {
790
-		return nil, err
791
-	}
792
-
793 789
 	daemonRepo := path.Join(config.Root, "containers")
794 790
 
795 791
 	if err := os.MkdirAll(daemonRepo, 0700); err != nil && !os.IsExist(err) {
... ...
@@ -938,10 +918,6 @@ func (daemon *Daemon) Close() error {
938 938
 		utils.Errorf("daemon.containerGraph.Close(): %s", err.Error())
939 939
 		errorsStrings = append(errorsStrings, err.Error())
940 940
 	}
941
-	if err := mount.Unmount(daemon.config.Root); err != nil {
942
-		utils.Errorf("daemon.Umount(%s): %s", daemon.config.Root, err.Error())
943
-		errorsStrings = append(errorsStrings, err.Error())
944
-	}
945 941
 	if len(errorsStrings) > 0 {
946 942
 		return fmt.Errorf("%s", strings.Join(errorsStrings, ", "))
947 943
 	}
... ...
@@ -97,6 +97,10 @@ func Init(root string, options []string) (graphdriver.Driver, error) {
97 97
 		return nil, err
98 98
 	}
99 99
 
100
+	if err := graphdriver.MakePrivate(root); err != nil {
101
+		return nil, err
102
+	}
103
+
100 104
 	for _, p := range paths {
101 105
 		if err := os.MkdirAll(path.Join(root, p), 0755); err != nil {
102 106
 			return nil, err
... ...
@@ -371,12 +375,14 @@ func (a *Driver) Cleanup() error {
371 371
 	if err != nil {
372 372
 		return err
373 373
 	}
374
+
374 375
 	for _, id := range ids {
375 376
 		if err := a.unmount(id); err != nil {
376 377
 			utils.Errorf("Unmounting %s: %s", utils.TruncateID(id), err)
377 378
 		}
378 379
 	}
379
-	return nil
380
+
381
+	return mountpk.Unmount(a.root)
380 382
 }
381 383
 
382 384
 func (a *Driver) aufsMount(ro []string, rw, target, mountLabel string) (err error) {
... ...
@@ -11,11 +11,13 @@ import "C"
11 11
 
12 12
 import (
13 13
 	"fmt"
14
-	"github.com/dotcloud/docker/daemon/graphdriver"
15 14
 	"os"
16 15
 	"path"
17 16
 	"syscall"
18 17
 	"unsafe"
18
+
19
+	"github.com/dotcloud/docker/daemon/graphdriver"
20
+	"github.com/dotcloud/docker/pkg/mount"
19 21
 )
20 22
 
21 23
 func init() {
... ...
@@ -34,6 +36,14 @@ func Init(home string, options []string) (graphdriver.Driver, error) {
34 34
 		return nil, graphdriver.ErrPrerequisites
35 35
 	}
36 36
 
37
+	if err := os.MkdirAll(home, 0700); err != nil {
38
+		return nil, err
39
+	}
40
+
41
+	if err := graphdriver.MakePrivate(home); err != nil {
42
+		return nil, err
43
+	}
44
+
37 45
 	return &Driver{
38 46
 		home: home,
39 47
 	}, nil
... ...
@@ -52,7 +62,7 @@ func (d *Driver) Status() [][2]string {
52 52
 }
53 53
 
54 54
 func (d *Driver) Cleanup() error {
55
-	return nil
55
+	return mount.Unmount(d.home)
56 56
 }
57 57
 
58 58
 func free(p *C.char) {
... ...
@@ -9,6 +9,7 @@ import (
9 9
 	"path"
10 10
 
11 11
 	"github.com/dotcloud/docker/daemon/graphdriver"
12
+	"github.com/dotcloud/docker/pkg/mount"
12 13
 	"github.com/dotcloud/docker/utils"
13 14
 )
14 15
 
... ...
@@ -31,10 +32,16 @@ func Init(home string, options []string) (graphdriver.Driver, error) {
31 31
 	if err != nil {
32 32
 		return nil, err
33 33
 	}
34
+
35
+	if err := graphdriver.MakePrivate(home); err != nil {
36
+		return nil, err
37
+	}
38
+
34 39
 	d := &Driver{
35 40
 		DeviceSet: deviceSet,
36 41
 		home:      home,
37 42
 	}
43
+
38 44
 	return d, nil
39 45
 }
40 46
 
... ...
@@ -58,7 +65,13 @@ func (d *Driver) Status() [][2]string {
58 58
 }
59 59
 
60 60
 func (d *Driver) Cleanup() error {
61
-	return d.DeviceSet.Shutdown()
61
+	err := d.DeviceSet.Shutdown()
62
+
63
+	if err2 := mount.Unmount(d.home); err == nil {
64
+		err = err2
65
+	}
66
+
67
+	return err
62 68
 }
63 69
 
64 70
 func (d *Driver) Create(id, parent string) error {
... ...
@@ -3,9 +3,11 @@ package graphdriver
3 3
 import (
4 4
 	"errors"
5 5
 	"fmt"
6
-	"github.com/dotcloud/docker/archive"
7 6
 	"os"
8 7
 	"path"
8
+
9
+	"github.com/dotcloud/docker/archive"
10
+	"github.com/dotcloud/docker/pkg/mount"
9 11
 )
10 12
 
11 13
 type FsMagic uint64
... ...
@@ -107,3 +109,18 @@ func New(root string, options []string) (driver Driver, err error) {
107 107
 	}
108 108
 	return nil, fmt.Errorf("No supported storage backend found")
109 109
 }
110
+
111
+func MakePrivate(mountPoint string) error {
112
+	mounted, err := mount.Mounted(mountPoint)
113
+	if err != nil {
114
+		return err
115
+	}
116
+
117
+	if !mounted {
118
+		if err := mount.Mount(mountPoint, mountPoint, "none", "bind,rw"); err != nil {
119
+			return err
120
+		}
121
+	}
122
+
123
+	return mount.ForceMount("", mountPoint, "none", "private")
124
+}