Browse code

Merge pull request #13493 from jlhawn/volume_unmount_fix

Fix container unmount networkMounts

Alexander Morozov authored on 2015/05/28 00:44:11
Showing 3 changed files
... ...
@@ -959,21 +959,37 @@ func (container *Container) DisableLink(name string) {
959 959
 }
960 960
 
961 961
 func (container *Container) UnmountVolumes(forceSyscall bool) error {
962
-	for _, m := range container.MountPoints {
963
-		dest, err := container.GetResourcePath(m.Destination)
962
+	var volumeMounts []mountPoint
963
+
964
+	for _, mntPoint := range container.MountPoints {
965
+		dest, err := container.GetResourcePath(mntPoint.Destination)
966
+		if err != nil {
967
+			return err
968
+		}
969
+
970
+		volumeMounts = append(volumeMounts, mountPoint{Destination: dest, Volume: mntPoint.Volume})
971
+	}
972
+
973
+	for _, mnt := range container.networkMounts() {
974
+		dest, err := container.GetResourcePath(mnt.Destination)
964 975
 		if err != nil {
965 976
 			return err
966 977
 		}
967 978
 
979
+		volumeMounts = append(volumeMounts, mountPoint{Destination: dest})
980
+	}
981
+
982
+	for _, volumeMount := range volumeMounts {
968 983
 		if forceSyscall {
969
-			syscall.Unmount(dest, 0)
984
+			syscall.Unmount(volumeMount.Destination, 0)
970 985
 		}
971 986
 
972
-		if m.Volume != nil {
973
-			if err := m.Volume.Unmount(); err != nil {
987
+		if volumeMount.Volume != nil {
988
+			if err := volumeMount.Volume.Unmount(); err != nil {
974 989
 				return err
975 990
 			}
976 991
 		}
977 992
 	}
993
+
978 994
 	return nil
979 995
 }
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"path/filepath"
9 9
 	"strings"
10 10
 
11
-	"github.com/docker/docker/daemon/execdriver"
12 11
 	"github.com/docker/docker/pkg/chrootarchive"
13 12
 	"github.com/docker/docker/runconfig"
14 13
 	"github.com/docker/docker/volume"
... ...
@@ -115,20 +114,6 @@ func validMountMode(mode string) bool {
115 115
 	return validModes[mode]
116 116
 }
117 117
 
118
-func (container *Container) specialMounts() []execdriver.Mount {
119
-	var mounts []execdriver.Mount
120
-	if container.ResolvConfPath != "" {
121
-		mounts = append(mounts, execdriver.Mount{Source: container.ResolvConfPath, Destination: "/etc/resolv.conf", Writable: !container.hostConfig.ReadonlyRootfs, Private: true})
122
-	}
123
-	if container.HostnamePath != "" {
124
-		mounts = append(mounts, execdriver.Mount{Source: container.HostnamePath, Destination: "/etc/hostname", Writable: !container.hostConfig.ReadonlyRootfs, Private: true})
125
-	}
126
-	if container.HostsPath != "" {
127
-		mounts = append(mounts, execdriver.Mount{Source: container.HostsPath, Destination: "/etc/hosts", Writable: !container.hostConfig.ReadonlyRootfs, Private: true})
128
-	}
129
-	return mounts
130
-}
131
-
132 118
 func copyExistingContents(source, destination string) error {
133 119
 	volList, err := ioutil.ReadDir(source)
134 120
 	if err != nil {
... ...
@@ -595,3 +595,40 @@ func (s *DockerSuite) TestCpNameHasColon(c *check.C) {
595 595
 		c.Fatalf("Wrong content in copied file %q, should be %q", content, "lololol\n")
596 596
 	}
597 597
 }
598
+
599
+func (s *DockerSuite) TestCopyAndRestart(c *check.C) {
600
+	expectedMsg := "hello"
601
+	out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", expectedMsg).CombinedOutput()
602
+	if err != nil {
603
+		c.Fatal(string(out), err)
604
+	}
605
+	id := strings.TrimSpace(string(out))
606
+
607
+	if out, err = exec.Command(dockerBinary, "wait", id).CombinedOutput(); err != nil {
608
+		c.Fatalf("unable to wait for container: %s", err)
609
+	}
610
+
611
+	status := strings.TrimSpace(string(out))
612
+	if status != "0" {
613
+		c.Fatalf("container exited with status %s", status)
614
+	}
615
+
616
+	tmpDir, err := ioutil.TempDir("", "test-docker-restart-after-copy-")
617
+	if err != nil {
618
+		c.Fatalf("unable to make temporary directory: %s", err)
619
+	}
620
+	defer os.RemoveAll(tmpDir)
621
+
622
+	if _, err = exec.Command(dockerBinary, "cp", fmt.Sprintf("%s:/etc/issue", id), tmpDir).CombinedOutput(); err != nil {
623
+		c.Fatalf("unable to copy from busybox container: %s", err)
624
+	}
625
+
626
+	if out, err = exec.Command(dockerBinary, "start", "-a", id).CombinedOutput(); err != nil {
627
+		c.Fatalf("unable to start busybox container after copy: %s - %s", err, out)
628
+	}
629
+
630
+	msg := strings.TrimSpace(string(out))
631
+	if msg != expectedMsg {
632
+		c.Fatalf("expected %q but got %q", expectedMsg, msg)
633
+	}
634
+}