Browse code

Transfer uid and gid to volume. Fixes #1737.

Brian Olsen authored on 2013/08/30 09:22:08
Showing 2 changed files
... ...
@@ -707,6 +707,19 @@ func (container *Container) Start(hostConfig *HostConfig) error {
707 707
 					}
708 708
 				}
709 709
 			}
710
+			var stat syscall.Stat_t
711
+			if err := syscall.Stat(rootVolPath, &stat); err != nil {
712
+				return err
713
+			}
714
+			var srcStat syscall.Stat_t
715
+			if err := syscall.Stat(srcPath, &srcStat); err != nil {
716
+				return err
717
+			}
718
+			if stat.Uid != srcStat.Uid || stat.Gid != srcStat.Gid {
719
+				if err := os.Chown(srcPath, int(stat.Uid), int(stat.Gid)); err != nil {
720
+					return err
721
+				}
722
+			}
710 723
 		}
711 724
 	}
712 725
 
... ...
@@ -1196,6 +1196,51 @@ func tempDir(t *testing.T) string {
1196 1196
 	return tmpDir
1197 1197
 }
1198 1198
 
1199
+// Test for #1737
1200
+func TestCopyVolumeUidGid(t *testing.T) {
1201
+	r := mkRuntime(t)
1202
+	defer nuke(r)
1203
+
1204
+	// Add directory not owned by root
1205
+	container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && chown daemon.daemon /hello"}, t)
1206
+	defer r.Destroy(container1)
1207
+
1208
+	if container1.State.Running {
1209
+		t.Errorf("Container shouldn't be running")
1210
+	}
1211
+	if err := container1.Run(); err != nil {
1212
+		t.Fatal(err)
1213
+	}
1214
+	if container1.State.Running {
1215
+		t.Errorf("Container shouldn't be running")
1216
+	}
1217
+
1218
+	rwTar, err := container1.ExportRw()
1219
+	if err != nil {
1220
+		t.Error(err)
1221
+	}
1222
+	img, err := r.graph.Create(rwTar, container1, "unit test commited image", "", nil)
1223
+	if err != nil {
1224
+		t.Error(err)
1225
+	}
1226
+
1227
+	// Test that the uid and gid is copied from the image to the volume
1228
+	tmpDir1 := tempDir(t)
1229
+	defer os.RemoveAll(tmpDir1)
1230
+	stdout1, _ := runContainer(r, []string{"-v", fmt.Sprintf("%s:/hello", tmpDir1), img.ID, "stat", "-c", "%U %G", "/hello"}, t)
1231
+	if !strings.Contains(stdout1, "daemon daemon") {
1232
+		t.Fatal("Container failed to transfer uid and gid to volume")
1233
+	}
1234
+
1235
+	// Test that the uid and gid is not copied from the image when the volume is read only
1236
+	tmpDir2 := tempDir(t)
1237
+	defer os.RemoveAll(tmpDir1)
1238
+	stdout2, _ := runContainer(r, []string{"-v", fmt.Sprintf("%s:/hello:ro", tmpDir2), img.ID, "stat", "-c", "%U %G", "/hello"}, t)
1239
+	if strings.Contains(stdout2, "daemon daemon") {
1240
+		t.Fatal("Container transfered uid and gid to volume")
1241
+	}
1242
+}
1243
+
1199 1244
 // Test for #1582
1200 1245
 func TestCopyVolumeContent(t *testing.T) {
1201 1246
 	r := mkRuntime(t)