From: Harald Hoyer <har...@redhat.com> removes code duplication --- Makefile.am | 1 + src/core/main.c | 2 +- src/core/shutdown.c | 77 +++++--------------------------------------------- src/core/switch-root.c | 6 ++-- src/core/switch-root.h | 2 +- 5 files changed, 13 insertions(+), 75 deletions(-)
diff --git a/Makefile.am b/Makefile.am index 4028112..6401fcb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1873,6 +1873,7 @@ systemd_shutdown_SOURCES = \ systemd_shutdown_LDADD = \ libsystemd-label.la \ + libsystemd-core.la \ libudev-internal.la \ libsystemd-shared.la diff --git a/src/core/main.c b/src/core/main.c index 792b316..1805128 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); 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..554d4da 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) >= 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/core/switch-root.c index 0ea61db..c5a51aa 100644 --- a/src/core/switch-root.c +++ b/src/core/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) { /* Don't try to unmount/move the old "/", there's no way to do it. */ static const char move_mounts[] = @@ -58,7 +58,7 @@ int switch_root(const char *new_root) { * 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"); + temporary_old_root = strappenda(new_root, oldroot); mkdir_p(temporary_old_root, 0755); old_root_remove = in_initrd(); @@ -129,7 +129,7 @@ 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) { + if (detach && umount2(oldroot, MNT_DETACH) < 0) { log_error("Failed to umount old root dir /mnt: %m"); return -errno; } diff --git a/src/core/switch-root.h b/src/core/switch-root.h index ab493b5..fa21d10 100644 --- a/src/core/switch-root.h +++ b/src/core/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); -- 2.1.0 _______________________________________________ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel