[systemd-devel] [PATCH V5] use the switch_root function in shutdown

harald at redhat.com harald at redhat.com
Thu Aug 28 04:47:36 PDT 2014


From: Harald Hoyer <harald at 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()
V5:
- fixed coding style
- mountflags now argument of switch_root()
- add comment for mountflags used


 Makefile.am                        |  4 +-
 src/core/main.c                    |  4 +-
 src/core/shutdown.c                | 88 ++++++++------------------------------
 src/{core => shared}/switch-root.c | 35 ++++++++-------
 src/{core => shared}/switch-root.h |  2 +-
 5 files changed, 41 insertions(+), 92 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 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..3807e73 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1841,8 +1841,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 1abc140..8026f59 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,15 +132,8 @@ 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) {
+        int r;
 
         if (mount("/run/initramfs", "/run/initramfs", NULL, MS_BIND, NULL) < 0) {
                 log_error("Failed to mount bind /run/initramfs on /run/initramfs: %m");
@@ -151,66 +145,19 @@ 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;
+        /* 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.
+         */
+        r = switch_root("/run/initramfs", "/oldroot", false, MS_BIND);
+        if (r < 0) {
+                log_error("Failed to switch root to \"/run/initramfs\": %m");
+                return r;
         }
 
         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,16 +319,19 @@ 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) {
+                if (switch_root_initramfs() >= 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\": %m");
         }
 
         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 0ea61db..5f075e6 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 ab493b5..adf893a 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);
-- 
2.1.0



More information about the systemd-devel mailing list