|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
From 5a4bf02ff57e4dd3453f2b868c72fe45f60033a3 Mon Sep 17 00:00:00 2001
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
From: Harald Hoyer <harald@redhat.com>
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Date: Thu, 21 Aug 2014 16:21:26 +0200
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Subject: [PATCH] use the switch_root function in shutdown
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
removes code duplication
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
also move switch-root to shared
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
---
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
Makefile.am | 4 +-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/core/main.c | 4 +-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/core/shutdown.c | 90 +++++++-------------------------------
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/{core => shared}/switch-root.c | 35 +++++++--------
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/{core => shared}/switch-root.h | 2 +-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
5 files changed, 39 insertions(+), 96 deletions(-)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
rename src/{core => shared}/switch-root.c (81%)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
rename src/{core => shared}/switch-root.h (88%)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
diff --git a/Makefile.am b/Makefile.am
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
index e091febc1f..1facb8da43 100644
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
--- a/Makefile.am
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+++ b/Makefile.am
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -868,6 +868,8 @@ libsystemd_shared_la_SOURCES = \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/shared/memfd.h \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/shared/uid-range.c \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/shared/uid-range.h \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ src/shared/switch-root.h \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ src/shared/switch-root.c \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/shared/nss-util.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
nodist_libsystemd_shared_la_SOURCES = \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -1109,8 +1111,6 @@ libsystemd_core_la_SOURCES = \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/core/namespace.h \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/core/build.h \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/core/sysfs-show.h \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- src/core/switch-root.h \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- src/core/switch-root.c \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/core/killall.h \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/core/killall.c \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
src/core/audit-fd.c \
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
diff --git a/src/core/main.c b/src/core/main.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
index 95ab40fffc..64c2b3f3a1 100644
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
--- a/src/core/main.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+++ b/src/core/main.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -1853,8 +1853,8 @@ finish:
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
* deserializing. */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
broadcast_signal(SIGTERM, false, true);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- /* And switch root */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- r = switch_root(switch_root_dir);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ /* And switch root with MS_MOVE, because we remove the old directory afterwards and detach it. */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ r = switch_root(switch_root_dir, "/mnt", true, MS_MOVE);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
if (r < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
log_error("Failed to switch root, ignoring: %s", strerror(-r));
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
diff --git a/src/core/shutdown.c b/src/core/shutdown.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
index 0e2ea5754f..1e88b05790 100644
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
--- a/src/core/shutdown.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+++ b/src/core/shutdown.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -48,6 +48,7 @@
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#include "killall.h"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#include "cgroup-util.h"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#include "def.h"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+#include "switch-root.h"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#define FINALIZE_ATTEMPTS 50
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -131,16 +132,7 @@ static int parse_argv(int argc, char *argv[]) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-static int prepare_new_root(void) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- static const char dirs[] =
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- "/run/initramfs/oldroot\0"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- "/run/initramfs/proc\0"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- "/run/initramfs/sys\0"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- "/run/initramfs/dev\0"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- "/run/initramfs/run\0";
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- const char *dir;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+static int switch_root_initramfs(void) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
if (mount("/run/initramfs", "/run/initramfs", NULL, MS_BIND, NULL) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
log_error("Failed to mount bind /run/initramfs on /run/initramfs: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -151,66 +143,13 @@ static int prepare_new_root(void) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- NULSTR_FOREACH(dir, dirs)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (mkdir_p_label(dir, 0755) < 0 && errno != EEXIST) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("Failed to mkdir %s: %m", dir);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (mount("/sys", "/run/initramfs/sys", NULL, MS_BIND, NULL) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("Failed to mount bind /sys on /run/initramfs/sys: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (mount("/proc", "/run/initramfs/proc", NULL, MS_BIND, NULL) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("Failed to mount bind /proc on /run/initramfs/proc: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (mount("/dev", "/run/initramfs/dev", NULL, MS_BIND, NULL) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("Failed to mount bind /dev on /run/initramfs/dev: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (mount("/run", "/run/initramfs/run", NULL, MS_BIND, NULL) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("Failed to mount bind /run on /run/initramfs/run: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ /* switch_root with MS_BIND, because there might still be processes lurking around, which have open file desriptors.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ * /run/initramfs/shutdown will take care of these.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ * Also do not detach the old root, because /run/initramfs/shutdown needs to access it.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ return switch_root("/run/initramfs", "/oldroot", false, MS_BIND);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-static int pivot_to_new_root(void) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (chdir("/run/initramfs") < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("Failed to change directory to /run/initramfs: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- /* Work-around for a kernel bug: for some reason the kernel
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * refuses switching root if any file systems are mounted
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * MS_SHARED. Hence remount them MS_PRIVATE here as a
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * work-around.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- *
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_warning("Failed to make \"/\" private mount: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (pivot_root(".", "oldroot") < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("pivot failed: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- /* only chroot if pivot root succeeded */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- chroot(".");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- setsid();
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- make_console_stdio();
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_info("Successfully changed into root pivot.");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
int main(int argc, char *argv[]) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
bool need_umount, need_swapoff, need_loop_detach, need_dm_detach;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -372,16 +311,21 @@ int main(int argc, char *argv[]) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
if (!in_container && !in_initrd() &&
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
access("/run/initramfs/shutdown", X_OK) == 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (prepare_new_root() >= 0 &&
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- pivot_to_new_root() >= 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ r = switch_root_initramfs();
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ if (r >= 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
arguments[0] = (char*) "/shutdown";
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_info("Returning to initrd...");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ setsid();
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ make_console_stdio();
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ log_info("Successfully changed into root pivot.\n"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ "Returning to initrd...");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
execv("/shutdown", arguments);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
log_error("Failed to execute shutdown binary: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ } else
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ log_error("Failed to switch root to \"/run/initramfs\": %s", strerror(-r));
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
if (need_umount || need_swapoff || need_loop_detach || need_dm_detach)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
diff --git a/src/core/switch-root.c b/src/shared/switch-root.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
similarity index 81%
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
rename from src/core/switch-root.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
rename to src/shared/switch-root.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
index 0ea61dbb29..5f075e6003 100644
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
--- a/src/core/switch-root.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+++ b/src/shared/switch-root.c
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -34,7 +34,7 @@
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#include "base-filesystem.h"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
#include "missing.h"
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-int switch_root(const char *new_root) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, unsigned long mountflags) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
/* Don't try to unmount/move the old "/", there's no way to do it. */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
static const char move_mounts[] =
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -52,14 +52,8 @@ int switch_root(const char *new_root) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
if (path_equal(new_root, "/"))
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- /* When using pivot_root() we assume that /mnt exists as place
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * we can temporarily move the old root to. As we immediately
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * unmount it from there it doesn't matter much which
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * directory we choose for this, but it should be more likely
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * than not that /mnt exists and is suitable as mount point
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * and is on the same fs as the old root dir */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- temporary_old_root = strappenda(new_root, "/mnt");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- mkdir_p(temporary_old_root, 0755);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ temporary_old_root = strappenda(new_root, oldroot);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ mkdir_p_label(temporary_old_root, 0755);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
old_root_remove = in_initrd();
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -84,7 +78,7 @@ int switch_root(const char *new_root) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
snprintf(new_mount, sizeof(new_mount), "%s%s", new_root, i);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
char_array_0(new_mount);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- mkdir_p(new_mount, 0755);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ mkdir_p_label(new_mount, 0755);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
if ((stat(new_mount, &sb) < 0) ||
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
sb.st_dev != new_root_stat.st_dev) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -97,11 +91,16 @@ int switch_root(const char *new_root) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
continue;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (mount(i, new_mount, NULL, MS_MOVE, NULL) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("Failed to move mount %s to %s, forcing unmount: %m", i, new_mount);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ if (mount(i, new_mount, NULL, mountflags, NULL) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ if (mountflags & MS_MOVE) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ log_error("Failed to move mount %s to %s, forcing unmount: %m", i, new_mount);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ if (umount2(i, MNT_FORCE) < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ log_warning("Failed to unmount %s: %m", i);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ }
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ if (mountflags & MS_BIND)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ log_error("Failed to bind mount %s to %s: %m", i, new_mount);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (umount2(i, MNT_FORCE) < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_warning("Failed to unmount %s: %m", i);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -127,10 +126,10 @@ int switch_root(const char *new_root) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
* not possible however, and hence we simply overmount root */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
if (pivot_root(new_root, temporary_old_root) >= 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- /* Immediately get rid of the old root. Since we are
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- * running off it we need to do this lazily. */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- if (umount2("/mnt", MNT_DETACH) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
- log_error("Failed to umount old root dir /mnt: %m");
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ /* Immediately get rid of the old root, if detach_oldroot is set.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ * Since we are running off it we need to do this lazily. */
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ if (detach_oldroot && umount2(oldroot, MNT_DETACH) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+ log_error("Failed to umount old root dir %s: %m", oldroot);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
diff --git a/src/core/switch-root.h b/src/shared/switch-root.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
similarity index 88%
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
rename from src/core/switch-root.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
rename to src/shared/switch-root.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
index ab493b5fb1..adf893a922 100644
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
--- a/src/core/switch-root.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+++ b/src/shared/switch-root.h
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
@@ -21,4 +21,4 @@
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
***/
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
-int switch_root(const char *switch_root);
|
|
Zbigniew Jędrzejewski-Szmek |
62fe94 |
+int switch_root(const char *new_root, const char *oldroot, bool detach_oldroot, unsigned long mountflags);
|