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.
This commit is contained in:
Colin Walters 2023-06-21 13:25:52 -04:00
parent d6799ecc24
commit 875915f6c9
2 changed files with 11 additions and 35 deletions

View 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

View File

@ -52,11 +52,11 @@
#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 *