Skip to content

Commit 69a2944

Browse files
Johan-Liebert1cgwalters
authored andcommitted
composefs-backend/install: Get version from UKI
Signed-off-by: Pragyan Poudyal <[email protected]>
1 parent 9789bbd commit 69a2944

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

crates/lib/src/bootc_composefs/boot.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ use cap_std_ext::{
1515
use clap::ValueEnum;
1616
use composefs::fs::read_file;
1717
use composefs::tree::RegularFile;
18-
use composefs_boot::bootloader::{PEType, EFI_ADDON_DIR_EXT, EFI_ADDON_FILE_EXT, EFI_EXT};
1918
use composefs_boot::BootOps;
19+
use composefs_boot::{
20+
bootloader::{PEType, EFI_ADDON_DIR_EXT, EFI_ADDON_FILE_EXT, EFI_EXT},
21+
uki::UkiError,
22+
};
2023
use fn_error_context::context;
2124
use ostree_ext::composefs::fsverity::{FsVerityHashValue, Sha512HashValue};
2225
use ostree_ext::composefs_boot::bootloader::UsrLibModulesVmlinuz;
@@ -365,7 +368,6 @@ struct BLSEntryPath<'a> {
365368
#[context("Setting up BLS boot")]
366369
pub(crate) fn setup_composefs_bls_boot(
367370
setup_type: BootSetupType,
368-
// TODO: Make this generic
369371
repo: crate::store::ComposefsRepository,
370372
id: &Sha512HashValue,
371373
entry: &ComposefsBootEntry<Sha512HashValue>,
@@ -563,6 +565,11 @@ pub(crate) fn setup_composefs_bls_boot(
563565
Ok(boot_digest)
564566
}
565567

568+
struct UKILabels {
569+
boot_label: String,
570+
version: Option<String>,
571+
}
572+
566573
/// Writes a PortableExecutable to ESP along with any PE specific or Global addons
567574
#[context("Writing {file_path} to ESP")]
568575
fn write_pe_to_esp(
@@ -574,10 +581,10 @@ fn write_pe_to_esp(
574581
is_insecure_from_opts: bool,
575582
mounted_efi: impl AsRef<Path>,
576583
bootloader: &Bootloader,
577-
) -> Result<Option<String>> {
584+
) -> Result<Option<UKILabels>> {
578585
let efi_bin = read_file(file, &repo).context("Reading .efi binary")?;
579586

580-
let mut boot_label = None;
587+
let mut boot_label: Option<UKILabels> = None;
581588

582589
// UKI Extension might not even have a cmdline
583590
// TODO: UKI Addon might also have a composefs= cmdline?
@@ -607,7 +614,15 @@ fn write_pe_to_esp(
607614
);
608615
}
609616

610-
boot_label = Some(uki::get_boot_label(&efi_bin).context("Getting UKI boot label")?);
617+
let osrel = uki::get_text_section(&efi_bin, ".osrel")
618+
.ok_or(UkiError::PortableExecutableError)??;
619+
620+
let parsed_osrel = OsReleaseInfo::parse(osrel);
621+
622+
boot_label = Some(UKILabels {
623+
boot_label: uki::get_boot_label(&efi_bin).context("Getting UKI boot label")?,
624+
version: parsed_osrel.get_version(),
625+
});
611626
}
612627

613628
// Write the UKI to ESP
@@ -693,8 +708,6 @@ fn write_grub_uki_menuentry(
693708
.context("opening boot/grub2")?;
694709

695710
// Iterate over all available deployments, and generate a menuentry for each
696-
//
697-
// TODO: We might find a staged deployment here
698711
if is_upgrade {
699712
let mut str_buf = String::new();
700713
let boot_dir =
@@ -758,20 +771,19 @@ fn write_grub_uki_menuentry(
758771
fn write_systemd_uki_config(
759772
esp_dir: &Dir,
760773
setup_type: &BootSetupType,
761-
boot_label: String,
774+
boot_label: UKILabels,
762775
id: &Sha512HashValue,
763776
) -> Result<()> {
764777
let default_sort_key = "0";
765778

766779
let mut bls_conf = BLSConfig::default();
767780
bls_conf
768-
.with_title(boot_label)
781+
.with_title(boot_label.boot_label)
769782
.with_cfg(BLSConfigType::EFI {
770783
efi: format!("/{SYSTEMD_UKI_DIR}/{}{}", id.to_hex(), EFI_EXT).into(),
771784
})
772785
.with_sort_key(default_sort_key.into())
773-
// TODO (Johan-Liebert1): Get version from UKI like we get boot label
774-
.with_version(default_sort_key.into());
786+
.with_version(boot_label.version.unwrap_or(default_sort_key.into()));
775787

776788
let (entries_dir, booted_bls) = match setup_type {
777789
BootSetupType::Setup(..) => {
@@ -827,7 +839,6 @@ fn write_systemd_uki_config(
827839
#[context("Setting up UKI boot")]
828840
pub(crate) fn setup_composefs_uki_boot(
829841
setup_type: BootSetupType,
830-
// TODO: Make this generic
831842
repo: crate::store::ComposefsRepository,
832843
id: &Sha512HashValue,
833844
entries: Vec<ComposefsBootEntry<Sha512HashValue>>,
@@ -868,7 +879,7 @@ pub(crate) fn setup_composefs_uki_boot(
868879

869880
let esp_mount = mount_esp(&esp_device).context("Mounting ESP")?;
870881

871-
let mut boot_label = String::new();
882+
let mut uki_label: Option<UKILabels> = None;
872883

873884
for entry in entries {
874885
match entry {
@@ -917,20 +928,25 @@ pub(crate) fn setup_composefs_uki_boot(
917928
)?;
918929

919930
if let Some(label) = ret {
920-
boot_label = label;
931+
uki_label = Some(label);
921932
}
922933
}
923934
};
924935
}
925936

937+
let uki_label = uki_label
938+
.ok_or_else(|| anyhow::anyhow!("Failed to get version and boot label from UKI"))?;
939+
926940
match bootloader {
927-
Bootloader::Grub => {
928-
write_grub_uki_menuentry(root_path, &setup_type, boot_label, id, &esp_device)?
929-
}
941+
Bootloader::Grub => write_grub_uki_menuentry(
942+
root_path,
943+
&setup_type,
944+
uki_label.boot_label,
945+
id,
946+
&esp_device,
947+
)?,
930948

931-
Bootloader::Systemd => {
932-
write_systemd_uki_config(&esp_mount.fd, &setup_type, boot_label, id)?
933-
}
949+
Bootloader::Systemd => write_systemd_uki_config(&esp_mount.fd, &setup_type, uki_label, id)?,
934950
};
935951

936952
Ok(())

crates/lib/src/bootc_composefs/update.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ use cap_std_ext::cap_std::{ambient_authority, fs::Dir};
2121

2222
#[context("Getting SHA256 Digest for {id}")]
2323
pub fn str_to_sha256digest(id: &str) -> Result<Sha256Digest> {
24-
let id = if id.starts_with("sha256:") {
25-
id.strip_prefix("sha256:").unwrap()
26-
} else {
27-
id
28-
};
29-
24+
let id = id.strip_prefix("sha256:").unwrap_or(id);
3025
Ok(parse_sha256(&id)?)
3126
}
3227

0 commit comments

Comments
 (0)