diff options
author | Harald Hoyer <harald@redhat.com> | 2014-08-21 16:21:26 +0200 |
---|---|---|
committer | Harald Hoyer <harald@redhat.com> | 2014-08-28 15:25:15 +0200 |
commit | 5a4bf02ff57e4dd3453f2b868c72fe45f60033a3 (patch) | |
tree | 2dfa5385d7af4ad0e8cc040a0a46807e103387c1 /src/core | |
parent | 667a0377fb25ddb0c3efbc43d186ffd4c097ce41 (diff) |
use the switch_root function in shutdown
removes code duplication
also move switch-root to shared
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/main.c | 4 | ||||
-rw-r--r-- | src/core/shutdown.c | 90 | ||||
-rw-r--r-- | src/core/switch-root.c | 164 | ||||
-rw-r--r-- | src/core/switch-root.h | 24 |
4 files changed, 19 insertions, 263 deletions
diff --git a/src/core/main.c b/src/core/main.c index 95ab40fffc..64c2b3f3a1 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1853,8 +1853,8 @@ finish: * deserializing. */ broadcast_signal(SIGTERM, false, true); - /* And switch root */ - r = switch_root(switch_root_dir); + /* And switch root with MS_MOVE, because we remove the old directory afterwards and detach it. */ + r = switch_root(switch_root_dir, "/mnt", true, MS_MOVE); 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 0e2ea5754f..1e88b05790 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 @@ -131,16 +132,7 @@ static int parse_argv(int argc, char *argv[]) { return 0; } -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; - +static int switch_root_initramfs(void) { 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; @@ -151,66 +143,13 @@ static int prepare_new_root(void) { 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; + /* switch_root with MS_BIND, because there might still be processes lurking around, which have open file desriptors. + * /run/initramfs/shutdown will take care of these. + * Also do not detach the old root, because /run/initramfs/shutdown needs to access it. + */ + return switch_root("/run/initramfs", "/oldroot", false, MS_BIND); } -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,16 +311,21 @@ 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) { + r = switch_root_initramfs(); + if (r >= 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"); - } + } else + log_error("Failed to switch root to \"/run/initramfs\": %s", strerror(-r)); + } if (need_umount || need_swapoff || need_loop_detach || need_dm_detach) diff --git a/src/core/switch-root.c b/src/core/switch-root.c deleted file mode 100644 index 0ea61dbb29..0000000000 --- a/src/core/switch-root.c +++ /dev/null @@ -1,164 +0,0 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - -/*** - This file is part of systemd. - - Copyright 2012 Harald Hoyer, Lennart Poettering - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. -***/ - -#include <sys/stat.h> -#include <stdbool.h> -#include <errno.h> -#include <string.h> -#include <sys/mount.h> -#include <unistd.h> -#include <fcntl.h> - -#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) { - - /* Don't try to unmount/move the old "/", there's no way to do it. */ - static const char move_mounts[] = - "/dev\0" - "/proc\0" - "/sys\0" - "/run\0"; - - _cleanup_close_ int old_root_fd = -1; - struct stat new_root_stat; - bool old_root_remove; - 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) { - log_error("Failed to stat directory %s: %m", new_root); - 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; - - 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) { - - /* Mount point seems to be mounted already or - * stat failed. Unmount the old mount - * point. */ - if (umount2(i, MNT_DETACH) < 0) - log_warning("Failed to unmount %s: %m", i); - 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); - } - } - - 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) { - log_error("Failed to change directory to %s: %m", new_root); - return -errno; - } - - if (old_root_remove) { - old_root_fd = open("/", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY|O_DIRECTORY); - if (old_root_fd < 0) - log_warning("Failed to open root directory: %m"); - } - - /* 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); - return -errno; - } - - if (chroot(".") < 0) { - log_error("Failed to change root: %m"); - return -errno; - } - - if (chdir("/") < 0) { - log_error("Failed to change directory: %m"); - return -errno; - } - - if (old_root_fd >= 0) { - struct stat rb; - - if (fstat(old_root_fd, &rb) < 0) - log_warning("Failed to stat old root directory, leaving: %m"); - else { - rm_rf_children(old_root_fd, false, false, &rb); - old_root_fd = -1; - } - } - - return 0; -} diff --git a/src/core/switch-root.h b/src/core/switch-root.h deleted file mode 100644 index ab493b5fb1..0000000000 --- a/src/core/switch-root.h +++ /dev/null @@ -1,24 +0,0 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - -#pragma once - -/*** - This file is part of systemd. - - Copyright 2012 Harald Hoyer, Lennart Poettering - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. -***/ - -int switch_root(const char *switch_root); |