]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
gpt-auto: add common parse_gpt_auto_root() parser
authorLennart Poettering <lennart@poettering.net>
Mon, 3 Mar 2025 13:35:29 +0000 (14:35 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 5 Mar 2025 11:38:33 +0000 (12:38 +0100)
src/fstab-generator/fstab-generator.c
src/gpt-auto-generator/gpt-auto-generator.c
src/shared/generator.c
src/shared/generator.h

index 7e85d476e4dd3de322c5ddb9e9c8d9854914f279..7e84be499d7a079c5b7fe0fdcd2f9b2b0adffae3 100644 (file)
@@ -1119,7 +1119,7 @@ static bool validate_root_or_usr_mount_source(const char *what, const char *swit
                 return false;
         }
 
-        if (STR_IN_SET(what, "gpt-auto", "gpt-auto-force")) {
+        if (parse_gpt_auto_root(what) > 0) {
                 /* This is handled by gpt-auto-generator */
                 log_debug("Skipping %s directory handling, as gpt-auto was requested.", switch_name);
                 return false;
index 6c2fbe178155a68bf6fe154306489fefeb17d9c0..c6727523f00b4a6a9bedc013e02e41ebaa9e3528 100644 (file)
 #include "unit-name.h"
 #include "virt.h"
 
-typedef enum GptAutoRoot {
-        GPT_AUTO_ROOT_UNSPECIFIED,  /* no root= specified */
-        GPT_AUTO_ROOT_OFF,          /* root= set to something else */
-        GPT_AUTO_ROOT_ON,           /* root= set explicitly to "gpt-auto" */
-        GPT_AUTO_ROOT_FORCE,        /* root= set explicitly to "gpt-auto-force" → ignores factory reset mode */
-} GptAutoRoot;
-
 static const char *arg_dest = NULL;
 static bool arg_enabled = true;
-static GptAutoRoot arg_auto_root = GPT_AUTO_ROOT_UNSPECIFIED;
+static GptAutoRoot arg_auto_root = _GPT_AUTO_ROOT_INVALID;
 static bool arg_swap_enabled = true;
 static char *arg_root_fstype = NULL;
 static char *arg_root_options = NULL;
@@ -697,7 +690,7 @@ static int add_root_mount(void) {
                 return 0;
 
         /* Neither explicitly enabled nor disabled? Then decide based on the EFI partition variables to be set */
-        if (arg_auto_root == GPT_AUTO_ROOT_UNSPECIFIED) {
+        if (arg_auto_root < 0) {
                 if (!is_efi_boot()) {
                         log_debug("Not an EFI boot, not creating root mount.");
                         return 0;
@@ -948,16 +941,8 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
                 /* Disable root disk logic if there's a root= value specified (unless it happens to be
                  * "gpt-auto" or "gpt-auto-force") */
 
-                if (streq(value, "gpt-auto")) {
-                        arg_auto_root = GPT_AUTO_ROOT_ON;
-                        log_debug("Enabling root partition auto-detection (respecting factory reset mode), root= is explicitly set to 'gpt-auto'.");
-                } else if (streq(value, "gpt-auto-force")) {
-                        arg_auto_root = GPT_AUTO_ROOT_FORCE;
-                        log_debug("Enabling root partition auto-detection (ignoring factory reset mode), root= is explicitly set to 'gpt-auto-force'.");
-                } else {
-                        arg_auto_root = GPT_AUTO_ROOT_OFF;
-                        log_debug("Disabling root partition auto-detection, root= is neither unset, nor set to 'gpt-auto' or 'gpt-auto-force'.");
-                }
+                arg_auto_root = parse_gpt_auto_root(value);
+                assert(arg_auto_root >= 0);
 
         } else if (streq(key, "roothash")) {
 
index 9d7246fe02cc199c17be4d820395e5e5d640d833..f2997c1b167597c2e8e27f1023ff0a1bce324e3f 100644 (file)
@@ -1030,3 +1030,22 @@ bool generator_soft_rebooted(void) {
 
         return (cached = (u > 0));
 }
+
+GptAutoRoot parse_gpt_auto_root(const char *value) {
+        assert(value);
+
+        /* Parses the 'gpt-auto'/'gpt-auto-root' parameters to root= */
+
+        if (streq(value, "gpt-auto")) {
+                log_debug("Enabling root partition auto-detection (respecting factory reset mode), root= is explicitly set to 'gpt-auto'.");
+                return GPT_AUTO_ROOT_ON;
+        }
+
+        if (streq(value, "gpt-auto-force")) {
+                log_debug("Enabling root partition auto-detection (ignoring factory reset mode), root= is explicitly set to 'gpt-auto-force'.");
+                return GPT_AUTO_ROOT_FORCE;
+        }
+
+        log_debug("Disabling root partition auto-detection, root= is neither unset, nor set to 'gpt-auto' or 'gpt-auto-force'.");
+        return GPT_AUTO_ROOT_OFF;
+}
index 5879e34ea25dbf8bfee69695cbbaf9b5b9769f5b..4738fe90afc07632fe289e9a35a8e9952de61cac 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <stdio.h>
 
+#include "errno-list.h"
 #include "macro.h"
 #include "main-func.h"
 
@@ -114,3 +115,13 @@ bool generator_soft_rebooted(void);
                      argv[argc == 4 ? 3 : 1]),                          \
                 exit_failure_if_negative,                               \
                 exit_failure_if_negative)
+
+typedef enum GptAutoRoot {
+        GPT_AUTO_ROOT_OFF = 0,      /* root= set to something else */
+        GPT_AUTO_ROOT_ON,           /* root= set explicitly to "gpt-auto" */
+        GPT_AUTO_ROOT_FORCE,        /* root= set explicitly to "gpt-auto-force" → ignores factory reset mode */
+        _GPT_AUTO_ROOT_MAX,
+        _GPT_AUTO_ROOT_INVALID = -EINVAL,
+} GptAutoRoot;
+
+GptAutoRoot parse_gpt_auto_root(const char *value);