]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: when switching root remove /run/systemd before executing the binary specified...
authorFranck Bui <fbui@suse.com>
Wed, 17 Jul 2024 09:02:03 +0000 (11:02 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 22 Jul 2024 10:29:08 +0000 (11:29 +0100)
It's important if the binary specified by the init= boot option is not systemd
otherwise it confuses systemctl that incorrectly assumes that systemd is still
the init system due to the presence of /run/systemd/system.

Also some tools might also check the presence of /run/systemd/private to test
if systemd is running as pid1.

src/core/main.c

index e5c2dc914e53e2813665d078097395d554fe3a48..6234eff7e5486f27eb1d3450fa217d1aae776eeb 100644 (file)
@@ -81,6 +81,7 @@
 #include "psi-util.h"
 #include "random-util.h"
 #include "rlimit-util.h"
+#include "rm-rf.h"
 #include "seccomp-util.h"
 #include "selinux-setup.h"
 #include "selinux-util.h"
@@ -2052,6 +2053,17 @@ static int do_reexecute(
         arg_serialization = safe_fclose(arg_serialization);
         fds = fdset_free(fds);
 
+        /* Drop /run/systemd directory. Some of its content can be used as a flag indicating that systemd is
+         * the init system but we might be replacing it with something different. If systemd is used again it
+         * will recreate the directory and its content anyway. */
+        r = rm_rf("/run/systemd.pre-switch-root", REMOVE_ROOT|REMOVE_MISSING_OK);
+        if (r < 0)
+                log_warning_errno(r, "Failed to prepare /run/systemd.pre-switch-root/, ignoring: %m");
+
+        r = RET_NERRNO(rename("/run/systemd", "/run/systemd.pre-switch-root"));
+        if (r < 0)
+                log_warning_errno(r, "Failed to move /run/systemd/ to /run/systemd.pre-switch-root/, ignoring: %m");
+
         /* Reopen the console */
         (void) make_console_stdio();