Skip to content

Commit 5e5a341

Browse files
Johan-Liebert1cgwalters
authored andcommitted
composefs/uki: Use custom prefix for UKI name
Similar to how we do with BLS binaries, we now add our custom prefix to UKI and UKI Addons. This is useful for us to find our EFI binaries in the ESP Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent bc7dd9a commit 5e5a341

6 files changed

Lines changed: 66 additions & 28 deletions

File tree

crates/lib/src/bootc_composefs/boot.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ use rustix::{mount::MountFlags, path::Arg};
9191
use schemars::JsonSchema;
9292
use serde::{Deserialize, Serialize};
9393

94-
use crate::parsers::bls_config::{BLSConfig, BLSConfigType};
9594
use crate::task::Task;
9695
use crate::{
9796
bootc_composefs::repo::get_imgref,
@@ -109,6 +108,10 @@ use crate::{
109108
bootc_composefs::status::get_container_manifest_and_config, bootc_kargs::compute_new_kargs,
110109
};
111110
use crate::{bootc_composefs::status::get_sorted_grub_uki_boot_entries, install::PostFetchState};
111+
use crate::{
112+
composefs_consts::UKI_NAME_PREFIX,
113+
parsers::bls_config::{BLSConfig, BLSConfigType},
114+
};
112115
use crate::{
113116
composefs_consts::{
114117
BOOT_LOADER_ENTRIES, COMPOSEFS_CMDLINE, ORIGIN_KEY_BOOT, ORIGIN_KEY_BOOT_DIGEST,
@@ -274,10 +277,26 @@ pub(crate) fn secondary_sort_key(os_id: &str) -> String {
274277
}
275278

276279
/// Returns the name of the directory where we store Type1 boot entries
277-
pub(crate) fn get_type1_dir_name(depl_verity: &String) -> String {
280+
pub(crate) fn get_type1_dir_name(depl_verity: &str) -> String {
278281
format!("{TYPE1_BOOT_DIR_PREFIX}{depl_verity}")
279282
}
280283

284+
/// Returns the name of a UKI given verity digest
285+
pub(crate) fn get_uki_name(depl_verity: &str) -> String {
286+
format!("{UKI_NAME_PREFIX}{depl_verity}{EFI_EXT}")
287+
}
288+
289+
/// Returns the name of a UKI Addon directory given verity digest
290+
pub(crate) fn get_uki_addon_dir_name(depl_verity: &str) -> String {
291+
format!("{UKI_NAME_PREFIX}{depl_verity}{EFI_ADDON_DIR_EXT}")
292+
}
293+
294+
#[allow(dead_code)]
295+
/// Returns the name of a UKI Addon given verity digest
296+
pub(crate) fn get_uki_addon_file_name(depl_verity: &str) -> String {
297+
format!("{UKI_NAME_PREFIX}{depl_verity}{EFI_ADDON_FILE_EXT}")
298+
}
299+
281300
/// Compute SHA256Sum of VMlinuz + Initrd
282301
///
283302
/// # Arguments
@@ -873,7 +892,7 @@ fn write_pe_to_esp(
873892
Some(parent) => {
874893
let renamed_path = match parent.as_str().ends_with(EFI_ADDON_DIR_EXT) {
875894
true => {
876-
let dir_name = format!("{}{}", uki_id.to_hex(), EFI_ADDON_DIR_EXT);
895+
let dir_name = get_uki_addon_dir_name(&uki_id.to_hex());
877896

878897
parent
879898
.parent()
@@ -897,7 +916,7 @@ fn write_pe_to_esp(
897916
.with_context(|| format!("Opening {final_pe_path:?}"))?;
898917

899918
let pe_name = match pe_type {
900-
PEType::Uki => &format!("{}{}", uki_id.to_hex(), EFI_EXT),
919+
PEType::Uki => &get_uki_name(&uki_id.to_hex()),
901920
PEType::UkiAddon => file_path
902921
.components()
903922
.last()
@@ -1017,7 +1036,7 @@ fn write_systemd_uki_config(
10171036
bls_conf
10181037
.with_title(boot_label.boot_label)
10191038
.with_cfg(BLSConfigType::EFI {
1020-
efi: format!("/{BOOTC_UKI_DIR}/{}{}", id.to_hex(), EFI_EXT).into(),
1039+
efi: format!("/{BOOTC_UKI_DIR}/{}", get_uki_name(&id.to_hex())).into(),
10211040
})
10221041
.with_sort_key(primary_sort_key.clone())
10231042
.with_version(boot_label.version.unwrap_or_else(|| id.to_hex()));

crates/lib/src/bootc_composefs/gc.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
use anyhow::{Context, Result};
88
use cap_std_ext::{cap_std::fs::Dir, dirext::CapStdExtDirExt};
99
use composefs::repository::GcResult;
10-
use composefs_boot::bootloader::{EFI_ADDON_DIR_EXT, EFI_EXT};
10+
use composefs_boot::bootloader::EFI_EXT;
1111

1212
use crate::{
1313
bootc_composefs::{
14-
boot::{BOOTC_UKI_DIR, BootType, get_type1_dir_name},
14+
boot::{BOOTC_UKI_DIR, BootType, get_type1_dir_name, get_uki_addon_dir_name, get_uki_name},
1515
delete::{delete_image, delete_staged, delete_state_dir},
1616
status::{get_composefs_status, get_imginfo, list_bootloader_entries},
1717
},
18-
composefs_consts::{STATE_DIR_RELATIVE, TYPE1_BOOT_DIR_PREFIX},
18+
composefs_consts::{STATE_DIR_RELATIVE, TYPE1_BOOT_DIR_PREFIX, UKI_NAME_PREFIX},
1919
store::{BootedComposefs, Storage},
2020
};
2121

@@ -89,8 +89,12 @@ fn collect_uki_binaries(boot_dir: &Dir, boot_binaries: &mut Vec<BootBinary>) ->
8989
let entry = entry?;
9090
let name = entry.file_name()?;
9191

92+
let Some(verity) = name.strip_prefix(UKI_NAME_PREFIX) else {
93+
continue;
94+
};
95+
9296
if name.ends_with(EFI_EXT) {
93-
boot_binaries.push((BootType::Uki, name));
97+
boot_binaries.push((BootType::Uki, verity.into()));
9498
}
9599
}
96100

@@ -151,15 +155,15 @@ fn delete_uki(storage: &Storage, uki_id: &str, dry_run: bool) -> Result<()> {
151155
let entry_name = entry.file_name()?;
152156

153157
// The actual UKI PE binary
154-
if entry_name == format!("{}{}", uki_id, EFI_EXT) {
158+
if entry_name == get_uki_name(uki_id) {
155159
tracing::debug!("Deleting UKI: {}", entry_name);
156160

157161
if dry_run {
158162
continue;
159163
}
160164

161165
entry.remove_file().context("Deleting UKI")?;
162-
} else if entry_name == format!("{}{}", uki_id, EFI_ADDON_DIR_EXT) {
166+
} else if entry_name == get_uki_addon_dir_name(uki_id) {
163167
// Addons dir
164168
tracing::debug!("Deleting UKI addons directory: {}", entry_name);
165169

crates/lib/src/bootc_composefs/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
bootc_composefs::{
3-
boot::{BOOTC_UKI_DIR, compute_boot_digest_uki},
3+
boot::{BOOTC_UKI_DIR, compute_boot_digest_uki, get_uki_name},
44
state::update_boot_digest_in_origin,
55
},
66
store::Storage,
@@ -12,7 +12,7 @@ use fn_error_context::context;
1212
fn get_uki(storage: &Storage, deployment_verity: &str) -> Result<Vec<u8>> {
1313
let uki_dir = storage.require_esp()?.fd.open_dir(BOOTC_UKI_DIR)?;
1414

15-
let req_fname = format!("{deployment_verity}.efi");
15+
let req_fname = get_uki_name(deployment_verity);
1616

1717
for entry in uki_dir.entries_utf8()? {
1818
let pe = entry?;

crates/lib/src/composefs_consts.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ pub(crate) const BOOTC_FINALIZE_STAGED_SERVICE: &str = "bootc-finalize-staged.se
3939

4040
/// The prefix for the directories containing kernel + initrd
4141
pub(crate) const TYPE1_BOOT_DIR_PREFIX: &str = "bootc_composefs-";
42+
43+
/// The prefix for names of UKI and UKI Addons
44+
pub(crate) const UKI_NAME_PREFIX: &str = TYPE1_BOOT_DIR_PREFIX;

crates/lib/src/parsers/bls_config.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::fmt::Display;
1414
use uapi_version::Version;
1515

1616
use crate::bootc_composefs::status::ComposefsCmdline;
17-
use crate::composefs_consts::COMPOSEFS_CMDLINE;
17+
use crate::composefs_consts::{COMPOSEFS_CMDLINE, UKI_NAME_PREFIX};
1818

1919
#[derive(Debug, PartialEq, Eq, Default)]
2020
pub enum BLSConfigType {
@@ -174,14 +174,20 @@ impl BLSConfig {
174174

175175
pub(crate) fn get_verity(&self) -> Result<String> {
176176
match &self.cfg_type {
177-
BLSConfigType::EFI { efi } => Ok(efi
178-
.components()
179-
.last()
180-
.ok_or(anyhow::anyhow!("Empty efi field"))?
181-
.to_string()
182-
.strip_suffix(EFI_EXT)
183-
.ok_or(anyhow::anyhow!("efi doesn't end with .efi"))?
184-
.to_string()),
177+
BLSConfigType::EFI { efi } => {
178+
let name = efi
179+
.components()
180+
.last()
181+
.ok_or(anyhow::anyhow!("Empty efi field"))?
182+
.to_string()
183+
.strip_prefix(UKI_NAME_PREFIX)
184+
.ok_or_else(|| anyhow::anyhow!("efi does not start with custom prefix"))?
185+
.strip_suffix(EFI_EXT)
186+
.ok_or_else(|| anyhow::anyhow!("efi doesn't end with .efi"))?
187+
.to_string();
188+
189+
Ok(name)
190+
}
185191

186192
BLSConfigType::NonEFI { options, .. } => {
187193
let options = options.as_ref().ok_or(anyhow::anyhow!("No options"))?;

crates/lib/src/parsers/grub_menuconfig.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use nom::{
1515
sequence::delimited,
1616
};
1717

18-
use crate::bootc_composefs::boot::BOOTC_UKI_DIR;
18+
use crate::{
19+
bootc_composefs::boot::{BOOTC_UKI_DIR, get_uki_name},
20+
composefs_consts::UKI_NAME_PREFIX,
21+
};
1922

2023
/// Body content of a GRUB menuentry containing parsed commands.
2124
#[derive(Debug, PartialEq, Eq)]
@@ -91,13 +94,12 @@ impl<'a> Display for MenuEntry<'a> {
9194
}
9295

9396
impl<'a> MenuEntry<'a> {
94-
#[allow(dead_code)]
9597
pub(crate) fn new(boot_label: &str, uki_id: &str) -> Self {
9698
Self {
9799
title: format!("{boot_label}: ({uki_id})"),
98100
body: MenuentryBody {
99101
insmod: vec!["fat", "chain"],
100-
chainloader: format!("/{BOOTC_UKI_DIR}/{uki_id}.efi"),
102+
chainloader: format!("/{BOOTC_UKI_DIR}/{}", get_uki_name(uki_id)),
101103
search: "--no-floppy --set=root --fs-uuid \"${EFI_PART_UUID}\"",
102104
version: 0,
103105
extra: vec![],
@@ -108,14 +110,18 @@ impl<'a> MenuEntry<'a> {
108110
pub(crate) fn get_verity(&self) -> Result<String> {
109111
let to_path = Utf8PathBuf::from(self.body.chainloader.clone());
110112

111-
Ok(to_path
113+
let name = to_path
112114
.components()
113115
.last()
114116
.ok_or(anyhow::anyhow!("Empty efi field"))?
115117
.to_string()
118+
.strip_prefix(UKI_NAME_PREFIX)
119+
.ok_or_else(|| anyhow::anyhow!("efi does not start with custom prefix"))?
116120
.strip_suffix(EFI_EXT)
117-
.ok_or(anyhow::anyhow!("efi doesn't end with .efi"))?
118-
.to_string())
121+
.ok_or_else(|| anyhow::anyhow!("efi doesn't end with .efi"))?
122+
.to_string();
123+
124+
Ok(name)
119125
}
120126
}
121127

0 commit comments

Comments
 (0)