[CRIU] [PATCH 1/4] mount: pick out a function to bind mount a point in a tmp place

Andrey Vagin avagin at openvz.org
Fri Dec 4 03:38:02 PST 2015


From: Andrew Vagin <avagin at virtuozzo.com>

This is used to get a mount without over-mounted parts.

Signed-off-by: Andrew Vagin <avagin at virtuozzo.com>
---
 mount.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/mount.c b/mount.c
index 2321416..21280d6 100644
--- a/mount.c
+++ b/mount.c
@@ -1054,6 +1054,29 @@ int open_mount(unsigned int s_dev)
 	return __open_mountpoint(m, -1);
 }
 
+/* Bind-mount a mount point in a temporary place without children */
+static char *get_clean_mnt(struct mount_info *mi, char *mnt_path_tmp, char *mnt_path_root)
+{
+	char *mnt_path;
+
+	mnt_path = mkdtemp(mnt_path_tmp);
+	if (mnt_path == NULL && errno == ENOENT)
+		mnt_path = mkdtemp(mnt_path_root);
+	if (mnt_path == NULL) {
+		pr_perror("Can't create a temporary directory");
+		return NULL;;
+	}
+
+	if (mount(mi->mountpoint, mnt_path, NULL, MS_BIND, NULL)) {
+		pr_perror("Can't bind-mount %d:%s to %s",
+				mi->mnt_id, mi->mountpoint, mnt_path);
+		rmdir(mnt_path);
+		return NULL;
+	}
+
+	return mnt_path;
+}
+
 static int open_mountpoint(struct mount_info *pm)
 {
 	int fd = -1, ns_old = -1;
@@ -1088,20 +1111,9 @@ static int open_mountpoint(struct mount_info *pm)
 	if (switch_ns(pm->nsid->ns_pid, &mnt_ns_desc, &ns_old) < 0)
 		goto out;
 
-	mnt_path = mkdtemp(mnt_path_tmp);
-	if (mnt_path == NULL && errno == ENOENT)
-		mnt_path = mkdtemp(mnt_path_root);
-	if (mnt_path == NULL) {
-		pr_perror("Can't create a temporary directory");
+	mnt_path = get_clean_mnt(pm, mnt_path_tmp, mnt_path_root);
+	if (mnt_path == NULL)
 		goto out;
-	}
-
-	if (mount(pm->mountpoint, mnt_path, NULL, MS_BIND, NULL)) {
-		pr_perror("Can't bind-mount %d:%s to %s",
-				pm->mnt_id, pm->mountpoint, mnt_path);
-		rmdir(mnt_path);
-		goto out;
-	}
 
 	fd = open_detach_mount(mnt_path);
 	if (fd < 0)
-- 
2.4.3



More information about the CRIU mailing list