From 6966979c5e3a216fccfb3312e11cb959ac4e4b30 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 17 Jul 2023 13:48:35 -0400 Subject: [PATCH] generator: Deduplicate ostree= karg parsing Avoid having two copies of a regular expression for parsing the `ostree=` kernel argument. Because the `ostree-system-generator` binary already has access to the internals because it's implemented in the shared library, expose the sysroot version internally and use that. Motivated by an attempt to change one of these copies but not the other. --- src/libostree/ostree-impl-system-generator.c | 36 +++++--------------- src/libostree/ostree-sysroot-private.h | 4 +++ src/libostree/ostree-sysroot.c | 22 +++++++----- 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/libostree/ostree-impl-system-generator.c b/src/libostree/ostree-impl-system-generator.c index 8404d189..33c78b65 100644 --- a/src/libostree/ostree-impl-system-generator.c +++ b/src/libostree/ostree-impl-system-generator.c @@ -32,6 +32,7 @@ #include "ostree-cmd-private.h" #include "ostree-core-private.h" +#include "ostree-sysroot-private.h" #include "ostree.h" #ifdef HAVE_LIBMOUNT @@ -85,29 +86,6 @@ path_kill_slashes (char *path) return path; } -/* Written by ostree-sysroot-deploy.c. We parse out the stateroot here since we - * need to know it to mount /var. Unfortunately we can't easily use the - * libostree API to find the booted deployment since /boot might not have been - * mounted yet. - */ -static char * -stateroot_from_ostree_cmdline (const char *ostree_cmdline, GError **error) -{ - static GRegex *regex; - static gsize regex_initialized; - if (g_once_init_enter (®ex_initialized)) - { - regex = g_regex_new ("^/ostree/boot.[01]/([^/]+)/", 0, 0, NULL); - g_assert (regex); - g_once_init_leave (®ex_initialized, 1); - } - - g_autoptr (GMatchInfo) match = NULL; - if (!g_regex_match (regex, ostree_cmdline, 0, &match)) - return glnx_null_throw (error, "Failed to parse %s", ostree_cmdline); - - return g_match_info_fetch (match, 1); -} #endif /* Forcibly enable our internal units, since we detected ostree= on the kernel cmdline */ @@ -159,10 +137,14 @@ fstab_generator (const char *ostree_cmdline, const char *normal_dir, const char static const char fstab_path[] = "/etc/fstab"; static const char var_path[] = "/var"; - /* ostree-prepare-root was patched to write the stateroot to this file */ - g_autofree char *stateroot = stateroot_from_ostree_cmdline (ostree_cmdline, error); - if (!stateroot) - return FALSE; + /* Written by ostree-sysroot-deploy.c. We parse out the stateroot here since we + * need to know it to mount /var. Unfortunately we can't easily use the + * libostree API to find the booted deployment since /boot might not have been + * mounted yet. + */ + g_autofree char *stateroot = NULL; + if (!_ostree_sysroot_parse_bootlink (ostree_cmdline, NULL, &stateroot, NULL, NULL, error)) + return glnx_prefix_error (error, "Parsing stateroot"); /* Load /etc/fstab if it exists, and look for a /var mount */ g_autoptr (OtLibMountFile) fstab = setmntent (fstab_path, "re"); diff --git a/src/libostree/ostree-sysroot-private.h b/src/libostree/ostree-sysroot-private.h index 78e736fc..63a29418 100644 --- a/src/libostree/ostree-sysroot-private.h +++ b/src/libostree/ostree-sysroot-private.h @@ -167,4 +167,8 @@ gboolean _ostree_sysroot_parse_bootdir_name (const char *name, char **out_osname gboolean _ostree_sysroot_list_all_boot_directories (OstreeSysroot *self, char ***out_bootdirs, GCancellable *cancellable, GError **error); +gboolean _ostree_sysroot_parse_bootlink (const char *bootlink, int *out_entry_bootversion, + char **out_osname, char **out_bootcsum, + int *out_treebootserial, GError **error); + G_END_DECLS diff --git a/src/libostree/ostree-sysroot.c b/src/libostree/ostree-sysroot.c index d786cc5a..68dddf4e 100644 --- a/src/libostree/ostree-sysroot.c +++ b/src/libostree/ostree-sysroot.c @@ -720,9 +720,10 @@ load_origin (OstreeSysroot *self, OstreeDeployment *deployment, GCancellable *ca return TRUE; } -static gboolean -parse_bootlink (const char *bootlink, int *out_entry_bootversion, char **out_osname, - char **out_bootcsum, int *out_treebootserial, GError **error) +// Parse the kernel argument ostree= +gboolean +_ostree_sysroot_parse_bootlink (const char *bootlink, int *out_entry_bootversion, char **out_osname, + char **out_bootcsum, int *out_treebootserial, GError **error) { static gsize regex_initialized; static GRegex *regex; @@ -742,10 +743,14 @@ parse_bootlink (const char *bootlink, int *out_entry_bootversion, char **out_osn g_autofree char *bootversion_str = g_match_info_fetch (match, 1); g_autofree char *treebootserial_str = g_match_info_fetch (match, 4); - *out_entry_bootversion = (int)g_ascii_strtoll (bootversion_str, NULL, 10); - *out_osname = g_match_info_fetch (match, 2); - *out_bootcsum = g_match_info_fetch (match, 3); - *out_treebootserial = (int)g_ascii_strtoll (treebootserial_str, NULL, 10); + if (out_entry_bootversion) + *out_entry_bootversion = (int)g_ascii_strtoll (bootversion_str, NULL, 10); + if (out_osname) + *out_osname = g_match_info_fetch (match, 2); + if (out_bootcsum) + *out_bootcsum = g_match_info_fetch (match, 3); + if (out_treebootserial) + *out_treebootserial = (int)g_ascii_strtoll (treebootserial_str, NULL, 10); return TRUE; } @@ -768,7 +773,8 @@ parse_deployment (OstreeSysroot *self, const char *boot_link, OstreeDeployment * g_autofree char *osname = NULL; g_autofree char *bootcsum = NULL; int treebootserial = -1; - if (!parse_bootlink (boot_link, &entry_boot_version, &osname, &bootcsum, &treebootserial, error)) + if (!_ostree_sysroot_parse_bootlink (boot_link, &entry_boot_version, &osname, &bootcsum, + &treebootserial, error)) return FALSE; g_autofree char *errprefix -- 2.30.2