[CRIU] [PATCH] mount: don't create a temporary directory for pivot_root()
Andrey Vagin
avagin at openvz.org
Sat Oct 4 15:41:55 PDT 2014
I found this solution in the LXC code. We can open the old root, call
pivot_root(".", "."), call fchdir to the old root and call umount(".").
Now restore will not fail, if the root is read-only.
In addition it's a bit faster.
Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
mount.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/mount.c b/mount.c
index c49f6a4..3e2fe8e 100644
--- a/mount.c
+++ b/mount.c
@@ -1464,7 +1464,7 @@ static int clean_mnt_ns(struct mount_info *mntinfo_tree)
static int cr_pivot_root(char *root)
{
- char put_root[] = "crtools-put-root.XXXXXX";
+ int old_root;
pr_info("Move the root to %s\n", root ? : ".");
@@ -1475,34 +1475,36 @@ static int cr_pivot_root(char *root)
}
}
- if (mkdtemp(put_root) == NULL) {
- pr_perror("Can't create a temporary directory");
+ old_root = open("/", O_DIRECTORY | O_RDONLY);
+ if (old_root < 0) {
+ pr_perror("Unable to open /");
return -1;
}
- if (pivot_root(".", put_root)) {
- pr_perror("pivot_root(., %s) failed", put_root);
- if (rmdir(put_root))
- pr_perror("Can't remove the directory %s", put_root);
+ if (pivot_root(".", ".")) {
+ close(old_root);
+ pr_perror("pivot_root(., .) failed");
return -1;
}
- if (mount("none", put_root, "none", MS_REC|MS_PRIVATE, NULL)) {
- pr_perror("Can't remount root with MS_PRIVATE");
+ if (fchdir(old_root)) {
+ perror("Unable to change working directory");
return -1;
}
+ close(old_root);
- if (mount("none", put_root, "none", MS_REC|MS_PRIVATE, NULL)) {
+ if (mount("none", ".", "none", MS_REC|MS_PRIVATE, NULL)) {
pr_perror("Can't remount root with MS_PRIVATE");
return -1;
}
- if (umount2(put_root, MNT_DETACH)) {
- pr_perror("Can't umount %s", put_root);
+ if (umount2(".", MNT_DETACH)) {
+ pr_perror("Can't umount the old root");
return -1;
}
- if (rmdir(put_root)) {
- pr_perror("Can't remove the directory %s", put_root);
+
+ if (chdir("/")) {
+ perror("Unable to change working directory");
return -1;
}
--
1.9.3
More information about the CRIU
mailing list