[Devel] [PATCH criu 3/3] mount: don't create a temporary directory if /tmp exists

Andrey Vagin avagin at openvz.org
Mon Nov 21 17:07:20 PST 2016


From: Andrei Vagin <avagin at virtuozzo.com>

pivot_root requires a place where to move an old root. Currently
a temporary directory is created for that, but it doesn't
work if the / directory is read-only.

Actually we can use any existing directory. In this patch,
criu tries to use /tmp and only if it doesn't exist,
criu creates a temporary directory.

https://bugs.openvz.org/browse/OVZ-6778

v2: don't give a constant string to mkdtemp
Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
Signed-off-by: Pavel Emelyanov <xemul at virtuozzo.com>
---
 criu/mount.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/criu/mount.c b/criu/mount.c
index 3a98e63..c76542b 100644
--- a/criu/mount.c
+++ b/criu/mount.c
@@ -3019,8 +3019,11 @@ static int do_umount_one(struct mount_info *mi)
 
 static int cr_pivot_root(char *root)
 {
-	char put_root[] = "crtools-put-root.XXXXXX";
+	char tmp_dir_tmpl[] = "crtools-put-root.XXXXXX";
+	bool tmp_dir = false;
+	char *put_root = "tmp";
 	int exit_code = -1;
+	struct stat st;
 
 	pr_info("Move the root to %s\n", root ? : ".");
 
@@ -3031,9 +3034,13 @@ static int cr_pivot_root(char *root)
 		}
 	}
 
-	if (mkdtemp(put_root) == NULL) {
-		pr_perror("Can't create a temporary directory");
-		return -1;
+	if (stat(put_root, &st) || !S_ISDIR(st.st_mode)) {
+		put_root = mkdtemp(tmp_dir_tmpl);
+		if (put_root == NULL) {
+			pr_perror("Can't create a temporary directory");
+			return -1;
+		}
+		tmp_dir = true;
 	}
 
 	if (mount(put_root, put_root, NULL, MS_BIND, NULL)) {
@@ -3070,7 +3077,7 @@ err_tmpfs:
 	}
 
 err_root:
-	if (rmdir(put_root)) {
+	if (tmp_dir && rmdir(put_root)) {
 		pr_perror("Can't remove the directory %s", put_root);
 		return -1;
 	}
-- 
1.8.3.1



More information about the Devel mailing list