[systemd-commits] 11 commits - Makefile.am src/core src/delta src/shared

Lennart Poettering lennart at kemper.freedesktop.org
Mon May 21 10:53:15 PDT 2012


 Makefile.am            |    4 +
 src/core/main.c        |   91 ++++++++++++++++++-----------------
 src/core/manager.c     |    7 +-
 src/core/switch-root.c |  125 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/core/switch-root.h |   27 ++++++++++
 src/delta/delta.c      |    2 
 src/shared/util.c      |   54 ++++++++++++++++-----
 src/shared/util.h      |    2 
 8 files changed, 251 insertions(+), 61 deletions(-)

New commits:
commit 2660882b52ae1a5d97a2344633a999d88a3cd45b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon May 21 19:48:04 2012 +0200

    main: rename a few fix to follow general naming style

diff --git a/src/core/main.c b/src/core/main.c
index 2205222..486dac8 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -747,7 +747,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_SHOW_STATUS,
                 ARG_SYSV_CONSOLE,
                 ARG_DESERIALIZE,
-                ARG_SWITCHEDROOT,
+                ARG_SWITCHED_ROOT,
                 ARG_INTROSPECT,
                 ARG_DEFAULT_STD_OUTPUT,
                 ARG_DEFAULT_STD_ERROR
@@ -772,7 +772,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "sysv-console",             optional_argument, NULL, ARG_SYSV_CONSOLE             },
 #endif
                 { "deserialize",              required_argument, NULL, ARG_DESERIALIZE              },
-                { "switchedroot",             no_argument,       NULL, ARG_SWITCHEDROOT             },
+                { "switched-root",            no_argument,       NULL, ARG_SWITCHED_ROOT            },
                 { "introspect",               optional_argument, NULL, ARG_INTROSPECT               },
                 { "default-standard-output",  required_argument, NULL, ARG_DEFAULT_STD_OUTPUT,      },
                 { "default-standard-error",   required_argument, NULL, ARG_DEFAULT_STD_ERROR,       },
@@ -944,7 +944,7 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
                 }
 
-                case ARG_SWITCHEDROOT:
+                case ARG_SWITCHED_ROOT:
                         /* Nothing special yet */
                         break;
 
