From 875915f6c977d359ca1e3e818a1b3b8286f6b6bc Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 21 Jun 2023 13:25:52 -0400 Subject: [PATCH] prepare-root: Link to glib Since we've split off the "prepare root as init" code into a separate file, we can now use glib to parse the config file again, which is a lot less hacky. This is particularly motivated by composefs, where we want to do more in the initramfs. Future patches may also link to parts of libostree. --- Makefile-switchroot.am | 5 ++-- src/switchroot/ostree-prepare-root.c | 41 ++++++---------------------- 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/Makefile-switchroot.am b/Makefile-switchroot.am index 60bfc0ed..09f954f3 100644 --- a/Makefile-switchroot.am +++ b/Makefile-switchroot.am @@ -50,8 +50,9 @@ CLEANFILES += ostree-prepare-root else ostree_boot_PROGRAMS += ostree-prepare-root ostree_prepare_root_CFLAGS += $(AM_CFLAGS) -Isrc/switchroot -I$(srcdir)/composefs -else ostree_prepare_root_SOURCES += src/switchroot/ostree-prepare-root.c +ostree_prepare_root_CPPFLAGS += $(OT_INTERNAL_GIO_UNIX_CFLAGS) -I $(srcdir)/libglnx +ostree_prepare_root_LDADD += $(AM_LDFLAGS) $(OT_INTERNAL_GIO_UNIX_LIBS) libglnx.la endif # BUILDOPT_USE_STATIC_COMPILER @@ -68,7 +69,7 @@ endif if BUILDOPT_SYSTEMD ostree_prepare_root_CPPFLAGS += -DHAVE_SYSTEMD=1 -ostree_prepare_root_LDADD += $(AM_LDFLAGS) $(LIBSYSTEMD_LIBS) +ostree_prepare_root_LDADD += $(LIBSYSTEMD_LIBS) endif # This is the "new mode" of using a generator for /var; see diff --git a/src/switchroot/ostree-prepare-root.c b/src/switchroot/ostree-prepare-root.c index 40f62f86..ab8bb142 100644 --- a/src/switchroot/ostree-prepare-root.c +++ b/src/switchroot/ostree-prepare-root.c @@ -52,11 +52,11 @@ #include "config.h" -#include #include #include #include #include +#include #include #include #include @@ -106,44 +106,19 @@ typedef enum OSTREE_COMPOSEFS_MODE_DIGEST, /* Always use and require specific digest */ } OstreeComposefsMode; -static inline bool +static bool sysroot_is_configured_ro (const char *sysroot) { - char *config_path = NULL; - assert (asprintf (&config_path, "%s/ostree/repo/config", sysroot) != -1); - FILE *f = fopen (config_path, "r"); - if (!f) + g_autoptr (GError) local_error = NULL; + g_autofree char *repo_config_path = g_build_filename (sysroot, "ostree/repo/config", NULL); + g_autoptr (GKeyFile) repo_config = g_key_file_new (); + if (!g_key_file_load_from_file (repo_config, repo_config_path, G_KEY_FILE_NONE, &local_error)) { - fprintf (stderr, "Missing expected repo config: %s\n", config_path); - free (config_path); + g_printerr ("Failed to load %s: %s", repo_config_path, local_error->message); return false; } - free (config_path); - - bool ret = false; - char *line = NULL; - size_t len = 0; - /* Note getline() will reuse the previous buffer */ - bool in_sysroot = false; - while (getline (&line, &len, f) != -1) - { - /* This is an awful hack to avoid depending on GLib in the - * initramfs right now. - */ - if (strstr (line, "[sysroot]") == line) - in_sysroot = true; - else if (*line == '[') - in_sysroot = false; - else if (in_sysroot && strstr (line, "readonly=true") == line) - { - ret = true; - break; - } - } - fclose (f); - free (line); - return ret; + return g_key_file_get_boolean (repo_config, "sysroot", "readonly", NULL); } static char * -- 2.30.2