remount: Use new metadata in `/run/ostree-booted` for composefs
authorColin Walters <walters@verbum.org>
Thu, 13 Jul 2023 11:47:32 +0000 (07:47 -0400)
committerColin Walters <walters@verbum.org>
Thu, 13 Jul 2023 11:57:18 +0000 (07:57 -0400)
Since we now have a generalized more structured way of serializing
state in the initramfs instead of "stamp files", use it for
passing the composefs state.

Makefile-switchroot.am
src/switchroot/ostree-prepare-root.c
src/switchroot/ostree-remount.c

index 6b7cca08deee39bcc7db2ee63bccd376604908ca..71a3cbda57deafe0bcba0d357f2c2a4d0214a3cb 100644 (file)
@@ -60,8 +60,8 @@ ostree_remount_SOURCES = \
     src/switchroot/ostree-mount-util.h \
     src/switchroot/ostree-remount.c \
     $(NULL)
-ostree_remount_CPPFLAGS = $(AM_CPPFLAGS) $(OT_INTERNAL_GIO_UNIX_CFLAGS) -Isrc/switchroot -I$(srcdir)/libglnx
-ostree_remount_LDADD = $(AM_LDFLAGS) $(OT_INTERNAL_GIO_UNIX_LIBS) libglnx.la
+ostree_remount_CPPFLAGS = $(AM_CPPFLAGS) $(OT_INTERNAL_GIO_UNIX_CFLAGS) -Isrc/switchroot -I$(srcdir)/src/libotcore -I$(srcdir)/src/libotutil -I$(srcdir)/libglnx
+ostree_remount_LDADD = $(AM_LDFLAGS) $(OT_INTERNAL_GIO_UNIX_LIBS) libotcore.la libotutil.la libglnx.la
 
 if USE_COMPOSEFS
 ostree_prepare_root_LDADD += libcomposefs.la
index 696ace271f45c820728c1307ddde930bf316f117..bbb207161c7bce32d5b495e2ea85c282a2024921 100644 (file)
@@ -400,11 +400,6 @@ main (int argc, char *argv[])
 
       if (lcfs_mount_image (OSTREE_COMPOSEFS_NAME, TMP_SYSROOT, &cfs_options) == 0)
         {
-          int fd = open (_OSTREE_COMPOSEFS_ROOT_STAMP, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
-          if (fd < 0)
-            err (EXIT_FAILURE, "failed to create %s", _OSTREE_COMPOSEFS_ROOT_STAMP);
-          (void)close (fd);
-
           using_composefs = 1;
           g_variant_builder_add (&metadata_builder, "{sv}", OTCORE_RUN_BOOTED_KEY_COMPOSEFS,
                                  g_variant_new_boolean (true));
index 2563d08bb16d9e2085f6c16c5f35fd962287b36a..38af0889050385a6acace56b977aa237115e3c73 100644 (file)
 #include <sys/statvfs.h>
 #include <unistd.h>
 
-#include <glib.h>
-
-#include "glnx-backport-autocleanups.h"
 #include "ostree-mount-util.h"
+#include "otcore.h"
 
 static void
 do_remount (const char *target, bool writable)
@@ -81,15 +79,36 @@ do_remount (const char *target, bool writable)
 int
 main (int argc, char *argv[])
 {
-  /* We really expect that nowadays that everything is done in the initramfs,
-   * but historically we created this file here, so we'll continue to do be
-   * sure here it exists.  This code should be removed at some point though.
-   */
+  g_autoptr (GError) error = NULL;
+  g_autoptr (GVariant) ostree_run_metadata_v = NULL;
   {
-    int fd = open (OSTREE_PATH_BOOTED, O_EXCL | O_CREAT | O_WRONLY | O_NOCTTY | O_CLOEXEC, 0640);
-    if (fd != -1)
-      (void)close (fd);
+    glnx_autofd int fd = open (OTCORE_RUN_BOOTED, O_RDONLY | O_CLOEXEC);
+    if (fd < 0)
+      {
+        /* We really expect that nowadays that everything is done in the initramfs,
+         * but historically we created this file here, so we'll continue to do be
+         * sure here it exists.  This code should be removed at some point though.
+         */
+        if (errno == ENOENT)
+          {
+            int subfd = open (OTCORE_RUN_BOOTED, O_EXCL | O_CREAT | O_WRONLY | O_NOCTTY | O_CLOEXEC,
+                              0640);
+            if (subfd != -1)
+              (void)close (subfd);
+          }
+        else
+          {
+            err (EXIT_FAILURE, "failed to open %s", OTCORE_RUN_BOOTED);
+          }
+      }
+    else
+      {
+        if (!ot_variant_read_fd (fd, 0, G_VARIANT_TYPE_VARDICT, TRUE, &ostree_run_metadata_v,
+                                 &error))
+          errx (EXIT_FAILURE, "failed to read %s: %s", OTCORE_RUN_BOOTED, error->message);
+      }
   }
+  g_autoptr (GVariantDict) ostree_run_metadata = g_variant_dict_new (ostree_run_metadata_v);
 
   /* The /sysroot mount needs to be private to avoid having a mount for e.g. /var/cache
    * also propagate to /sysroot/ostree/deploy/$stateroot/var/cache
@@ -100,10 +119,9 @@ main (int argc, char *argv[])
   if (mount ("none", "/sysroot", NULL, MS_REC | MS_PRIVATE, NULL) < 0)
     perror ("warning: While remounting /sysroot MS_PRIVATE");
 
-  bool root_is_composefs = false;
-  struct stat stbuf;
-  if (fstatat (AT_FDCWD, _OSTREE_COMPOSEFS_ROOT_STAMP, &stbuf, 0) == 0)
-    root_is_composefs = true;
+  gboolean root_is_composefs = FALSE;
+  g_variant_dict_lookup (ostree_run_metadata, OTCORE_RUN_BOOTED_KEY_COMPOSEFS, "b",
+                         &root_is_composefs);
 
   if (path_is_on_readonly_fs ("/") && !root_is_composefs)
     {