Browse code

Fix container mount cleanup issues

- Refactor generic and path based cleanup functions into a single function.
- Include aufs and zfs mounts in the mounts cleanup.
- Containers that receive exit event on restore don't require manual cleanup.
- Make missing sandbox id message a warning because currently sandboxes are always cleared on startup. libnetwork#975
- Don't unmount volumes for containers that don't have base path. Shouldn't be needed after #21372

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>

Tonis Tiigi authored on 2016/03/30 07:27:04
Showing 6 changed files
... ...
@@ -720,7 +720,7 @@ func (daemon *Daemon) releaseNetwork(container *container.Container) {
720 720
 
721 721
 	sb, err := daemon.netController.SandboxByID(sid)
722 722
 	if err != nil {
723
-		logrus.Errorf("error locating sandbox id %s: %v", sid, err)
723
+		logrus.Warnf("error locating sandbox id %s: %v", sid, err)
724 724
 		return
725 725
 	}
726 726
 
... ...
@@ -304,10 +304,6 @@ func (daemon *Daemon) restore() error {
304 304
 				mapLock.Lock()
305 305
 				restartContainers[c] = make(chan struct{})
306 306
 				mapLock.Unlock()
307
-			} else if !c.IsRunning() && !c.IsPaused() {
308
-				if mountid, err := daemon.layerStore.GetMountID(c.ID); err == nil {
309
-					daemon.cleanupMountsByID(mountid)
310
-				}
311 307
 			}
312 308
 
313 309
 			// if c.hostConfig.Links is nil (not just empty), then it is using the old sqlite links and needs to be migrated
... ...
@@ -5,7 +5,7 @@ import (
5 5
 	"fmt"
6 6
 	"io"
7 7
 	"os"
8
-	"path/filepath"
8
+	"regexp"
9 9
 	"strings"
10 10
 
11 11
 	"github.com/Sirupsen/logrus"
... ...
@@ -28,91 +28,53 @@ func (daemon *Daemon) cleanupMountsFromReaderByID(reader io.Reader, id string, u
28 28
 		return nil
29 29
 	}
30 30
 	var errors []string
31
-	mountRoot := ""
32
-	shmSuffix := "/" + id + "/shm"
33
-	mergedSuffix := "/" + id + "/merged"
31
+
32
+	regexps := getCleanPatterns(id)
34 33
 	sc := bufio.NewScanner(reader)
35 34
 	for sc.Scan() {
36
-		line := sc.Text()
37
-		fields := strings.Fields(line)
38
-		if strings.HasPrefix(fields[4], daemon.root) {
39
-			logrus.Debugf("Mount base: %v", fields[4])
40
-			mnt := fields[4]
41
-			if strings.HasSuffix(mnt, shmSuffix) || strings.HasSuffix(mnt, mergedSuffix) {
42
-				logrus.Debugf("Unmounting %v", mnt)
43
-				if err := unmount(mnt); err != nil {
44
-					logrus.Error(err)
45
-					errors = append(errors, err.Error())
35
+		if fields := strings.Fields(sc.Text()); len(fields) >= 4 {
36
+			if mnt := fields[4]; strings.HasPrefix(mnt, daemon.root) {
37
+				for _, p := range regexps {
38
+					if p.MatchString(mnt) {
39
+						if err := unmount(mnt); err != nil {
40
+							logrus.Error(err)
41
+							errors = append(errors, err.Error())
42
+						}
43
+					}
46 44
 				}
47
-			} else if mountBase := filepath.Base(mnt); mountBase == id {
48
-				mountRoot = mnt
49 45
 			}
50 46
 		}
51 47
 	}
52 48
 
53
-	if mountRoot != "" {
54
-		logrus.Debugf("Unmounting %v", mountRoot)
55
-		if err := unmount(mountRoot); err != nil {
56
-			logrus.Error(err)
57
-			errors = append(errors, err.Error())
58
-		}
59
-	}
60
-
61 49
 	if err := sc.Err(); err != nil {
62 50
 		return err
63 51
 	}
64 52
 
65 53
 	if len(errors) > 0 {
66
-		return fmt.Errorf("Error cleaningup mounts:\n%v", strings.Join(errors, "\n"))
54
+		return fmt.Errorf("Error cleaning up mounts:\n%v", strings.Join(errors, "\n"))
67 55
 	}
68 56
 
69
-	logrus.Debugf("Cleaning up old container shm/mqueue/rootfs mounts: done.")
57
+	logrus.Debugf("Cleaning up old mountid %v: done.", id)
70 58
 	return nil
71 59
 }
72 60
 
73 61
 // cleanupMounts umounts shm/mqueue mounts for old containers
74 62
 func (daemon *Daemon) cleanupMounts() error {
75
-	logrus.Debugf("Cleaning up old container shm/mqueue/rootfs mounts: start.")
76
-	f, err := os.Open("/proc/self/mountinfo")
77
-	if err != nil {
78
-		return err
79
-	}
80
-	defer f.Close()
81
-
82
-	return daemon.cleanupMountsFromReader(f, mount.Unmount)
63
+	return daemon.cleanupMountsByID("")
83 64
 }
84 65
 
85
-func (daemon *Daemon) cleanupMountsFromReader(reader io.Reader, unmount func(target string) error) error {
86
-	if daemon.root == "" {
87
-		return nil
66
+func getCleanPatterns(id string) (regexps []*regexp.Regexp) {
67
+	var patterns []string
68
+	if id == "" {
69
+		id = "[0-9a-f]{64}"
70
+		patterns = append(patterns, "containers/"+id+"/shm")
88 71
 	}
89
-	sc := bufio.NewScanner(reader)
90
-	var errors []string
91
-	for sc.Scan() {
92
-		line := sc.Text()
93
-		fields := strings.Fields(line)
94
-		if strings.HasPrefix(fields[4], daemon.root) {
95
-			logrus.Debugf("Mount base: %v", fields[4])
96
-			mnt := fields[4]
97
-			mountBase := filepath.Base(mnt)
98
-			if mountBase == "shm" || mountBase == "merged" {
99
-				logrus.Debugf("Unmounting %v", mnt)
100
-				if err := unmount(mnt); err != nil {
101
-					logrus.Error(err)
102
-					errors = append(errors, err.Error())
103
-				}
104
-			}
72
+	patterns = append(patterns, "aufs/mnt/"+id+"$", "overlay/"+id+"/merged$", "zfs/graph/"+id+"$")
73
+	for _, p := range patterns {
74
+		r, err := regexp.Compile(p)
75
+		if err == nil {
76
+			regexps = append(regexps, r)
105 77
 		}
106 78
 	}
107
-
108
-	if err := sc.Err(); err != nil {
109
-		return err
110
-	}
111
-
112
-	if len(errors) > 0 {
113
-		return fmt.Errorf("Error cleaningup mounts:\n%v", strings.Join(errors, "\n"))
114
-	}
115
-
116
-	logrus.Debugf("Cleaning up old container shm/mqueue/rootfs mounts: done.")
117
-	return nil
79
+	return
118 80
 }
... ...
@@ -59,7 +59,7 @@ func TestCleanupMounts(t *testing.T) {
59 59
 		return nil
60 60
 	}
61 61
 
62
-	d.cleanupMountsFromReader(strings.NewReader(mountsFixture), unmount)
62
+	d.cleanupMountsFromReaderByID(strings.NewReader(mountsFixture), "", unmount)
63 63
 
64 64
 	if unmounted != 1 {
65 65
 		t.Fatalf("Expected to unmount the shm (and the shm only)")
... ...
@@ -97,7 +97,7 @@ func TestNotCleanupMounts(t *testing.T) {
97 97
 		return nil
98 98
 	}
99 99
 	mountInfo := `234 232 0:59 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k`
100
-	d.cleanupMountsFromReader(strings.NewReader(mountInfo), unmount)
100
+	d.cleanupMountsFromReaderByID(strings.NewReader(mountInfo), "", unmount)
101 101
 	if unmounted {
102 102
 		t.Fatalf("Expected not to clean up /dev/shm")
103 103
 	}
... ...
@@ -174,8 +174,10 @@ func (daemon *Daemon) Cleanup(container *container.Container) {
174 174
 		daemon.unregisterExecCommand(container, eConfig)
175 175
 	}
176 176
 
177
-	if err := container.UnmountVolumes(false, daemon.LogVolumeEvent); err != nil {
178
-		logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
177
+	if container.BaseFS != "" {
178
+		if err := container.UnmountVolumes(false, daemon.LogVolumeEvent); err != nil {
179
+			logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
180
+		}
179 181
 	}
180 182
 	container.CancelAttachContext()
181 183
 }
... ...
@@ -31,8 +31,10 @@ func (clnt *client) Restore(containerID string, options ...CreateOption) error {
31 31
 			select {
32 32
 			case <-time.After(2 * time.Second):
33 33
 			case <-w.wait():
34
+				return nil
34 35
 			}
35 36
 		case <-w.wait():
37
+			return nil
36 38
 		}
37 39
 	}
38 40
 	return clnt.setExited(containerID)