Commit d6d6af9a authored by Andrew Vagin's avatar Andrew Vagin Committed by Pavel Emelyanov

mount: pick out a function to bind mount a point in a tmp place

This is used to get a mount without over-mounted parts.
Signed-off-by: 's avatarAndrew Vagin <avagin@virtuozzo.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 6217a84a
...@@ -1068,6 +1068,29 @@ int open_mount(unsigned int s_dev) ...@@ -1068,6 +1068,29 @@ int open_mount(unsigned int s_dev)
return __open_mountpoint(m, -1); 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) static int open_mountpoint(struct mount_info *pm)
{ {
int fd = -1, ns_old = -1; int fd = -1, ns_old = -1;
...@@ -1102,20 +1125,9 @@ static int open_mountpoint(struct mount_info *pm) ...@@ -1102,20 +1125,9 @@ static int open_mountpoint(struct mount_info *pm)
if (switch_ns(pm->nsid->ns_pid, &mnt_ns_desc, &ns_old) < 0) if (switch_ns(pm->nsid->ns_pid, &mnt_ns_desc, &ns_old) < 0)
goto out; goto out;
mnt_path = mkdtemp(mnt_path_tmp); mnt_path = get_clean_mnt(pm, mnt_path_tmp, mnt_path_root);
if (mnt_path == NULL && errno == ENOENT) if (mnt_path == NULL)
mnt_path = mkdtemp(mnt_path_root);
if (mnt_path == NULL) {
pr_perror("Can't create a temporary directory");
goto out; 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); fd = open_detach_mount(mnt_path);
if (fd < 0) if (fd < 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment