From: Harald Hoyer <har...@redhat.com> removes code duplication
also move switch-root to shared --- V2: - Removed all references to "/mnt" in switch_root() and the bogus comment. V3: - moved switch-root.[ch] to shared - added switch to mount MS_MOVE or MS_BIND the old dirs V4: - mkdir_p_label() in switch_root() Makefile.am | 4 +- src/core/main.c | 2 +- src/core/shutdown.c | 77 ++++---------------------------------- src/{core => shared}/switch-root.c | 34 ++++++++--------- src/{core => shared}/switch-root.h | 2 +- 5 files changed, 28 insertions(+), 91 deletions(-) rename src/{core => shared}/switch-root.c (83%) rename src/{core => shared}/switch-root.h (89%) diff --git a/Makefile.am b/Makefile.am index 4028112..14ba8f8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -864,6 +864,8 @@ libsystemd_shared_la_SOURCES = \ src/shared/memfd.h \ src/shared/uid-range.c \ src/shared/uid-range.h \ + src/shared/switch-root.h \ + src/shared/switch-root.c \ src/shared/nss-util.h nodist_libsystemd_shared_la_SOURCES = \ @@ -1105,8 +1107,6 @@ libsystemd_core_la_SOURCES = \ src/core/namespace.h \ src/core/build.h \ src/core/sysfs-show.h \ - src/core/switch-root.h \ - src/core/switch-root.c \ src/core/killall.h \ src/core/killall.c \ src/core/audit-fd.c \ diff --git a/src/core/main.c b/src/core/main.c index 792b316..62a038c 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1842,7 +1842,7 @@ finish: broadcast_signal(SIGTERM, false, true); /* And switch root */ - r = switch_root(switch_root_dir); + r = switch_root(switch_root_dir, "/mnt", true, true); if (r < 0) log_error("Failed to switch root, ignoring: %s", strerror(-r)); } diff --git a/src/core/shutdown.c b/src/core/shutdown.c index 1abc140..f0c66a1 100644 --- a/src/core/shutdown.c +++ b/src/core/shutdown.c @@ -48,6 +48,7 @@ #include "killall.h" #include "cgroup-util.h" #include "def.h" +#include "switch-root.h" #define FINALIZE_ATTEMPTS 50 @@ -132,15 +133,6 @@ static int parse_argv(int argc, char *argv[]) { } static int prepare_new_root(void) { - static const char dirs[] = - "/run/initramfs/oldroot\0" - "/run/initramfs/proc\0" - "/run/initramfs/sys\0" - "/run/initramfs/dev\0" - "/run/initramfs/run\0"; - - const char *dir; - if (mount("/run/initramfs", "/run/initramfs", NULL, MS_BIND, NULL) < 0) { log_error("Failed to mount bind /run/initramfs on /run/initramfs: %m"); return -errno; @@ -150,67 +142,9 @@ static int prepare_new_root(void) { log_error("Failed to make /run/initramfs private mount: %m"); return -errno; } - - NULSTR_FOREACH(dir, dirs) - if (mkdir_p_label(dir, 0755) < 0 && errno != EEXIST) { - log_error("Failed to mkdir %s: %m", dir); - return -errno; - } - - if (mount("/sys", "/run/initramfs/sys", NULL, MS_BIND, NULL) < 0) { - log_error("Failed to mount bind /sys on /run/initramfs/sys: %m"); - return -errno; - } - - if (mount("/proc", "/run/initramfs/proc", NULL, MS_BIND, NULL) < 0) { - log_error("Failed to mount bind /proc on /run/initramfs/proc: %m"); - return -errno; - } - - if (mount("/dev", "/run/initramfs/dev", NULL, MS_BIND, NULL) < 0) { - log_error("Failed to mount bind /dev on /run/initramfs/dev: %m"); - return -errno; - } - - if (mount("/run", "/run/initramfs/run", NULL, MS_BIND, NULL) < 0) { - log_error("Failed to mount bind /run on /run/initramfs/run: %m"); - return -errno; - } - return 0; } -static int pivot_to_new_root(void) { - - if (chdir("/run/initramfs") < 0) { - log_error("Failed to change directory to /run/initramfs: %m"); - 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"); - - if (pivot_root(".", "oldroot") < 0) { - log_error("pivot failed: %m"); - /* only chroot if pivot root succeeded */ - return -errno; - } - - chroot("."); - - setsid(); - make_console_stdio(); - - log_info("Successfully changed into root pivot."); - - return 0; -} int main(int argc, char *argv[]) { bool need_umount, need_swapoff, need_loop_detach, need_dm_detach; @@ -372,12 +306,15 @@ int main(int argc, char *argv[]) { if (!in_container && !in_initrd() && access("/run/initramfs/shutdown", X_OK) == 0) { - if (prepare_new_root() >= 0 && - pivot_to_new_root() >= 0) { + switch_root("/run/initramfs", "/oldroot", false, false) >= 0) { arguments[0] = (char*) "/shutdown"; - log_info("Returning to initrd..."); + setsid(); + make_console_stdio(); + + log_info("Successfully changed into root pivot.\n" + "Returning to initrd..."); execv("/shutdown", arguments); log_error("Failed to execute shutdown binary: %m"); diff --git a/src/core/switch-root.c b/src/shared/switch-root.c similarity index 83% rename from src/core/switch-root.c rename to src/shared/switch-root.c index 0ea61db..01a22ca 100644 --- a/src/core/switch-root.c +++ b/src/shared/switch-root.c @@ -34,7 +34,7 @@ #include "base-filesystem.h" #include "missing.h" -int switch_root(const char *new_root) { +int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, bool mount_move) { /* Don't try to unmount/move the old "/", there's no way to do it. */ static const char move_mounts[] = @@ -52,14 +52,8 @@ int switch_root(const char *new_root) { 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); + temporary_old_root = strappenda(new_root, oldroot); + mkdir_p_label(temporary_old_root, 0755); old_root_remove = in_initrd(); @@ -84,7 +78,7 @@ 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); + mkdir_p_label(new_mount, 0755); if ((stat(new_mount, &sb) < 0) || sb.st_dev != new_root_stat.st_dev) { @@ -97,11 +91,17 @@ int switch_root(const char *new_root) { continue; } - if (mount(i, new_mount, NULL, MS_MOVE, NULL) < 0) { - log_error("Failed to move mount %s to %s, forcing unmount: %m", i, new_mount); - - if (umount2(i, MNT_FORCE) < 0) - log_warning("Failed to unmount %s: %m", i); + if (mount(i, new_mount, NULL, mount_move ? MS_MOVE : MS_BIND, NULL) < 0) { + if (mount_move) { + log_error("Failed to move mount %s to %s, forcing unmount: %m", + i, new_mount); + + if (umount2(i, MNT_FORCE) < 0) + log_warning("Failed to unmount %s: %m", i); + } else { + log_error("Failed to bind mount %s to %s: %m", + i, new_mount); + } } } @@ -129,8 +129,8 @@ int switch_root(const char *new_root) { /* 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"); + if (detach_oldroot && umount2(oldroot, MNT_DETACH) < 0) { + log_error("Failed to umount old root dir %s: %m", oldroot); return -errno; } diff --git a/src/core/switch-root.h b/src/shared/switch-root.h similarity index 89% rename from src/core/switch-root.h rename to src/shared/switch-root.h index ab493b5..fafbcd3 100644 --- a/src/core/switch-root.h +++ b/src/shared/switch-root.h @@ -21,4 +21,4 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -int switch_root(const char *switch_root); +int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, bool mount_move); -- 2.1.0 _______________________________________________ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel