Skip to content

Commit aefb8ee

Browse files
refactor: Pass boot dir to boot entry readers
This allows for easier testing Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent b93a14b commit aefb8ee

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

crates/lib/src/deploy.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use ostree_ext::tokio_util::spawn_blocking_cancellable_flatten;
2626
use rustix::fs::{fsync, renameat_with, AtFlags, RenameFlags};
2727

2828
use crate::composefs_consts::{
29-
BOOT_LOADER_ENTRIES, ROLLBACK_BOOT_LOADER_ENTRIES, USER_CFG, USER_CFG_ROLLBACK,
29+
BOOT_LOADER_ENTRIES, ROLLBACK_BOOT_LOADER_ENTRIES, USER_CFG,
30+
USER_CFG_ROLLBACK,
3031
};
3132
use crate::install::{get_efi_uuid_source, BootType};
3233
use crate::parsers::bls_config::{parse_bls_config, BLSConfig};
@@ -755,8 +756,11 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> {
755756
let user_cfg_path = PathBuf::from("/sysroot/boot/grub2");
756757

757758
let mut str = String::new();
759+
let boot_dir =
760+
cap_std::fs::Dir::open_ambient_dir("/sysroot/boot", cap_std::ambient_authority())
761+
.context("Opening boot dir")?;
758762
let mut menuentries =
759-
get_sorted_uki_boot_entries(&mut str).context("Getting UKI boot entries")?;
763+
get_sorted_uki_boot_entries(&boot_dir, &mut str).context("Getting UKI boot entries")?;
760764

761765
// TODO(Johan-Liebert): Currently assuming there are only two deployments
762766
assert!(menuentries.len() == 2);
@@ -803,17 +807,25 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> {
803807
}
804808

805809
// Need str to store lifetime
806-
pub(crate) fn get_sorted_uki_boot_entries<'a>(str: &'a mut String) -> Result<Vec<MenuEntry<'a>>> {
807-
let mut file = std::fs::File::open(format!("/sysroot/boot/grub2/{USER_CFG}"))?;
810+
pub(crate) fn get_sorted_uki_boot_entries<'a>(
811+
boot_dir: &Dir,
812+
str: &'a mut String,
813+
) -> Result<Vec<MenuEntry<'a>>> {
814+
let mut file = boot_dir
815+
.open(format!("grub2/{USER_CFG}"))
816+
.with_context(|| format!("Opening {USER_CFG}"))?;
808817
file.read_to_string(str)?;
809818
parse_grub_menuentry_file(str)
810819
}
811820

812-
#[context("Getting boot entries")]
813-
pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConfig>> {
821+
#[context("Getting sorted BLS entries")]
822+
pub(crate) fn get_sorted_bls_boot_entries(
823+
boot_dir: &Dir,
824+
ascending: bool,
825+
) -> Result<Vec<BLSConfig>> {
814826
let mut all_configs = vec![];
815827

816-
for entry in std::fs::read_dir(format!("/sysroot/boot/loader/{BOOT_LOADER_ENTRIES}"))? {
828+
for entry in boot_dir.read_dir(format!("loader/{BOOT_LOADER_ENTRIES}"))? {
817829
let entry = entry?;
818830

819831
let file_name = entry.file_name();
@@ -826,8 +838,13 @@ pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConf
826838
continue;
827839
}
828840

829-
let contents = std::fs::read_to_string(&entry.path())
830-
.with_context(|| format!("Failed to read {:?}", entry.path()))?;
841+
let mut file = entry
842+
.open()
843+
.with_context(|| format!("Failed to open {:?}", file_name))?;
844+
845+
let mut contents = String::new();
846+
file.read_to_string(&mut contents)
847+
.with_context(|| format!("Failed to read {:?}", file_name))?;
831848

832849
let config = parse_bls_config(&contents).context("Parsing bls config")?;
833850

@@ -841,11 +858,15 @@ pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConf
841858

842859
#[context("Rolling back BLS")]
843860
pub(crate) fn rollback_composefs_bls() -> Result<()> {
861+
let boot_dir =
862+
cap_std::fs::Dir::open_ambient_dir("/sysroot/boot", cap_std::ambient_authority())
863+
.context("Opening boot dir")?;
864+
844865
// Sort in descending order as that's the order they're shown on the boot screen
845866
// After this:
846867
// all_configs[0] -> booted depl
847868
// all_configs[1] -> rollback depl
848-
let mut all_configs = get_sorted_bls_boot_entries(false)?;
869+
let mut all_configs = get_sorted_bls_boot_entries(&boot_dir, false)?;
849870

850871
// Update the indicies so that they're swapped
851872
for (idx, cfg) in all_configs.iter_mut().enumerate() {

crates/lib/src/install.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,9 @@ pub(crate) fn setup_composefs_uki_boot(
19931993
)?;
19941994

19951995
let mut str_buf = String::new();
1996-
let entries = get_sorted_uki_boot_entries(&mut str_buf)?;
1996+
let boot_dir = cap_std::fs::Dir::open_ambient_dir(boot_dir, cap_std::ambient_authority())
1997+
.context("Opening boot dir")?;
1998+
let entries = get_sorted_uki_boot_entries(&boot_dir, &mut str_buf)?;
19971999

19982000
// Write out only the currently booted entry, which should be the very first one
19992001
// Even if we have booted into the second menuentry "boot entry", the default will be the

crates/lib/src/status.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,11 @@ pub(crate) async fn composefs_deployment_status() -> Result<Host> {
552552
anyhow::bail!("Could not determine boot type");
553553
};
554554

555+
let boot_dir = sysroot.open_dir("boot").context("Opening boot dir")?;
556+
555557
match boot_type {
556558
BootType::Bls => {
557-
host.status.rollback_queued = !get_sorted_bls_boot_entries(false)?
559+
host.status.rollback_queued = !get_sorted_bls_boot_entries(&boot_dir, false)?
558560
.first()
559561
.ok_or(anyhow::anyhow!("First boot entry not found"))?
560562
.options
@@ -566,7 +568,7 @@ pub(crate) async fn composefs_deployment_status() -> Result<Host> {
566568
BootType::Uki => {
567569
let mut s = String::new();
568570

569-
host.status.rollback_queued = !get_sorted_uki_boot_entries(&mut s)?
571+
host.status.rollback_queued = !get_sorted_uki_boot_entries(&boot_dir, &mut s)?
570572
.first()
571573
.ok_or(anyhow::anyhow!("First boot entry not found"))?
572574
.body

0 commit comments

Comments
 (0)