Skip to content

Commit bc7dd9a

Browse files
Johan-Liebert1cgwalters
authored andcommitted
storage: Add bls_boot_binaries_dir method
For grub booted systems, we keep Type1 (BLS) boot binaries (kernel + initrd) in `/sysroot/boot`, which is also where `Storage::boot_dir` points to. But, for systems with systemd-boot as the bootloader, we keep the said binaries at `ESP/EFI/Linux` but `Storage::boot_dir` points at the ESP This function opens the proper directory depending on the bootloader. We need this primarily for GC Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent 8f1683d commit bc7dd9a

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

crates/lib/src/bootc_composefs/gc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use composefs::repository::GcResult;
1010
use composefs_boot::bootloader::{EFI_ADDON_DIR_EXT, EFI_EXT};
1111

1212
use crate::{
13-
bootc_composefs::boot::get_type1_dir_name,
1413
bootc_composefs::{
15-
boot::{BOOTC_UKI_DIR, BootType},
14+
boot::{BOOTC_UKI_DIR, BootType, get_type1_dir_name},
1615
delete::{delete_image, delete_staged, delete_state_dir},
1716
status::{get_composefs_status, get_imginfo, list_bootloader_entries},
1817
},
@@ -66,7 +65,7 @@ type BootBinary = (BootType, String);
6665
#[fn_error_context::context("Collecting boot binaries")]
6766
fn collect_boot_binaries(storage: &Storage) -> Result<Vec<BootBinary>> {
6867
let mut boot_binaries = Vec::new();
69-
let boot_dir = storage.require_boot_dir()?;
68+
let boot_dir = storage.bls_boot_binaries_dir()?;
7069
let esp = storage.require_esp()?;
7170

7271
// Scan for UKI binaries in EFI/Linux/bootc
@@ -125,14 +124,14 @@ fn collect_type1_boot_binaries(boot_dir: &Dir, boot_binaries: &mut Vec<BootBinar
125124

126125
#[fn_error_context::context("Deleting kernel and initrd")]
127126
fn delete_kernel_initrd(storage: &Storage, dir_to_delete: &str, dry_run: bool) -> Result<()> {
128-
let boot_dir = storage.require_boot_dir()?;
129-
130127
tracing::debug!("Deleting Type1 entry {dir_to_delete}");
131128

132129
if dry_run {
133130
return Ok(());
134131
}
135132

133+
let boot_dir = storage.bls_boot_binaries_dir()?;
134+
136135
boot_dir
137136
.remove_dir_all(dir_to_delete)
138137
.with_context(|| anyhow::anyhow!("Deleting {dir_to_delete}"))
@@ -217,6 +216,7 @@ pub(crate) async fn composefs_gc(
217216
let boot_binaries = collect_boot_binaries(storage)?;
218217

219218
tracing::debug!("bootloader_entries: {bootloader_entries:?}");
219+
tracing::debug!("boot_binaries: {boot_binaries:?}");
220220

221221
// Bootloader entry is deleted, but the binary (UKI/kernel+initrd) still exists
222222
let unreferenced_boot_binaries = boot_binaries

crates/lib/src/store/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use ostree_ext::sysroot::SysrootLock;
3636
use ostree_ext::{gio, ostree};
3737
use rustix::fs::Mode;
3838

39-
use crate::bootc_composefs::boot::mount_esp;
39+
use crate::bootc_composefs::boot::{EFI_LINUX, mount_esp};
4040
use crate::bootc_composefs::status::{ComposefsCmdline, composefs_booted, get_bootloader};
4141
use crate::lsm;
4242
use crate::podstorage::CStorage;
@@ -376,6 +376,28 @@ impl Storage {
376376
.ok_or_else(|| anyhow::anyhow!("ESP not found"))
377377
}
378378

379+
/// Returns the Directory where the Type1 boot binaries are stored
380+
/// `/sysroot/boot` for Grub, and ESP/EFI/Linux for systemd-boot
381+
pub(crate) fn bls_boot_binaries_dir(&self) -> Result<Dir> {
382+
let boot_dir = self.require_boot_dir()?;
383+
384+
// boot dir in case of systemd-boot points to the ESP, but we store
385+
// the actual binaries inside ESP/EFI/Linux
386+
let boot_dir = match get_bootloader()? {
387+
Bootloader::Grub => boot_dir.try_clone()?,
388+
Bootloader::Systemd => {
389+
let boot_dir = boot_dir
390+
.open_dir(EFI_LINUX)
391+
.with_context(|| format!("Opening {EFI_LINUX}"))?;
392+
393+
boot_dir
394+
}
395+
Bootloader::None => anyhow::bail!("Unknown bootloader"),
396+
};
397+
398+
Ok(boot_dir)
399+
}
400+
379401
/// Access the underlying ostree repository
380402
pub(crate) fn get_ostree(&self) -> Result<&SysrootLock> {
381403
self.ostree

0 commit comments

Comments
 (0)