switch-root: introduce SwitchRootFlags flags parameter to switch_root()
authorLennart Poettering <lennart@poettering.net>
Fri, 19 May 2023 13:48:12 +0000 (15:48 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 2 Jun 2023 14:49:39 +0000 (16:49 +0200)
Let's replace the current boolean param with a proper flags param. With
a single flag this doesn't appear to make much sense, though it does
already make things more readable I think.

However, once we add a second flag, it starts to make more sense.

Also, while we are at it, condition the "istmp" determinaton with this
flag too, since we only need it when the flag is set.

src/core/main.c
src/shared/switch-root.c
src/shared/switch-root.h
src/shutdown/shutdown.c

index 28cfca25b3effa6a062efd5e122b8ac2a5aa580a..f067b13fff876057b5e70cbf1da39a5973d1390a 100644 (file)
@@ -1840,7 +1840,7 @@ static int do_reexecute(
         if (switch_root_dir) {
                 r = switch_root(/* new_root= */ switch_root_dir,
                                 /* old_root_after= */ NULL,
-                                /* destroy_old_root= */ objective == MANAGER_SWITCH_ROOT);
+                                /* flags= */ objective == MANAGER_SWITCH_ROOT ? SWITCH_ROOT_DESTROY_OLD_ROOT : 0);
                 if (r < 0)
                         log_error_errno(r, "Failed to switch root, trying to continue: %m");
         }
index 2ea65eafaf1dd52bdae5a65250db65c4b8e0dc44..333118f75fc7bd79a3cb23863246eda49a4b7598 100644 (file)
@@ -28,7 +28,7 @@
 
 int switch_root(const char *new_root,
                 const char *old_root_after,   /* path below the new root, where to place the old root after the transition; may be NULL to unmount it */
-                bool destroy_old_root) {
+                SwitchRootFlags flags) {
 
         struct {
                 const char *path;
@@ -66,11 +66,14 @@ int switch_root(const char *new_root,
                 return 0;
         }
 
-        istmp = fd_is_temporary_fs(old_root_fd);
-        if (istmp < 0)
-                return log_error_errno(istmp, "Failed to stat root directory: %m");
-        if (istmp > 0)
-                log_debug("Root directory is on tmpfs, will do cleanup later.");
+        if (FLAGS_SET(flags, SWITCH_ROOT_DESTROY_OLD_ROOT)) {
+                istmp = fd_is_temporary_fs(old_root_fd);
+                if (istmp < 0)
+                        return log_error_errno(istmp, "Failed to stat root directory: %m");
+                if (istmp > 0)
+                        log_debug("Root directory is on tmpfs, will do cleanup later.");
+        } else
+                istmp = -1; /* don't know */
 
         if (old_root_after) {
                 /* Determine where we shall place the old root after the transition */
@@ -160,7 +163,7 @@ int switch_root(const char *new_root,
                         return log_error_errno(errno, "Failed to change directory: %m");
         }
 
-        if (istmp && destroy_old_root) {
+        if (istmp > 0) {
                 struct stat rb;
 
                 if (fstat(old_root_fd, &rb) < 0)
index 9882d2ef91ef35c9568dbd5f88b0b3dd149fdf06..059b4603ea5dc12a8bbfef928367ba0d65aef32e 100644 (file)
@@ -3,4 +3,8 @@
 
 #include <stdbool.h>
 
-int switch_root(const char *new_root, const char *old_root_after, bool destroy_old_root);
+typedef enum SwitchRootFlags {
+        SWITCH_ROOT_DESTROY_OLD_ROOT = 1 << 0, /* rm -rf old root when switching – under the condition that it is backed by non-persistent tmpfs/ramfs/… */
+} SwitchRootFlags;
+
+int switch_root(const char *new_root, const char *old_root_after, SwitchRootFlags flags);
index bd6ded5529ca43ed779f9633ca8c9a35079d582e..be4adb79d578fcd9a7bef83460eac64b0b9a5c6a 100644 (file)
@@ -169,7 +169,7 @@ static int switch_root_initramfs(void) {
         return switch_root(
                         /* new_root= */ "/run/initramfs",
                         /* old_root_after= */ "/oldroot",
-                        /* destroy_old_root= */ false);
+                        /* flags= */ 0);
 }
 
 /* Read the following fields from /proc/meminfo: