Skip to content

Commit f0ffe64

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

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

754755
let mut str = String::new();
756+
let boot_dir =
757+
cap_std::fs::Dir::open_ambient_dir("/sysroot/boot", cap_std::ambient_authority())
758+
.context("Opening boot dir")?;
755759
let mut menuentries =
756-
get_sorted_uki_boot_entries(&mut str).context("Getting UKI boot entries")?;
760+
get_sorted_uki_boot_entries(&boot_dir, &mut str).context("Getting UKI boot entries")?;
757761

758762
// TODO(Johan-Liebert): Currently assuming there are only two deployments
759763
assert!(menuentries.len() == 2);
@@ -800,17 +804,25 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> {
800804
}
801805

802806
// Need str to store lifetime
803-
pub(crate) fn get_sorted_uki_boot_entries<'a>(str: &'a mut String) -> Result<Vec<MenuEntry<'a>>> {
804-
let mut file = std::fs::File::open(format!("/sysroot/boot/grub2/{USER_CFG}"))?;
807+
pub(crate) fn get_sorted_uki_boot_entries<'a>(
808+
boot_dir: &Dir,
809+
str: &'a mut String,
810+
) -> Result<Vec<MenuEntry<'a>>> {
811+
let mut file = boot_dir
812+
.open(format!("grub2/{USER_CFG}"))
813+
.with_context(|| format!("Opening {USER_CFG}"))?;
805814
file.read_to_string(str)?;
806815
parse_grub_menuentry_file(str)
807816
}
808817

809-
#[context("Getting boot entries")]
810-
pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConfig>> {
818+
#[context("Getting sorted BLS entries")]
819+
pub(crate) fn get_sorted_bls_boot_entries(
820+
boot_dir: &Dir,
821+
ascending: bool,
822+
) -> Result<Vec<BLSConfig>> {
811823
let mut all_configs = vec![];
812824

813-
for entry in std::fs::read_dir(format!("/sysroot/boot/loader/{BOOT_LOADER_ENTRIES}"))? {
825+
for entry in boot_dir.read_dir(format!("loader/{BOOT_LOADER_ENTRIES}"))? {
814826
let entry = entry?;
815827

816828
let file_name = entry.file_name();
@@ -823,8 +835,13 @@ pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConf
823835
continue;
824836
}
825837

826-
let contents = std::fs::read_to_string(&entry.path())
827-
.with_context(|| format!("Failed to read {:?}", entry.path()))?;
838+
let mut file = entry
839+
.open()
840+
.with_context(|| format!("Failed to open {:?}", file_name))?;
841+
842+
let mut contents = String::new();
843+
file.read_to_string(&mut contents)
844+
.with_context(|| format!("Failed to read {:?}", file_name))?;
828845

829846
let config = parse_bls_config(&contents).context("Parsing bls config")?;
830847

@@ -838,11 +855,15 @@ pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConf
838855

839856
#[context("Rolling back BLS")]
840857
pub(crate) fn rollback_composefs_bls() -> Result<()> {
858+
let boot_dir =
859+
cap_std::fs::Dir::open_ambient_dir("/sysroot/boot", cap_std::ambient_authority())
860+
.context("Opening boot dir")?;
861+
841862
// Sort in descending order as that's the order they're shown on the boot screen
842863
// After this:
843864
// all_configs[0] -> booted depl
844865
// all_configs[1] -> rollback depl
845-
let mut all_configs = get_sorted_bls_boot_entries(false)?;
866+
let mut all_configs = get_sorted_bls_boot_entries(&boot_dir, false)?;
846867

847868
// Update the indicies so that they're swapped
848869
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
@@ -2029,7 +2029,9 @@ pub(crate) fn setup_composefs_uki_boot(
20292029
)?;
20302030

20312031
let mut str_buf = String::new();
2032-
let entries = get_sorted_uki_boot_entries(&mut str_buf)?;
2032+
let boot_dir = cap_std::fs::Dir::open_ambient_dir(boot_dir, cap_std::ambient_authority())
2033+
.context("Opening boot dir")?;
2034+
let entries = get_sorted_uki_boot_entries(&boot_dir, &mut str_buf)?;
20332035

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

491+
let boot_dir = sysroot.open_dir("boot").context("Opening boot dir")?;
492+
491493
match boot_type {
492494
BootType::Bls => {
493-
host.status.rollback_queued = !get_sorted_bls_boot_entries(false)?
495+
host.status.rollback_queued = !get_sorted_bls_boot_entries(&boot_dir, false)?
494496
.first()
495497
.ok_or(anyhow::anyhow!("First boot entry not found"))?
496498
.options
@@ -500,7 +502,7 @@ pub(crate) async fn composefs_deployment_status() -> Result<Host> {
500502
BootType::Uki => {
501503
let mut s = String::new();
502504

503-
host.status.rollback_queued = !get_sorted_uki_boot_entries(&mut s)?
505+
host.status.rollback_queued = !get_sorted_uki_boot_entries(&boot_dir, &mut s)?
504506
.first()
505507
.ok_or(anyhow::anyhow!("First boot entry not found"))?
506508
.body

0 commit comments

Comments
 (0)