Browse code

daemon.cleanupMounts(): use mount.SingleEntryFilter

Use mount.SingleEntryFilter as we're only interested in a single entry.

Test case data of TestShouldUnmountRoot is modified accordingly, as
from now on:

1. `info` can't be nil;

2. the mountpoint check is not performed (as SingleEntryFilter
guarantees it to be equal to daemon.root).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

Kir Kolyshkin authored on 2018/03/01 14:55:57
Showing 2 changed files
... ...
@@ -74,18 +74,22 @@ func (daemon *Daemon) cleanupMounts() error {
74 74
 		return err
75 75
 	}
76 76
 
77
-	infos, err := mount.GetMounts(nil)
77
+	info, err := mount.GetMounts(mount.SingleEntryFilter(daemon.root))
78 78
 	if err != nil {
79 79
 		return errors.Wrap(err, "error reading mount table for cleanup")
80 80
 	}
81 81
 
82
-	info := getMountInfo(infos, daemon.root)
82
+	if len(info) < 1 {
83
+		// no mount found, we're done here
84
+		return nil
85
+	}
86
+
83 87
 	// `info.Root` here is the root mountpoint of the passed in path (`daemon.root`).
84 88
 	// The ony cases that need to be cleaned up is when the daemon has performed a
85 89
 	//   `mount --bind /daemon/root /daemon/root && mount --make-shared /daemon/root`
86 90
 	// This is only done when the daemon is started up and `/daemon/root` is not
87 91
 	// already on a shared mountpoint.
88
-	if !shouldUnmountRoot(daemon.root, info) {
92
+	if !shouldUnmountRoot(daemon.root, info[0]) {
89 93
 		return nil
90 94
 	}
91 95
 
... ...
@@ -114,12 +118,6 @@ func getRealPath(path string) (string, error) {
114 114
 }
115 115
 
116 116
 func shouldUnmountRoot(root string, info *mount.Info) bool {
117
-	if info == nil {
118
-		return false
119
-	}
120
-	if info.Mountpoint != root {
121
-		return false
122
-	}
123 117
 	if !strings.HasSuffix(root, info.Root) {
124 118
 		return false
125 119
 	}
... ...
@@ -180,12 +180,6 @@ func TestShouldUnmountRoot(t *testing.T) {
180 180
 			expect: true,
181 181
 		},
182 182
 		{
183
-			desc:   "not a mountpoint",
184
-			root:   "/docker",
185
-			info:   nil,
186
-			expect: false,
187
-		},
188
-		{
189 183
 			desc:   "root is at in a submount from `/`",
190 184
 			root:   "/foo/docker",
191 185
 			info:   &mount.Info{Root: "/docker", Mountpoint: "/foo/docker"},
... ...
@@ -197,12 +191,6 @@ func TestShouldUnmountRoot(t *testing.T) {
197 197
 			info:   &mount.Info{Root: "/docker/volumes/1234657/_data", Mountpoint: "/docker"},
198 198
 			expect: false,
199 199
 		},
200
-		{
201
-			desc:   "root is mounted in from a parent mount namespace different root dir",
202
-			root:   "/foo/bar",
203
-			info:   &mount.Info{Root: "/docker/volumes/1234657/_data", Mountpoint: "/foo/bar"},
204
-			expect: false,
205
-		},
206 200
 	} {
207 201
 		t.Run(test.desc, func(t *testing.T) {
208 202
 			for _, options := range []struct {