lib/sysroot-cleanup: Factor out bootfs cleanup
authorJonathan Lebon <jonathan@jlebon.com>
Thu, 13 Apr 2023 21:22:40 +0000 (17:22 -0400)
committerJonathan Lebon <jonathan@jlebon.com>
Fri, 14 Apr 2023 13:57:16 +0000 (09:57 -0400)
Crawling through the bootfs and the deployment dirs was already mostly
separate. The only inefficiency here is that we now iterate over the
array of active deployments twice when building the hash tables. No
functional change otherwise.

Prep for future patch.

src/libostree/ostree-sysroot-cleanup.c

index d8347f45842796ae7a6e0e28bed80515c1ed221c..2fc2ffb4b2e2094f9f917fd1c0d16fad8b3e949b 100644 (file)
@@ -260,23 +260,12 @@ cleanup_old_deployments (OstreeSysroot       *self,
   /* Load all active deployments referenced by bootloader configuration. */
   g_autoptr(GHashTable) active_deployment_dirs =
     g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-  g_autoptr(GHashTable) active_boot_checksums =
-    g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-  g_autoptr(GHashTable) active_overlay_initrds =
-    g_hash_table_new (g_str_hash, g_str_equal); /* borrows from deployment's bootconfig */
   for (guint i = 0; i < self->deployments->len; i++)
     {
       OstreeDeployment *deployment = self->deployments->pdata[i];
       char *deployment_path = ostree_sysroot_get_deployment_dirpath (self, deployment);
-      char *bootcsum = g_strdup (ostree_deployment_get_bootcsum (deployment));
       /* Transfer ownership */
       g_hash_table_replace (active_deployment_dirs, deployment_path, deployment_path);
-      g_hash_table_replace (active_boot_checksums, bootcsum, bootcsum);
-
-      OstreeBootconfigParser *bootconfig = ostree_deployment_get_bootconfig (deployment);
-      char **initrds = ostree_bootconfig_parser_get_overlay_initrds (bootconfig);
-      for (char **it = initrds; it && *it; it++)
-        g_hash_table_add (active_overlay_initrds, (char*)glnx_basename (*it));
     }
 
   /* Find all deployment directories, both active and inactive */
@@ -297,6 +286,35 @@ cleanup_old_deployments (OstreeSysroot       *self,
         return FALSE;
     }
 
+  return TRUE;
+}
+
+/* This function deletes any files in the bootfs unreferenced by the active
+ * bootloader configuration.
+ */
+static gboolean
+cleanup_bootfs (OstreeSysroot       *self,
+                GCancellable        *cancellable,
+                GError             **error)
+{
+  /* Load all active bootcsums and overlays referenced by bootloader configuration. */
+  g_autoptr(GHashTable) active_boot_checksums =
+    g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+  g_autoptr(GHashTable) active_overlay_initrds =
+    g_hash_table_new (g_str_hash, g_str_equal); /* borrows from deployment's bootconfig */
+  for (guint i = 0; i < self->deployments->len; i++)
+    {
+      OstreeDeployment *deployment = self->deployments->pdata[i];
+      char *bootcsum = g_strdup (ostree_deployment_get_bootcsum (deployment));
+      /* Transfer ownership */
+      g_hash_table_replace (active_boot_checksums, bootcsum, bootcsum);
+
+      OstreeBootconfigParser *bootconfig = ostree_deployment_get_bootconfig (deployment);
+      char **initrds = ostree_bootconfig_parser_get_overlay_initrds (bootconfig);
+      for (char **it = initrds; it && *it; it++)
+        g_hash_table_add (active_overlay_initrds, (char*)glnx_basename (*it));
+    }
+
   /* Clean up boot directories */
   g_auto(GStrv) all_boot_dirs = NULL;
   if (!_ostree_sysroot_list_all_boot_directories (self, &all_boot_dirs, cancellable, error))
@@ -554,6 +572,9 @@ _ostree_sysroot_cleanup_internal (OstreeSysroot              *self,
   if (!cleanup_old_deployments (self, cancellable, error))
     return glnx_prefix_error (error, "Cleaning deployments");
 
+  if (!cleanup_bootfs (self, cancellable, error))
+    return glnx_prefix_error (error, "Cleaning bootfs");
+
   OstreeRepo *repo = ostree_sysroot_repo (self);
   if (!generate_deployment_refs (self, repo,
                                  self->bootversion,