276 lines
11 KiB
Diff
276 lines
11 KiB
Diff
From 5a4bf02ff57e4dd3453f2b868c72fe45f60033a3 Mon Sep 17 00:00:00 2001
|
|
From: Harald Hoyer <harald@redhat.com>
|
|
Date: Thu, 21 Aug 2014 16:21:26 +0200
|
|
Subject: [PATCH] use the switch_root function in shutdown
|
|
|
|
removes code duplication
|
|
|
|
also move switch-root to shared
|
|
---
|
|
Makefile.am | 4 +-
|
|
src/core/main.c | 4 +-
|
|
src/core/shutdown.c | 90 +++++++-------------------------------
|
|
src/{core => shared}/switch-root.c | 35 +++++++--------
|
|
src/{core => shared}/switch-root.h | 2 +-
|
|
5 files changed, 39 insertions(+), 96 deletions(-)
|
|
rename src/{core => shared}/switch-root.c (81%)
|
|
rename src/{core => shared}/switch-root.h (88%)
|
|
|
|
diff --git a/Makefile.am b/Makefile.am
|
|
index e091febc1f..1facb8da43 100644
|
|
--- a/Makefile.am
|
|
+++ b/Makefile.am
|
|
@@ -868,6 +868,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 = \
|
|
@@ -1109,8 +1111,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 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/shared/switch-root.c
|
|
similarity index 81%
|
|
rename from src/core/switch-root.c
|
|
rename to src/shared/switch-root.c
|
|
index 0ea61dbb29..5f075e6003 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, unsigned long mountflags) {
|
|
|
|
/* 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,16 @@ 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 (mount(i, new_mount, NULL, mountflags, NULL) < 0) {
|
|
+ if (mountflags & MS_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);
|
|
+ }
|
|
+ if (mountflags & MS_BIND)
|
|
+ log_error("Failed to bind mount %s to %s: %m", i, new_mount);
|
|
|
|
- if (umount2(i, MNT_FORCE) < 0)
|
|
- log_warning("Failed to unmount %s: %m", i);
|
|
}
|
|
}
|
|
|
|
@@ -127,10 +126,10 @@ int switch_root(const char *new_root) {
|
|
* 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");
|
|
+ /* Immediately get rid of the old root, if detach_oldroot is set.
|
|
+ * Since we are running off it we need to do this lazily. */
|
|
+ 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 88%
|
|
rename from src/core/switch-root.h
|
|
rename to src/shared/switch-root.h
|
|
index ab493b5fb1..adf893a922 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, unsigned long mountflags);
|