Skip to content

Commit 1bed68f

Browse files
Johan-Liebert1cgwalters
authored andcommitted
composefs/gc: Change function signature
Update function signature for `composefs_gc` to take in a `GCOpts` struct with named fields instead of two booleans Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent 94959e1 commit 1bed68f

5 files changed

Lines changed: 60 additions & 23 deletions

File tree

crates/lib/src/bootc_composefs/delete.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cap_std_ext::{cap_std::fs::Dir, dirext::CapStdExtDirExt};
66
use crate::{
77
bootc_composefs::{
88
boot::{BootType, get_efi_uuid_source},
9-
gc::composefs_gc,
9+
gc::{GCOpts, composefs_gc},
1010
rollback::{composefs_rollback, rename_exchange_user_cfg},
1111
status::{get_composefs_status, get_sorted_grub_uki_boot_entries},
1212
},
@@ -261,7 +261,15 @@ pub(crate) async fn delete_composefs_deployment(
261261

262262
delete_depl_boot_entries(&depl_to_del, &storage, deleting_staged)?;
263263

264-
composefs_gc(storage, booted_cfs, false, true).await?;
264+
composefs_gc(
265+
storage,
266+
booted_cfs,
267+
GCOpts {
268+
dry_run: false,
269+
prune_repo: true,
270+
},
271+
)
272+
.await?;
265273

266274
Ok(())
267275
}

crates/lib/src/bootc_composefs/finalize.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::Path;
22

33
use crate::bootc_composefs::boot::BootType;
4-
use crate::bootc_composefs::gc::composefs_gc;
4+
use crate::bootc_composefs::gc::{GCOpts, composefs_gc};
55
use crate::bootc_composefs::rollback::{rename_exchange_bls_entries, rename_exchange_user_cfg};
66
use crate::bootc_composefs::status::get_composefs_status;
77
use crate::composefs_consts::STATE_DIR_ABS;
@@ -150,7 +150,15 @@ pub(crate) async fn composefs_backend_finalize(
150150

151151
// Now that we have successfully updated bootloader entires, we can GC the unreferenced ones
152152
// We do not prune the composefs repository here though
153-
composefs_gc(storage, booted_cfs, false, false).await?;
153+
composefs_gc(
154+
storage,
155+
booted_cfs,
156+
GCOpts {
157+
dry_run: false,
158+
prune_repo: false,
159+
},
160+
)
161+
.await?;
154162

155163
Ok(())
156164
}

crates/lib/src/bootc_composefs/gc.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ fn unreferenced_boot_binaries<'a>(
192192
.collect()
193193
}
194194

195+
pub(crate) struct GCOpts {
196+
pub(crate) dry_run: bool,
197+
pub(crate) prune_repo: bool,
198+
}
199+
195200
/// 1. List all bootloader entries
196201
/// 2. List all EROFS images
197202
/// 3. List all state directories
@@ -212,8 +217,7 @@ fn unreferenced_boot_binaries<'a>(
212217
pub(crate) async fn composefs_gc(
213218
storage: &Storage,
214219
booted_cfs: &BootedComposefs,
215-
dry_run: bool,
216-
prune_repo: bool,
220+
gc_opts: GCOpts,
217221
) -> Result<GcResult> {
218222
const COMPOSEFS_GC_JOURNAL_ID: &str = "3b2a1f0e9d8c7b6a5f4e3d2c1b0a9f8e7";
219223

@@ -284,12 +288,14 @@ pub(crate) async fn composefs_gc(
284288

285289
for (ty, verity) in unreferenced_boot_binaries {
286290
match ty {
287-
BootType::Bls => delete_kernel_initrd(storage, &get_type1_dir_name(verity), dry_run)?,
288-
BootType::Uki => delete_uki(storage, verity, dry_run)?,
291+
BootType::Bls => {
292+
delete_kernel_initrd(storage, &get_type1_dir_name(verity), gc_opts.dry_run)?
293+
}
294+
BootType::Uki => delete_uki(storage, verity, gc_opts.dry_run)?,
289295
}
290296
}
291297

292-
if !prune_repo {
298+
if !gc_opts.prune_repo {
293299
return Ok(GcResult::default());
294300
}
295301

@@ -329,13 +335,13 @@ pub(crate) async fn composefs_gc(
329335

330336
for verity in &orphaned_state_dirs {
331337
tracing::debug!("Cleaning up orphaned state dir: {verity}");
332-
delete_staged(staged, &all_orphans, dry_run)?;
333-
delete_state_dir(&sysroot, verity, dry_run)?;
338+
delete_staged(staged, &all_orphans, gc_opts.dry_run)?;
339+
delete_state_dir(&sysroot, verity, gc_opts.dry_run)?;
334340
}
335341

336342
for verity in &orphaned_boot_entries {
337343
tracing::debug!("Cleaning up orphaned bootloader entry: {verity}");
338-
delete_staged(staged, &all_orphans, dry_run)?;
344+
delete_staged(staged, &all_orphans, gc_opts.dry_run)?;
339345
}
340346

341347
// Collect the set of manifest digests referenced by live deployments,
@@ -431,7 +437,7 @@ pub(crate) async fn composefs_gc(
431437
.any(|(tag_name, _)| tag_name == &expected_tag);
432438
if !has_tag {
433439
tracing::info!("Creating missing bootc tag for live deployment: {expected_tag}");
434-
if !dry_run {
440+
if !gc_opts.dry_run {
435441
composefs_oci::tag_image(&*booted_cfs.repo, manifest_digest, &expected_tag)
436442
.with_context(|| format!("Creating migration tag {expected_tag}"))?;
437443
}
@@ -450,7 +456,7 @@ pub(crate) async fn composefs_gc(
450456

451457
if !live_manifest_digests.iter().any(|d| d == manifest_digest) {
452458
tracing::debug!("Removing unreferenced bootc tag: {tag_name}");
453-
if !dry_run {
459+
if !gc_opts.dry_run {
454460
composefs_oci::untag_image(&*booted_cfs.repo, tag_name)
455461
.with_context(|| format!("Removing tag {tag_name}"))?;
456462
}
@@ -463,7 +469,7 @@ pub(crate) async fn composefs_gc(
463469
.collect::<Vec<_>>();
464470

465471
// Prune containers-storage: remove images not backing any live deployment.
466-
if !dry_run && !live_container_images.is_empty() {
472+
if !gc_opts.dry_run && !live_container_images.is_empty() {
467473
let subpath = crate::podstorage::CStorage::subpath();
468474
if sysroot.try_exists(&subpath).unwrap_or(false) {
469475
let run = Dir::open_ambient_dir("/run", cap_std_ext::cap_std::ambient_authority())?;
@@ -482,7 +488,7 @@ pub(crate) async fn composefs_gc(
482488
// images for deployments that predate the manifest→image link;
483489
// once all deployments have been pulled with the new code, these
484490
// become redundant.
485-
let gc_result = if dry_run {
491+
let gc_result = if gc_opts.dry_run {
486492
booted_cfs.repo.gc_dry_run(&additional_roots)?
487493
} else {
488494
booted_cfs.repo.gc(&additional_roots)?

crates/lib/src/bootc_composefs/update.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use fn_error_context::context;
1111
use ocidir::cap_std::ambient_authority;
1212
use ostree_ext::container::ManifestDiff;
1313

14+
use crate::bootc_composefs::gc::GCOpts;
1415
use crate::{
1516
bootc_composefs::{
1617
boot::{BootSetupType, BootType, setup_composefs_bls_boot, setup_composefs_uki_boot},
@@ -324,7 +325,15 @@ pub(crate) async fn do_upgrade(
324325

325326
// We take into account the staged bootloader entries so this won't remove
326327
// the currently staged entry
327-
composefs_gc(storage, booted_cfs, false, true).await?;
328+
composefs_gc(
329+
storage,
330+
booted_cfs,
331+
GCOpts {
332+
dry_run: false,
333+
prune_repo: true,
334+
},
335+
)
336+
.await?;
328337

329338
apply_upgrade(storage, booted_cfs, &id.to_hex(), opts).await
330339
}

crates/lib/src/cli.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use schemars::schema_for;
3838
use serde::{Deserialize, Serialize};
3939

4040
use crate::bootc_composefs::delete::delete_composefs_deployment;
41-
use crate::bootc_composefs::gc::composefs_gc;
41+
use crate::bootc_composefs::gc::{GCOpts, composefs_gc};
4242
use crate::bootc_composefs::soft_reboot::{prepare_soft_reboot_composefs, reset_soft_reboot};
4343
use crate::bootc_composefs::{
4444
digest::{compute_composefs_digest, new_temp_composefs_repo},
@@ -2231,12 +2231,18 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
22312231
}
22322232

22332233
BootedStorageKind::Composefs(booted_cfs) => {
2234-
let effective_dry_run = dry_run || assert_no_op;
2235-
let gc_result =
2236-
composefs_gc(storage, &booted_cfs, effective_dry_run, prune_repo)
2237-
.await?;
2234+
let dry_run = dry_run || assert_no_op;
2235+
let gc_result = composefs_gc(
2236+
storage,
2237+
&booted_cfs,
2238+
GCOpts {
2239+
dry_run,
2240+
prune_repo,
2241+
},
2242+
)
2243+
.await?;
22382244

2239-
if effective_dry_run {
2245+
if dry_run {
22402246
println!("Dry run (no files deleted)");
22412247
}
22422248

0 commit comments

Comments
 (0)