@@ -1184,7 +1184,7 @@ int main(int argc, char *argv[]) {
         const char *shutdown_verb = NULL;
         dual_timestamp initrd_timestamp = { 0ULL, 0ULL };
         static char systemd[] = "systemd";
-        bool is_reexec = false;
+        bool skip_setup = false;
         int j;
         bool loaded_policy = false;
         bool arm_reboot_watchdog = false;
@@ -1205,17 +1205,17 @@ int main(int argc, char *argv[]) {
         /* Determine if this is a reexecution or normal bootup. We do
          * the full command line parsing much later, so let's just
          * have a quick peek here. */
-
         for (j = 1; j < argc; j++)
                 if (streq(argv[j], "--deserialize")) {
-                        is_reexec = true;
+                        skip_setup = true;
                         break;
                 }
 
-        /* If we have switched root, do all the special things */
+        /* If we have switched root, do all the special setup
+         * things */
         for (j = 1; j < argc; j++)
-                if (streq(argv[j], "--switchedroot")) {
-                        is_reexec = false;
+                if (streq(argv[j], "--switched-root")) {
+                        skip_setup = false;
                         break;
                 }
 
@@ -1250,7 +1250,7 @@ int main(int argc, char *argv[]) {
                 arg_running_as = MANAGER_SYSTEM;
                 log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_JOURNAL : LOG_TARGET_JOURNAL_OR_KMSG);
 
-                if (!is_reexec) {
+                if (!skip_setup) {
                         if (selinux_setup(&loaded_policy) < 0)
                                 goto finish;
                         if (ima_setup() < 0)
@@ -1262,7 +1262,7 @@ int main(int argc, char *argv[]) {
                 if (label_init(NULL) < 0)
                         goto finish;
 
-                if (!is_reexec)
+                if (!skip_setup)
                         if (hwclock_is_localtime() > 0) {
                                 int min;
 
@@ -1374,7 +1374,7 @@ int main(int argc, char *argv[]) {
                 /* Parse the data passed to us. We leave this
                  * variables set, but the manager later on will not
                  * pass them on to our children. */
-                if(!in_initrd())
+                if (!in_initrd())
                         parse_initrd_timestamp(&initrd_timestamp);
 
                 /* Unset some environment variables passed in from the
@@ -1388,7 +1388,7 @@ int main(int argc, char *argv[]) {
                 unsetenv("SHLVL");
                 unsetenv("_");
 
-                /* When we are invoked by a tool chroot-like such as
+                /* When we are invoked by a chroot-like tool such as
                  * nspawn, these might be set, but make little sense
                  * to pass on */
                 unsetenv("USER");
@@ -1415,7 +1415,7 @@ int main(int argc, char *argv[]) {
         /* Reset the console, but only if this is really init and we
          * are freshly booted */
         if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
-                console_setup(getpid() == 1 && !is_reexec);
+                console_setup(getpid() == 1 && !skip_setup);
                 make_null_stdio();
         }
 
@@ -1436,7 +1436,7 @@ int main(int argc, char *argv[]) {
         log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
                  PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
 
-        if (arg_running_as == MANAGER_SYSTEM && !is_reexec) {
+        if (arg_running_as == MANAGER_SYSTEM && !skip_setup) {
                 locale_setup();
 
                 if (arg_show_status || plymouth_running())
@@ -1671,7 +1671,7 @@ finish:
                         i = 0;
                         args[i++] = SYSTEMD_BINARY_PATH;
                         if (switch_root_dir)
-                                args[i++] = "--switchedroot";
+                                args[i++] = "--switched-root";
                         args[i++] = arg_running_as == MANAGER_SYSTEM ? "--system" : "--user";
                         args[i++] = "--deserialize";
                         args[i++] = sfd;

commit 416693175bc317ef3fa4963af51a5ee077320d09
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon May 21 19:33:39 2012 +0200

    switch-root: move switch_root() call into its own .c file

diff --git a/Makefile.am b/Makefile.am
index 7d6d744..e9ac82c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -789,7 +789,9 @@ libsystemd_core_la_SOURCES = \
 	src/core/special.h \
 	src/core/bus-errors.h \
 	src/core/build.h \
-	src/core/sysfs-show.h
+	src/core/sysfs-show.h \
+	src/core/switch-root.h \
+	src/core/switch-root.c
 
 nodist_libsystemd_core_la_SOURCES = \
 	src/core/load-fragment-gperf.c \
diff --git a/src/core/main.c b/src/core/main.c
index 4d09cb7..2205222 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -49,6 +49,7 @@
 #include "virt.h"
 #include "watchdog.h"
 #include "path-util.h"
+#include "switch-root.h"
 
 #include "mount-setup.h"
 #include "loopback-setup.h"
@@ -1173,90 +1174,6 @@ static void test_cgroups(void) {
         sleep(10);
 }
 
-static int do_switch_root(const char *switch_root) {
-        int r=0;
-        /*  Don't try to unmount the old "/", there's no way to do it. */
-        const char *umounts[] = { "/dev", "/proc", "/sys", "/run", NULL };
-        int i;
-        int cfd = -1;
-        struct stat switch_root_stat, sb;
-        bool remove_old_root;
-
-        if (path_equal(switch_root, "/"))
-                return 0;
-
-        if (stat(switch_root, &switch_root_stat) != 0) {
-                r = -errno;
-                log_error("failed to stat directory %s", switch_root);
-                goto fail;
-        }
-
-        remove_old_root = in_initrd();
-
-        for (i = 0; umounts[i] != NULL; i++) {
-                char newmount[PATH_MAX];
-
-                snprintf(newmount, sizeof(newmount), "%s%s", switch_root, umounts[i]);
-
-                if ((stat(newmount, &sb) != 0) || (sb.st_dev != switch_root_stat.st_dev)) {
-                        /* mount point seems to be mounted already or stat failed */
-                        umount2(umounts[i], MNT_DETACH);
-                        continue;
-                }
-
-                if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
-                        log_error("failed to mount moving %s to %s",
-                                  umounts[i], newmount);
-                        log_error("forcing unmount of %s", umounts[i]);
-                        umount2(umounts[i], MNT_FORCE);
-                }
-        }
-
-        if (chdir(switch_root)) {
-                r = -errno;
-                log_error("failed to change directory to %s", switch_root);
-                goto fail;
-        }
-
-        if (remove_old_root)
-                cfd = open("/", O_RDONLY);
-
-        if (mount(switch_root, "/", NULL, MS_MOVE, NULL) < 0) {
-                r = -errno;
-                log_error("failed to mount moving %s to /", switch_root);
-                goto fail;
-        }
-
-        if (chroot(".")) {
-                r = -errno;
-                log_error("failed to change root");
-                goto fail;
-        }
-
-        if (cfd >= 0) {
-                struct stat rb;
-
-                if (fstat(cfd, &rb)) {
-                        log_error("failed to stat old root directory");
-                        goto fail;
-                }
-
-                rm_rf_children(cfd, false, false, &rb);
-                close(cfd);
-                cfd=-1;
-        }
-
-        return 0;
-
-fail:
-        if (cfd >= 0)
-                close(cfd);
-
-        log_error("Failed to switch root, ignoring: %s", strerror(-r));
-
-        return r;
-}
-
 int main(int argc, char *argv[]) {
         Manager *m = NULL;
         int r, retval = EXIT_FAILURE;
@@ -1271,7 +1188,7 @@ int main(int argc, char *argv[]) {
         int j;
         bool loaded_policy = false;
         bool arm_reboot_watchdog = false;
-        char *switch_root = NULL, *switch_root_init = NULL;
+        char *switch_root_dir = NULL, *switch_root_init = NULL;
 
 #ifdef HAVE_SYSV_COMPAT
         if (getpid() != 1 && strstr(program_invocation_short_name, "init")) {
@@ -1673,7 +1590,7 @@ int main(int argc, char *argv[]) {
 
                 case MANAGER_SWITCH_ROOT:
                         /* Steal the switch root parameters */
-                        switch_root = m->switch_root;
+                        switch_root_dir = m->switch_root;
                         switch_root_init = m->switch_root_init;
                         m->switch_root = m->switch_root_init = NULL;
 
@@ -1728,8 +1645,11 @@ finish:
                  * rebooted while we do that */
                 watchdog_close(true);
 
-                if (switch_root)
-                        do_switch_root(switch_root);
+                if (switch_root_dir) {
+                        r = switch_root(switch_root_dir);
+                        if (r < 0)
+                                log_error("Failed to switch root, ignoring: %s", strerror(-r));
+                }
 
                 args_size = MAX(6, argc+1);
                 args = newa(const char*, args_size);
@@ -1750,7 +1670,7 @@ finish:
 
                         i = 0;
                         args[i++] = SYSTEMD_BINARY_PATH;
-                        if (switch_root)
+                        if (switch_root_dir)
                                 args[i++] = "--switchedroot";
                         args[i++] = arg_running_as == MANAGER_SYSTEM ? "--system" : "--user";
                         args[i++] = "--deserialize";
diff --git a/src/core/switch-root.c b/src/core/switch-root.c
new file mode 100644
index 0000000..ed0a31e
--- /dev/null
+++ b/src/core/switch-root.c
@@ -0,0 +1,125 @@
+/*-*- 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"
+
+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";
+
+        int r, old_root_fd = -1;
+        struct stat new_root_stat;
+        bool old_root_remove;
+        const char *i;
+
+        if (path_equal(new_root, "/"))
+                return 0;
+
+        old_root_remove = in_initrd();
+
+        if (stat(new_root, &new_root_stat) < 0) {
+                r = -errno;
+                log_error("Failed to stat directory %s: %m", new_root);
+                goto fail;
+        }
+
+        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);
+
+                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);
+                }
+        }
+
+        if (chdir(new_root) < 0) {
+                r = -errno;
+                log_error("Failed to change directory to %s: %m", new_root);
+                goto fail;
+        }
+
+        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");
+        }
+
+        if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) {
+                r = -errno;
+                log_error("Failed to mount moving %s to /: %m", new_root);
+                goto fail;
+        }
+
+        if (chroot(".") < 0) {
+                r = -errno;
+                log_error("Failed to change root: %m");
+                goto fail;
+        }
+
+        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);
+        }
+
+        r = 0;
+
+fail:
+        if (old_root_fd >= 0)
+                close_nointr_nofail(old_root_fd);
+
+        return r;
+}
diff --git a/src/core/switch-root.h b/src/core/switch-root.h
new file mode 100644
index 0000000..0c4cd1e
--- /dev/null
+++ b/src/core/switch-root.h
@@ -0,0 +1,27 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef fooswitchroothfoo
+#define fooswitchroothfoo
+
+/***
+  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);
+
+#endif

commit 97a66e94700845239cfe628da3569ac06034fa4d
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 15:08:28 2012 +0200

    main: do_switch_root() do not recursively remove across device boundaries

diff --git a/src/core/main.c b/src/core/main.c
index 878eaf3..4d09cb7 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1234,7 +1234,14 @@ static int do_switch_root(const char *switch_root) {
         }
 
         if (cfd >= 0) {
-                rm_rf_children(cfd, false, false);
+                struct stat rb;
+
+                if (fstat(cfd, &rb)) {
+                        log_error("failed to stat old root directory");
+                        goto fail;
+                }
+
+                rm_rf_children(cfd, false, false, &rb);
                 close(cfd);
                 cfd=-1;
         }

commit 597f43c7849dc9fc9aeb806f8d4f9679cabce8e6
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 15:08:27 2012 +0200

    util: rm_rf_children() add root_dev parameter
    
    if root_dev is set, remove subdirectories only, if the device is the
    same as the root_dev. This prevents to remove files across device
    boundaries.

diff --git a/src/shared/util.c b/src/shared/util.c
index 681484b..5acddd3 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -3139,7 +3139,7 @@ int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
         return 0;
 }
 
-int rm_rf_children(int fd, bool only_dirs, bool honour_sticky) {
+int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
         DIR *d;
         int ret = 0;
 
@@ -3208,24 +3208,37 @@ int rm_rf_children(int fd, bool only_dirs, bool honour_sticky) {
 
                 if (is_dir) {
                         int subdir_fd;
-
-                        subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
-                        if (subdir_fd < 0) {
-                                if (ret == 0 && errno != ENOENT)
-                                        ret = -errno;
-                                continue;
+                        struct stat sb;
+                        if (root_dev) {
+                                if (fstatat(fd, de->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
+                                        if (ret == 0 && errno != ENOENT)
+                                                ret = -errno;
+                                        continue;
+                                }
                         }
 
-                        r = rm_rf_children(subdir_fd, only_dirs, honour_sticky);
-                        if (r < 0 && ret == 0)
-                                ret = r;
+                        /* if root_dev is set, remove subdirectories only, if device is same as dir */
+                        if ((root_dev == NULL) || (sb.st_dev == root_dev->st_dev)) {
 
-                        if (!keep_around)
-                                if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
+                                subdir_fd = openat(fd, de->d_name,
+                                                   O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
+                                if (subdir_fd < 0) {
                                         if (ret == 0 && errno != ENOENT)
                                                 ret = -errno;
+                                        continue;
                                 }
 
+                                r = rm_rf_children(subdir_fd, only_dirs, honour_sticky, root_dev);
+                                if (r < 0 && ret == 0)
+                                        ret = r;
+
+                                if (!keep_around)
+                                        if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
+                                                if (ret == 0 && errno != ENOENT)
+                                                        ret = -errno;
+                                        }
+                        }
+
                 } else if (!only_dirs && !keep_around) {
 
                         if (unlinkat(fd, de->d_name, 0) < 0) {
@@ -3259,7 +3272,7 @@ int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky
                 return 0;
         }
 
-        r = rm_rf_children(fd, only_dirs, honour_sticky);
+        r = rm_rf_children(fd, only_dirs, honour_sticky, NULL);
 
         if (delete_root) {
 
diff --git a/src/shared/util.h b/src/shared/util.h
index 4fccf96..2d890fa 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -349,7 +349,7 @@ int get_ctty(pid_t, dev_t *_devnr, char **r);
 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
 int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
 
-int rm_rf_children(int fd, bool only_dirs, bool honour_sticky);
+int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev);
 int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
 
 int pipe_eof(int fd);

commit d966a7b3631a54602297ef3bff1dcaf35cbefd12
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 14:22:45 2012 +0200

    delta/delta.c: initialize bottom for fail state

diff --git a/src/delta/delta.c b/src/delta/delta.c
index 1a5b08a..4694fc8 100644
--- a/src/delta/delta.c
+++ b/src/delta/delta.c
@@ -231,7 +231,7 @@ finish:
 static int process_suffix(const char *prefixes, const char *suffix) {
         const char *p;
         char *f;
-        Hashmap *top, *bottom;
+        Hashmap *top, *bottom=NULL;
         int r = 0, k;
         Iterator i;
         int n_found = 0;

commit c3ba62509efe274c2bf6482056044e6fab65fec0
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 14:22:44 2012 +0200

    core/main.c: handle the initrd timestamp differently, if in the initrd
    
    If systemd is in the initrd/initramfs, set the initrd timestamp and do
    not try to read it from the RD_TIMESTAMP environment variable.

diff --git a/src/core/main.c b/src/core/main.c
index d7143ed..878eaf3 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1310,6 +1310,19 @@ int main(int argc, char *argv[]) {
         log_set_max_level(LOG_INFO);
 
         if (getpid() == 1) {
+                if (in_initrd()) {
+                        char *rd_timestamp = NULL;
+
+                        dual_timestamp_get(&initrd_timestamp);
+                        asprintf(&rd_timestamp, "%llu %llu",
+                                 (unsigned long long) initrd_timestamp.realtime,
+                                 (unsigned long long) initrd_timestamp.monotonic);
+                        if (rd_timestamp) {
+                                setenv("RD_TIMESTAMP", rd_timestamp, 1);
+                                free(rd_timestamp);
+                        }
+                }
+
                 arg_running_as = MANAGER_SYSTEM;
                 log_set_target(detect_container(NULL) > 0 ? LOG_TARGET_JOURNAL : LOG_TARGET_JOURNAL_OR_KMSG);
 
@@ -1437,7 +1450,8 @@ int main(int argc, char *argv[]) {
                 /* Parse the data passed to us. We leave this
                  * variables set, but the manager later on will not
                  * pass them on to our children. */
-                parse_initrd_timestamp(&initrd_timestamp);
+                if(!in_initrd())
+                        parse_initrd_timestamp(&initrd_timestamp);
 
                 /* Unset some environment variables passed in from the
                  * kernel that don't really make sense for us. */

commit e148ac5a6cccd1c9c23bf7b3085b50fdb1cd1cf9
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 14:22:43 2012 +0200

    core/main.c: do_switch_root(): do not remove the old root if not in initrd
    
    Only recursively remove the old root, if we have been in an
    initrd/initramfs.

diff --git a/src/core/main.c b/src/core/main.c
index 7471214..d7143ed 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1180,6 +1180,7 @@ static int do_switch_root(const char *switch_root) {
         int i;
         int cfd = -1;
         struct stat switch_root_stat, sb;
+        bool remove_old_root;
 
         if (path_equal(switch_root, "/"))
                 return 0;
@@ -1190,6 +1191,8 @@ static int do_switch_root(const char *switch_root) {
                 goto fail;
         }
 
+        remove_old_root = in_initrd();
+
         for (i = 0; umounts[i] != NULL; i++) {
                 char newmount[PATH_MAX];
 
@@ -1215,7 +1218,8 @@ static int do_switch_root(const char *switch_root) {
                 goto fail;
         }
 
-        cfd = open("/", O_RDONLY);
+        if (remove_old_root)
+                cfd = open("/", O_RDONLY);
 
         if (mount(switch_root, "/", NULL, MS_MOVE, NULL) < 0) {
                 r = -errno;

commit d03bc1b814b853497120c35a9a8d6a66925963ff
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 14:22:42 2012 +0200

    core/main.c: add "--switchedroot" parameter
    
    If systemd serializes from a switch_root, it adds "--switchedroot" to
    the systemd in the real root.
    
    If "--switchedroot" is found, then we do not skip all the stuff, which
    is skipped for normal rexecs.

diff --git a/src/core/main.c b/src/core/main.c
index ea06dc8..7471214 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -746,6 +746,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_SHOW_STATUS,
                 ARG_SYSV_CONSOLE,
                 ARG_DESERIALIZE,
+                ARG_SWITCHEDROOT,
                 ARG_INTROSPECT,
                 ARG_DEFAULT_STD_OUTPUT,
                 ARG_DEFAULT_STD_ERROR
@@ -770,6 +771,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "sysv-console",             optional_argument, NULL, ARG_SYSV_CONSOLE             },
 #endif
                 { "deserialize",              required_argument, NULL, ARG_DESERIALIZE              },
+                { "switchedroot",             no_argument,       NULL, ARG_SWITCHEDROOT             },
                 { "introspect",               optional_argument, NULL, ARG_INTROSPECT               },
                 { "default-standard-output",  required_argument, NULL, ARG_DEFAULT_STD_OUTPUT,      },
                 { "default-standard-error",   required_argument, NULL, ARG_DEFAULT_STD_ERROR,       },
@@ -941,6 +943,10 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
                 }
 
+                case ARG_SWITCHEDROOT:
+                        /* Nothing special yet */
+                        break;
+
                 case ARG_INTROSPECT: {
                         const char * const * i = NULL;
 
@@ -1278,6 +1284,13 @@ int main(int argc, char *argv[]) {
                         break;
                 }
 
+        /* If we have switched root, do all the special things */
+        for (j = 1; j < argc; j++)
+                if (streq(argv[j], "--switchedroot")) {
+                        is_reexec = false;
+                        break;
+                }
+
         /* If we get started via the /sbin/init symlink then we are
            called 'init'. After a subsequent reexecution we are then
            called 'systemd'. That is confusing, hence let's call us
@@ -1693,7 +1706,7 @@ finish:
                 if (switch_root)
                         do_switch_root(switch_root);
 
-                args_size = MAX(5, argc+1);
+                args_size = MAX(6, argc+1);
                 args = newa(const char*, args_size);
 
                 if (!switch_root_init) {
@@ -1712,6 +1725,8 @@ finish:
 
                         i = 0;
                         args[i++] = SYSTEMD_BINARY_PATH;
+                        if (switch_root)
+                                args[i++] = "--switchedroot";
                         args[i++] = arg_running_as == MANAGER_SYSTEM ? "--system" : "--user";
                         args[i++] = "--deserialize";
                         args[i++] = sfd;

commit f38ed060600d124a68332694ab497c344bd8e089
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 14:22:41 2012 +0200

    manager: only serialize the timestamps for the initramfs if in_initrd()

diff --git a/src/core/manager.c b/src/core/manager.c
index f8fb8a2..3ccb392 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -1735,8 +1735,11 @@ int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
         fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
 
         dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
-        dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
-        dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
+
+        if (! in_initrd()) {
+                dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
+                dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
+        }
 
         fputc('\n', f);
 

commit 9be346c94eab9de0edc30f08b6c6d070c3a17c79
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 14:22:40 2012 +0200

    util.c: add in_initrd()
    
    in_initrd() checks, if the stat() for the device for "/" is 1, which it
    is for the initramfs.

diff --git a/src/shared/util.c b/src/shared/util.c
index 5318540..681484b 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -5639,3 +5639,18 @@ bool is_valid_documentation_url(const char *url) {
 
         return false;
 }
+
+bool in_initrd(void) {
+        static bool checked=false;
+        static bool is_in_initrd=false;
+
+        if (!checked) {
+                struct stat sb;
+                if (stat("/", &sb) == 0) {
+                        is_in_initrd = (sb.st_dev == 1);
+                        checked = true;
+                }
+        }
+
+        return is_in_initrd;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 0af0299..4fccf96 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -512,4 +512,5 @@ int can_sleep(const char *type);
 
 bool is_valid_documentation_url(const char *url);
 
+bool in_initrd(void);
 #endif

commit f67cc036ba92a3c71acb664ed2d548de5827cf1f
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed May 16 14:22:39 2012 +0200

    main: corrected do_switch_root()
    
    do_switch_root now mount moves "/dev", "/proc", "/sys", "/run" and
    removes the old root recursively.

diff --git a/src/core/main.c b/src/core/main.c
index a51d1be..ea06dc8 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1168,30 +1168,73 @@ static void test_cgroups(void) {
 }
 
 static int do_switch_root(const char *switch_root) {
-        int r;
+        int r=0;
+        /*  Don't try to unmount the old "/", there's no way to do it. */
+        const char *umounts[] = { "/dev", "/proc", "/sys", "/run", NULL };
+        int i;
+        int cfd = -1;
+        struct stat switch_root_stat, sb;
 
         if (path_equal(switch_root, "/"))
                 return 0;
 
-        if (chdir(switch_root) < 0) {
+        if (stat(switch_root, &switch_root_stat) != 0) {
                 r = -errno;
+                log_error("failed to stat directory %s", switch_root);
                 goto fail;
         }
 
+        for (i = 0; umounts[i] != NULL; i++) {
+                char newmount[PATH_MAX];
+
+                snprintf(newmount, sizeof(newmount), "%s%s", switch_root, umounts[i]);
+
+                if ((stat(newmount, &sb) != 0) || (sb.st_dev != switch_root_stat.st_dev)) {
+                        /* mount point seems to be mounted already or stat failed */
+                        umount2(umounts[i], MNT_DETACH);
+                        continue;
+                }
+
+                if (mount(umounts[i], newmount, NULL, MS_MOVE, NULL) < 0) {
+                        log_error("failed to mount moving %s to %s",
+                                  umounts[i], newmount);
+                        log_error("forcing unmount of %s", umounts[i]);
+                        umount2(umounts[i], MNT_FORCE);
+                }
+        }
+
+        if (chdir(switch_root)) {
+                r = -errno;
+                log_error("failed to change directory to %s", switch_root);
+                goto fail;
+        }
+
+        cfd = open("/", O_RDONLY);
+
         if (mount(switch_root, "/", NULL, MS_MOVE, NULL) < 0) {
                 r = -errno;
-                chdir("/");
+                log_error("failed to mount moving %s to /", switch_root);
                 goto fail;
         }
 
-        if (chroot(".") < 0)
-                log_warning("Failed to change root, ignoring: %m");
+        if (chroot(".")) {
+                r = -errno;
+                log_error("failed to change root");
+                goto fail;
+        }
 
-        /* FIXME: remove old root */
+        if (cfd >= 0) {
+                rm_rf_children(cfd, false, false);
+                close(cfd);
+                cfd=-1;
+        }
 
         return 0;
 
 fail:
+        if (cfd >= 0)
+                close(cfd);
+
         log_error("Failed to switch root, ignoring: %s", strerror(-r));
 
         return r;
diff --git a/src/shared/util.c b/src/shared/util.c
index 0234f3b..5318540 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -3139,7 +3139,7 @@ int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
         return 0;
 }
 
-static int rm_rf_children(int fd, bool only_dirs, bool honour_sticky) {
+int rm_rf_children(int fd, bool only_dirs, bool honour_sticky) {
         DIR *d;
         int ret = 0;
 
diff --git a/src/shared/util.h b/src/shared/util.h
index 49756dc..0af0299 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -349,6 +349,7 @@ int get_ctty(pid_t, dev_t *_devnr, char **r);
 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
 int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
 
+int rm_rf_children(int fd, bool only_dirs, bool honour_sticky);
 int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky);
 
 int pipe_eof(int fd);



More information about the systemd-commits mailing list