Skip to content

Commit 787b30e

Browse files
refactor: Pass boot dir to boot entry readers
This allows for easier testing Signed-off-by: Pragyan Poudyal <[email protected]>
1 parent c0eb340 commit 787b30e

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};
@@ -760,8 +761,11 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> {
760761
let user_cfg_path = PathBuf::from("/sysroot/boot/grub2");
761762

762763
let mut str = String::new();
764+
let boot_dir =
765+
cap_std::fs::Dir::open_ambient_dir("/sysroot/boot", cap_std::ambient_authority())
766+
.context("Opening boot dir")?;
763767
let mut menuentries =
764-
get_sorted_uki_boot_entries(&mut str).context("Getting UKI boot entries")?;
768+
get_sorted_uki_boot_entries(&boot_dir, &mut str).context("Getting UKI boot entries")?;
765769

766770
// TODO(Johan-Liebert): Currently assuming there are only two deployments
767771
assert!(menuentries.len() == 2);
@@ -808,17 +812,25 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> {
808812
}
809813

810814
// Need str to store lifetime
811-
pub(crate) fn get_sorted_uki_boot_entries<'a>(str: &'a mut String) -> Result<Vec<MenuEntry<'a>>> {
812-
let mut file = std::fs::File::open(format!("/sysroot/boot/grub2/{USER_CFG}"))?;
815+
pub(crate) fn get_sorted_uki_boot_entries<'a>(
816+
boot_dir: &Dir,
817+
str: &'a mut String,
818+
) -> Result<Vec<MenuEntry<'a>>> {
819+
let mut file = boot_dir
820+
.open(format!("grub2/{USER_CFG}"))
821+
.with_context(|| format!("Opening {USER_CFG}"))?;
813822
file.read_to_string(str)?;
814823
parse_grub_menuentry_file(str)
815824
}
816825

817-
#[context("Getting boot entries")]
818-
pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConfig>> {
826+
#[context("Getting sorted BLS entries")]
827+
pub(crate) fn get_sorted_bls_boot_entries(
828+
boot_dir: &Dir,
829+
ascending: bool,
830+
) -> Result<Vec<BLSConfig>> {
819831
let mut all_configs = vec![];
820832

821-
for entry in std::fs::read_dir(format!("/sysroot/boot/loader/{BOOT_LOADER_ENTRIES}"))? {
833+
for entry in boot_dir.read_dir(format!("loader/{BOOT_LOADER_ENTRIES}"))? {
822834
let entry = entry?;
823835

824836
let file_name = entry.file_name();
@@ -831,8 +843,13 @@ pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConf
831843
continue;
832844
}
833845

834-
let contents = std::fs::read_to_string(&entry.path())
835-
.with_context(|| format!("Failed to read {:?}", entry.path()))?;
846+
let mut file = entry
847+
.open()
848+
.with_context(|| format!("Failed to open {:?}", file_name))?;
849+
850+
let mut contents = String::new();
851+
file.read_to_string(&mut contents)
852+
.with_context(|| format!("Failed to read {:?}", file_name))?;
836853

837854
let config = parse_bls_config(&contents).context("Parsing bls config")?;
838855

@@ -846,11 +863,15 @@ pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConf
846863

847864
#[context("Rolling back BLS")]
848865
pub(crate) fn rollback_composefs_bls() -> Result<()> {
866+
let boot_dir =
867+
cap_std::fs::Dir::open_ambient_dir("/sysroot/boot", cap_std::ambient_authority())
868+
.context("Opening boot dir")?;
869+
849870
// Sort in descending order as that's the order they're shown on the boot screen
850871
// After this:
851872
// all_configs[0] -> booted depl
852873
// all_configs[1] -> rollback depl
853-
let mut all_configs = get_sorted_bls_boot_entries(false)?;
874+
let mut all_configs = get_sorted_bls_boot_entries(&boot_dir, false)?;
854875

855876
// Update the indicies so that they're swapped
856877
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
@@ -2017,7 +2017,9 @@ pub(crate) fn setup_composefs_uki_boot(
20172017
)?;
20182018

20192019
let mut str_buf = String::new();
2020-
let entries = get_sorted_uki_boot_entries(&mut str_buf)?;
2020+
let boot_dir = cap_std::fs::Dir::open_ambient_dir(boot_dir, cap_std::ambient_authority())
2021+
.context("Opening boot dir")?;
2022+
let entries = get_sorted_uki_boot_entries(&boot_dir, &mut str_buf)?;
20212023

20222024
// Write out only the currently booted entry, which should be the very first one
20232025
// 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
@@ -553,9 +553,11 @@ pub(crate) async fn composefs_deployment_status() -> Result<Host> {
553553
anyhow::bail!("Could not determine boot type");
554554
};
555555

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

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

0 commit comments

Comments
 (0)