prepare-root: Link to glib
authorColin Walters <walters@verbum.org>
Wed, 21 Jun 2023 17:25:52 +0000 (13:25 -0400)
committerColin Walters <walters@verbum.org>
Fri, 30 Jun 2023 09:18:23 +0000 (05:18 -0400)
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
src/switchroot/ostree-prepare-root.c

index 60bfc0ede8cd381802e7524a539639a4f8164a6a..09f954f38139b31926fcbb7773737eff2e046752 100644 (file)
@@ -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
index 40f62f868d4fa351b0e0337fd06747b0c700d1e8..ab8bb14227293dfb7ecdbf1366b59f85b81ae963 100644 (file)
 
 #include "config.h"
 
-#include <assert.h>
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <libglnx.h>
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -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 *