generator: Deduplicate ostree= karg parsing
authorColin Walters <walters@verbum.org>
Mon, 17 Jul 2023 17:48:35 +0000 (13:48 -0400)
committerColin Walters <walters@verbum.org>
Tue, 18 Jul 2023 12:29:09 +0000 (08:29 -0400)
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
src/libostree/ostree-sysroot-private.h
src/libostree/ostree-sysroot.c

index 8404d189719933b0a26b962bd8e53c2e4c436d10..33c78b65b7696447b0419d98e07c754f70417709 100644 (file)
@@ -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 (&regex_initialized))
-    {
-      regex = g_regex_new ("^/ostree/boot.[01]/([^/]+)/", 0, 0, NULL);
-      g_assert (regex);
-      g_once_init_leave (&regex_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");
index 78e736fca8fcbcfbcf8aaad67a9ec133bd5809a1..63a294188c53edf2afbbf7276f0ef7599f4b4c7f 100644 (file)
@@ -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
index d786cc5a2aa96d161424267746616e07c98f2a3b..68dddf4ede2bbc367fa2c28bdbc3540bfd8a1a6e 100644 (file)
@@ -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