[CRIU] [PATCH 2/2] zdtm/mnt_ext_*: don't affect the host mount namespace

Andrey Vagin avagin at openvz.org
Tue May 24 14:29:38 PDT 2016


From: Andrew Vagin <avagin at virtuozzo.com>

Currently all test mounts are mounted in the host mount namespace,
so on restore we get root with some mounts.

Cc: Tycho Andersen <tycho.andersen at canonical.com>
Signed-off-by: Andrew Vagin <avagin at virtuozzo.com>
---
 test/zdtm/static/mnt_ext_auto.c   | 56 +++++++++++++++++++++------------------
 test/zdtm/static/mnt_ext_master.c | 49 ++++++++++++++--------------------
 2 files changed, 50 insertions(+), 55 deletions(-)

diff --git a/test/zdtm/static/mnt_ext_auto.c b/test/zdtm/static/mnt_ext_auto.c
index 4a931b9..42fb363 100644
--- a/test/zdtm/static/mnt_ext_auto.c
+++ b/test/zdtm/static/mnt_ext_auto.c
@@ -1,3 +1,5 @@
+#define _GNU_SOURCE
+#include <sched.h>
 #include <sys/mount.h>
 #include <unistd.h>
 #include <sys/stat.h>
@@ -9,8 +11,8 @@
 
 #include "zdtmtst.h"
 
-const char *test_doc	= "Run busy loop while migrating";
-const char *test_author	= "Roman Kagan <rkagan at parallels.com>";
+const char *test_doc	= "Check --mnt-ext-map auto";
+const char *test_author	= "Andrew Vagin <avagin at gmail.com>";
 
 char *dirname = "mnt_ext_auto.test";
 TEST_OPTION(dirname, string, "directory name", 1);
@@ -19,8 +21,7 @@ int main(int argc, char ** argv)
 {
 	char src[PATH_MAX], dst[PATH_MAX], *root;
 	char *dname = "/tmp/zdtm_ext_auto.XXXXXX";
-	int status;
-	pid_t pid;
+	struct stat sta, stb;
 
 	root = getenv("ZDTM_ROOT");
 	if (root == NULL) {
@@ -28,42 +29,45 @@ int main(int argc, char ** argv)
 		return 1;
 	}
 
-	sprintf(dst, "%s/ext_mounts", getenv("ZDTM_ROOT"));
+	sprintf(dst, "%s/%s", get_current_dir_name(), dirname);
 
 	if (strcmp(getenv("ZDTM_NEWNS"), "1"))
 		goto test;
 
-	pid = fork();
-	if (pid < 0)
+	mkdir(dname, 755);
+	sprintf(src, "%s/test", dname);
+	if (mount("zdtm_auto_ext_mnt", dname, "tmpfs", 0, NULL)) {
+		pr_perror("mount");
 		return 1;
-	if (pid == 0) {
-		test_ext_init(argc, argv);
-
-		mkdir(dname, 755);
-		sprintf(src, "%s/test", dname);
-		if (mount("zdtm_auto_ext_mnt", dname, "tmpfs", 0, NULL)) {
-			pr_perror("mount");
-			return 1;
-		}
-		mkdir(src, 755);
-		mkdir(dst, 755);
-		if (mount(src, dst, NULL, MS_BIND, NULL)) {
-			pr_perror("bind");
-			return 1;
-		}
-		return 0;
 	}
+	mkdir(src, 755);
 
-	wait(&status);
-	if (status != 0)
+	unshare(CLONE_NEWNS);
+	mkdir(dst, 755);
+	if (mount(src, dst, NULL, MS_BIND, NULL)) {
+		pr_perror("bind");
 		return 1;
-
+	}
 test:
 	test_init(argc, argv);
 
+	if (stat(dirname, &stb)) {
+		pr_perror("stat");
+		sleep(100);
+		return 1;
+	}
+
 	test_daemon();
 	test_waitsig();
 
+	if (stat(dirname, &sta)) {
+		pr_perror("stat");
+		return 1;
+	}
+	if (sta.st_dev != stb.st_dev) {
+		fail();
+		return 1;
+	}
 
 	pass();
 
diff --git a/test/zdtm/static/mnt_ext_master.c b/test/zdtm/static/mnt_ext_master.c
index 290868e..c0ef7ad 100644
--- a/test/zdtm/static/mnt_ext_master.c
+++ b/test/zdtm/static/mnt_ext_master.c
@@ -1,3 +1,6 @@
+#define _GNU_SOURCE
+#include <sched.h>
+
 #include <sys/mount.h>
 #include <unistd.h>
 #include <sys/stat.h>
@@ -19,8 +22,6 @@ int main(int argc, char ** argv)
 {
 	char src[PATH_MAX], dst[PATH_MAX], *root;
 	char *dname = "/tmp/zdtm_ext_auto.XXXXXX";
-	int status;
-	pid_t pid;
 
 	root = getenv("ZDTM_ROOT");
 	if (root == NULL) {
@@ -33,37 +34,27 @@ int main(int argc, char ** argv)
 	if (strcmp(getenv("ZDTM_NEWNS"), "1"))
 		goto test;
 
-	pid = fork();
-	if (pid < 0)
+	mkdir(dname, 755);
+	sprintf(src, "%s/test", dname);
+	if (mount("zdtm_auto_ext_mnt", dname, "tmpfs", 0, NULL)) {
+		pr_perror("mount");
 		return 1;
-	if (pid == 0) {
-		test_ext_init(argc, argv);
-
-		mkdir(dname, 755);
-		sprintf(src, "%s/test", dname);
-		if (mount("zdtm_auto_ext_mnt", dname, "tmpfs", 0, NULL)) {
-			pr_perror("mount");
-			return 1;
-		}
-
-		mkdir(src, 755);
-		mkdir(dst, 755);
-		if (mount(src, dst, NULL, MS_BIND, NULL)) {
-			pr_perror("bind");
-			return 1;
-		}
-
-		if (mount(src, dst, NULL, MS_SLAVE, NULL)) {
-			pr_perror("slave");
-			return 1;
-		}
-
-		return 0;
 	}
 
-	wait(&status);
-	if (status != 0)
+	mkdir(src, 755);
+	mkdir(dst, 755);
+
+	unshare(CLONE_NEWNS);
+
+	if (mount(src, dst, NULL, MS_BIND, NULL)) {
+		pr_perror("bind");
 		return 1;
+	}
+
+	if (mount(src, dst, NULL, MS_SLAVE, NULL)) {
+		pr_perror("slave");
+		return 1;
+	}
 
 test:
 	test_init(argc, argv);
-- 
2.7.4



More information about the CRIU mailing list