From bf974e32fcd2b484ae440891a499a7d353489483 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Thu, 13 Apr 2023 17:22:40 -0400 Subject: lib/sysroot-cleanup: Factor out bootfs cleanup 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 | 43 +++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/libostree/ostree-sysroot-cleanup.c b/src/libostree/ostree-sysroot-cleanup.c index d8347f45..2fc2ffb4 100644 --- a/src/libostree/ostree-sysroot-cleanup.c +++ b/src/libostree/ostree-sysroot-cleanup.c @@ -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, -- cgit v1.2.1