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.
This commit is contained in:
Colin Walters 2023-07-17 13:48:35 -04:00
parent 1c0fd7d458
commit 6966979c5e
3 changed files with 27 additions and 35 deletions

View 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");

View 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

View 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