X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Fswitch-root.c;h=0ea61dbb290f8413fba8cc0f28990bde4ca76966;hb=4427c3f43a87c2e0c784fda6be1b9715be820733;hp=ed0a31e6978b3649429f57f83710ed7dc621d52e;hpb=416693175bc317ef3fa4963af51a5ee077320d09;p=elogind.git diff --git a/src/core/switch-root.c b/src/core/switch-root.c index ed0a31e69..0ea61dbb2 100644 --- a/src/core/switch-root.c +++ b/src/core/switch-root.c @@ -30,6 +30,9 @@ #include "util.h" #include "path-util.h" #include "switch-root.h" +#include "mkdir.h" +#include "base-filesystem.h" +#include "missing.h" int switch_root(const char *new_root) { @@ -40,22 +43,40 @@ int switch_root(const char *new_root) { "/sys\0" "/run\0"; - int r, old_root_fd = -1; + _cleanup_close_ int old_root_fd = -1; struct stat new_root_stat; bool old_root_remove; - const char *i; + const char *i, *temporary_old_root; + int r; if (path_equal(new_root, "/")) return 0; + /* When using pivot_root() we assume that /mnt exists as place + * we can temporarily move the old root to. As we immediately + * unmount it from there it doesn't matter much which + * directory we choose for this, but it should be more likely + * than not that /mnt exists and is suitable as mount point + * and is on the same fs as the old root dir */ + temporary_old_root = strappenda(new_root, "/mnt"); + mkdir_p(temporary_old_root, 0755); + old_root_remove = in_initrd(); if (stat(new_root, &new_root_stat) < 0) { - r = -errno; log_error("Failed to stat directory %s: %m", new_root); - goto fail; + return -errno; } + /* Work-around for a kernel bug: for some reason the kernel + * refuses switching root if any file systems are mounted + * MS_SHARED. Hence remount them MS_PRIVATE here as a + * work-around. + * + * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */ + if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0) + log_warning("Failed to make \"/\" private mount: %m"); + NULSTR_FOREACH(i, move_mounts) { char new_mount[PATH_MAX]; struct stat sb; @@ -63,6 +84,8 @@ int switch_root(const char *new_root) { snprintf(new_mount, sizeof(new_mount), "%s%s", new_root, i); char_array_0(new_mount); + mkdir_p(new_mount, 0755); + if ((stat(new_mount, &sb) < 0) || sb.st_dev != new_root_stat.st_dev) { @@ -82,10 +105,15 @@ int switch_root(const char *new_root) { } } + r = base_filesystem_create(new_root); + if (r < 0) { + log_error("Failed to create the base filesystem: %s", strerror(-r)); + return r; + } + if (chdir(new_root) < 0) { - r = -errno; log_error("Failed to change directory to %s: %m", new_root); - goto fail; + return -errno; } if (old_root_remove) { @@ -94,16 +122,31 @@ int switch_root(const char *new_root) { log_warning("Failed to open root directory: %m"); } - if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) { - r = -errno; + /* We first try a pivot_root() so that we can umount the old + * root dir. In many cases (i.e. where rootfs is /), that's + * not possible however, and hence we simply overmount root */ + if (pivot_root(new_root, temporary_old_root) >= 0) { + + /* Immediately get rid of the old root. Since we are + * running off it we need to do this lazily. */ + if (umount2("/mnt", MNT_DETACH) < 0) { + log_error("Failed to umount old root dir /mnt: %m"); + return -errno; + } + + } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) { log_error("Failed to mount moving %s to /: %m", new_root); - goto fail; + return -errno; } if (chroot(".") < 0) { - r = -errno; log_error("Failed to change root: %m"); - goto fail; + return -errno; + } + + if (chdir("/") < 0) { + log_error("Failed to change directory: %m"); + return -errno; } if (old_root_fd >= 0) { @@ -111,15 +154,11 @@ int switch_root(const char *new_root) { if (fstat(old_root_fd, &rb) < 0) log_warning("Failed to stat old root directory, leaving: %m"); - else + else { rm_rf_children(old_root_fd, false, false, &rb); + old_root_fd = -1; + } } - r = 0; - -fail: - if (old_root_fd >= 0) - close_nointr_nofail(old_root_fd); - - return r; + return 0; }