Skip to content

Commit 751c2bb

Browse files
bootloader: Introduce BootloaderKind enum
The two variants in this enum distinguish between Grub classic and BLS compatible bootloader (GrubCC and SystemdBoot). This cleans up the match statements where we perform the same op for GrubCC and SystemdBoot Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent 6b0e715 commit 751c2bb

9 files changed

Lines changed: 68 additions & 61 deletions

File tree

crates/lib/src/bootc_composefs/backwards_compat/bcompat_boot.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
TYPE1_ENT_PATH_STAGED, UKI_NAME_PREFIX, USER_CFG_STAGED,
1818
},
1919
parsers::bls_config::{BLSConfig, BLSConfigType},
20-
spec::Bootloader,
20+
spec::BootloaderKind,
2121
store::Storage,
2222
};
2323
use anyhow::{Context, Result};
@@ -323,8 +323,8 @@ pub(crate) async fn prepend_custom_prefix(
323323
handle_bls_conf(storage, cfs_cmdline, boot_dir, false)?;
324324
}
325325

326-
BootType::Uki => match bootloader {
327-
Bootloader::Grub => {
326+
BootType::Uki => match bootloader.kind()? {
327+
BootloaderKind::GRUBClassic => {
328328
let esp = storage.require_esp()?;
329329

330330
let mut buf = String::new();
@@ -384,11 +384,9 @@ pub(crate) async fn prepend_custom_prefix(
384384
rename_exchange_user_cfg(&grub_dir)?;
385385
}
386386

387-
Bootloader::Systemd | Bootloader::GrubCC => {
387+
BootloaderKind::BLSCompatible => {
388388
handle_bls_conf(storage, cfs_cmdline, boot_dir, true)?;
389389
}
390-
391-
Bootloader::None => unreachable!("Checked at install time"),
392390
},
393391
};
394392

crates/lib/src/bootc_composefs/boot.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ use crate::bootc_composefs::status::ComposefsCmdline;
9696
use crate::bootc_kargs::compute_new_kargs;
9797
use crate::composefs_consts::{TYPE1_BOOT_DIR_PREFIX, TYPE1_ENT_PATH, TYPE1_ENT_PATH_STAGED};
9898
use crate::parsers::bls_config::{BLSConfig, BLSConfigType};
99+
use crate::spec::BootloaderKind;
99100
use crate::task::Task;
100101
use crate::{bootc_composefs::repo::open_composefs_repo, store::Storage};
101102
use crate::{bootc_composefs::status::get_sorted_grub_uki_boot_entries, install::PostFetchState};
@@ -582,8 +583,8 @@ pub(crate) fn setup_composefs_bls_boot(
582583

583584
compute_new_kargs(mounted_erofs, current_root, &mut cmdline_refs)?;
584585

585-
let (entry_paths, _tmpdir_guard) = match bootloader {
586-
Bootloader::Grub => {
586+
let (entry_paths, _tmpdir_guard) = match bootloader.kind()? {
587+
BootloaderKind::GRUBClassic => {
587588
let root = Dir::open_ambient_dir(&root_path, ambient_authority())
588589
.context("Opening root path")?;
589590

@@ -607,7 +608,7 @@ pub(crate) fn setup_composefs_bls_boot(
607608
)
608609
}
609610

610-
Bootloader::Systemd | Bootloader::GrubCC => {
611+
BootloaderKind::BLSCompatible => {
611612
let efi_mount = mount_esp(&esp_device).context("Mounting ESP")?;
612613

613614
let mounted_efi = Utf8PathBuf::from(efi_mount.dir.path().as_str()?);
@@ -622,8 +623,6 @@ pub(crate) fn setup_composefs_bls_boot(
622623
Some(efi_mount),
623624
)
624625
}
625-
626-
Bootloader::None => unreachable!("Checked at install time"),
627626
};
628627

629628
let (bls_config, boot_digest, os_id) = match &entry {
@@ -1164,16 +1163,14 @@ pub(crate) fn setup_composefs_uki_boot(
11641163

11651164
let boot_digest = uki_info.boot_digest.clone();
11661165

1167-
match bootloader {
1168-
Bootloader::Grub => {
1166+
match bootloader.kind()? {
1167+
BootloaderKind::GRUBClassic => {
11691168
write_grub_uki_menuentry(root_path, &setup_type, uki_info.boot_label, id, &esp_device)?
11701169
}
11711170

1172-
Bootloader::Systemd | Bootloader::GrubCC => {
1171+
BootloaderKind::BLSCompatible => {
11731172
write_systemd_uki_config(&esp_mount.fd, &setup_type, uki_info, id)?
11741173
}
1175-
1176-
Bootloader::None => unreachable!("Checked at install time"),
11771174
};
11781175

11791176
Ok(boot_digest)

crates/lib/src/bootc_composefs/delete.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
TYPE1_ENT_PATH, TYPE1_ENT_PATH_STAGED, USER_CFG_STAGED,
1616
},
1717
parsers::bls_config::{BLSConfigType, parse_bls_config},
18-
spec::{BootEntry, Bootloader, DeploymentEntry},
18+
spec::{BootEntry, BootloaderKind, DeploymentEntry},
1919
status::Slot,
2020
store::{BootedComposefs, Storage},
2121
};
@@ -145,20 +145,18 @@ fn delete_depl_boot_entries(
145145
) -> Result<()> {
146146
let boot_dir = storage.require_boot_dir()?;
147147

148-
match deployment.deployment.bootloader {
149-
Bootloader::Grub => match deployment.deployment.boot_type {
148+
match deployment.deployment.bootloader.kind()? {
149+
BootloaderKind::GRUBClassic => match deployment.deployment.boot_type {
150150
BootType::Bls => delete_type1_conf_file(deployment, boot_dir, deleting_staged),
151151
BootType::Uki => {
152152
remove_grub_menucfg_entry(&deployment.deployment.verity, boot_dir, deleting_staged)
153153
}
154154
},
155155

156-
Bootloader::Systemd | Bootloader::GrubCC => {
156+
BootloaderKind::BLSCompatible => {
157157
// For Systemd UKI as well, we use .conf files
158158
delete_type1_conf_file(deployment, boot_dir, deleting_staged)
159159
}
160-
161-
Bootloader::None => unreachable!("Checked at install time"),
162160
}
163161
}
164162

crates/lib/src/bootc_composefs/finalize.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::bootc_composefs::gc::{GCOpts, composefs_gc};
55
use crate::bootc_composefs::rollback::{rename_exchange_bls_entries, rename_exchange_user_cfg};
66
use crate::bootc_composefs::status::get_composefs_status;
77
use crate::composefs_consts::STATE_DIR_ABS;
8-
use crate::spec::Bootloader;
8+
use crate::spec::BootloaderKind;
99
use crate::store::{BootedComposefs, Storage};
1010
use anyhow::{Context, Result};
1111
use bootc_initramfs_setup::mount_composefs_image;
@@ -131,21 +131,19 @@ pub(crate) async fn composefs_backend_finalize(
131131

132132
let boot_dir = storage.require_boot_dir()?;
133133

134-
match booted_composefs.bootloader {
135-
Bootloader::Grub => match staged_composefs.boot_type {
134+
match booted_composefs.bootloader.kind()? {
135+
BootloaderKind::GRUBClassic => match staged_composefs.boot_type {
136136
BootType::Bls => {
137137
let entries_dir = boot_dir.open_dir("loader")?;
138138
rename_exchange_bls_entries(&entries_dir)?;
139139
}
140140
BootType::Uki => finalize_staged_grub_uki(boot_dir)?,
141141
},
142142

143-
Bootloader::Systemd | Bootloader::GrubCC => {
143+
BootloaderKind::BLSCompatible => {
144144
let entries_dir = boot_dir.open_dir("loader")?;
145145
rename_exchange_bls_entries(&entries_dir)?;
146146
}
147-
148-
Bootloader::None => unreachable!("Checked at install time"),
149147
};
150148

151149
// Now that we have successfully updated bootloader entires, we can GC the unreferenced ones

crates/lib/src/bootc_composefs/rollback.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::bootc_composefs::boot::{
1212
};
1313
use crate::bootc_composefs::status::{get_composefs_status, get_sorted_type1_boot_entries};
1414
use crate::composefs_consts::TYPE1_ENT_PATH_STAGED;
15-
use crate::spec::Bootloader;
15+
use crate::spec::{Bootloader, BootloaderKind};
1616
use crate::store::{BootedComposefs, Storage};
1717
use crate::{
1818
bootc_composefs::{boot::get_efi_uuid_source, status::get_sorted_grub_uki_boot_entries},
@@ -224,8 +224,8 @@ pub(crate) async fn composefs_rollback(
224224

225225
let boot_dir = storage.require_boot_dir()?;
226226

227-
match &rollback_entry.bootloader {
228-
Bootloader::Grub => match rollback_entry.boot_type {
227+
match &rollback_entry.bootloader.kind()? {
228+
BootloaderKind::GRUBClassic => match rollback_entry.boot_type {
229229
BootType::Bls => {
230230
rollback_composefs_entries(boot_dir, rollback_entry.bootloader.clone())?;
231231
}
@@ -234,12 +234,10 @@ pub(crate) async fn composefs_rollback(
234234
}
235235
},
236236

237-
Bootloader::Systemd | Bootloader::GrubCC => {
237+
BootloaderKind::BLSCompatible => {
238238
// We use BLS entries for systemd UKI as well
239239
rollback_composefs_entries(boot_dir, rollback_entry.bootloader.clone())?;
240240
}
241-
242-
Bootloader::None => unreachable!("Checked at install time"),
243241
}
244242

245243
if reverting {

crates/lib/src/bootc_composefs/status.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
bls_config::{BLSConfig, BLSConfigType, parse_bls_config},
2626
grub_menuconfig::{MenuEntry, parse_grub_menuentry_file},
2727
},
28-
spec::{BootEntry, BootOrder, Host, HostSpec, ImageStatus},
28+
spec::{BootEntry, BootOrder, BootloaderKind, Host, HostSpec, ImageStatus},
2929
store::Storage,
3030
utils::{EfiError, read_uefi_var},
3131
};
@@ -339,8 +339,8 @@ pub(crate) fn list_bootloader_entries(storage: &Storage) -> Result<Vec<Bootloade
339339
let bootloader = get_bootloader()?;
340340
let boot_dir = storage.require_boot_dir()?;
341341

342-
let entries = match bootloader {
343-
Bootloader::Grub => {
342+
let entries = match bootloader.kind()? {
343+
BootloaderKind::GRUBClassic => {
344344
// Grub entries are always in boot
345345
let grub_dir = boot_dir.open_dir("grub2").context("Opening grub dir")?;
346346

@@ -368,9 +368,7 @@ pub(crate) fn list_bootloader_entries(storage: &Storage) -> Result<Vec<Bootloade
368368
}
369369
}
370370

371-
Bootloader::Systemd | Bootloader::GrubCC => list_type1_entries(boot_dir)?,
372-
373-
Bootloader::None => unreachable!("Checked at install time"),
371+
BootloaderKind::BLSCompatible => list_type1_entries(boot_dir)?,
374372
};
375373

376374
Ok(entries)
@@ -873,8 +871,11 @@ async fn composefs_deployment_status_from(
873871
let booted_cfs = host.require_composefs_booted()?;
874872

875873
let mut grub_menu_string = String::new();
876-
let (is_rollback_queued, sorted_bls_config, grub_menu_entries) = match booted_cfs.bootloader {
877-
Bootloader::Grub => match boot_type {
874+
let (is_rollback_queued, sorted_bls_config, grub_menu_entries) = match booted_cfs
875+
.bootloader
876+
.kind()?
877+
{
878+
BootloaderKind::GRUBClassic => match boot_type {
878879
BootType::Bls => {
879880
let bls_configs = get_sorted_type1_boot_entries(boot_dir, false)?;
880881
let bls_config = bls_configs
@@ -915,7 +916,7 @@ async fn composefs_deployment_status_from(
915916
},
916917

917918
// We will have BLS stuff and the UKI stuff in the same DIR
918-
Bootloader::Systemd | Bootloader::GrubCC => {
919+
BootloaderKind::BLSCompatible => {
919920
let bls_configs = get_sorted_type1_boot_entries(boot_dir, true)?;
920921
let bls_config = bls_configs
921922
.first()
@@ -938,8 +939,6 @@ async fn composefs_deployment_status_from(
938939

939940
(is_rollback_queued, Some(bls_configs), None)
940941
}
941-
942-
Bootloader::None => unreachable!("Checked at install time"),
943942
};
944943

945944
// Determine rollback deployment by matching extra deployment boot entries against entires read from /boot

crates/lib/src/bootc_composefs/update.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ocidir::cap_std::ambient_authority;
1212
use ostree_ext::container::ManifestDiff;
1313

1414
use crate::bootc_composefs::gc::GCOpts;
15+
use crate::spec::BootloaderKind;
1516
use crate::{
1617
bootc_composefs::{
1718
boot::{BootSetupType, BootType, setup_composefs_bls_boot, setup_composefs_uki_boot},
@@ -30,7 +31,7 @@ use crate::{
3031
COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR, STATE_DIR_RELATIVE,
3132
TYPE1_ENT_PATH_STAGED, USER_CFG_STAGED,
3233
},
33-
spec::{Bootloader, Host, ImageReference},
34+
spec::{Host, ImageReference},
3435
store::{BootedComposefs, ComposefsRepository, Storage},
3536
};
3637

@@ -170,8 +171,8 @@ pub(crate) fn validate_update(
170171

171172
// Remove staged bootloader entries, if any
172173
// GC should take care of the UKI PEs and other binaries
173-
match get_bootloader()? {
174-
Bootloader::Grub => match booted.boot_type {
174+
match get_bootloader()?.kind()? {
175+
BootloaderKind::GRUBClassic => match booted.boot_type {
175176
BootType::Bls => rm_staged_type1_ent(boot_dir)?,
176177

177178
BootType::Uki => {
@@ -184,9 +185,7 @@ pub(crate) fn validate_update(
184185
}
185186
},
186187

187-
Bootloader::Systemd | Bootloader::GrubCC => rm_staged_type1_ent(boot_dir)?,
188-
189-
Bootloader::None => unreachable!("Checked at install time"),
188+
BootloaderKind::BLSCompatible => rm_staged_type1_ent(boot_dir)?,
190189
}
191190

192191
// Remove state directory

crates/lib/src/spec.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ pub enum Bootloader {
245245
None,
246246
}
247247

248+
#[derive(Debug)]
249+
pub enum BootloaderKind {
250+
/// Bootloader that support Bootloader Specification
251+
/// GrubCC and SystemdBoot
252+
BLSCompatible,
253+
/// Classic Grub
254+
GRUBClassic,
255+
}
256+
248257
impl Display for Bootloader {
249258
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
250259
let string = match self {
@@ -272,6 +281,18 @@ impl FromStr for Bootloader {
272281
}
273282
}
274283

284+
impl Bootloader {
285+
/// Returns whether the Bootloader is BLSCompatible
286+
/// Throws and error if Bootloader is None
287+
pub(crate) fn kind(&self) -> Result<BootloaderKind> {
288+
match self {
289+
Bootloader::Grub => Ok(BootloaderKind::GRUBClassic),
290+
Bootloader::Systemd | Bootloader::GrubCC => Ok(BootloaderKind::BLSCompatible),
291+
Bootloader::None => anyhow::bail!("Bootloader was None"),
292+
}
293+
}
294+
}
295+
275296
/// A bootable entry
276297
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
277298
#[serde(rename_all = "camelCase")]

crates/lib/src/store/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
9393
use std::cell::OnceCell;
9494
use std::ops::Deref;
95-
use std::os::fd::{AsFd, AsRawFd};
9695
use std::sync::Arc;
9796

9897
use anyhow::{Context, Result};
@@ -119,7 +118,7 @@ use crate::bootc_composefs::boot::{EFI_LINUX, mount_esp};
119118
use crate::bootc_composefs::status::{ComposefsCmdline, composefs_booted, get_bootloader};
120119
use crate::lsm;
121120
use crate::podstorage::CStorage;
122-
use crate::spec::{Bootloader, ImageStatus};
121+
use crate::spec::{BootloaderKind, ImageStatus};
123122
use crate::utils::{deployment_fd, open_dir_remount_rw};
124123

125124
/// See <https://github.com/containers/composefs-rs/issues/159>
@@ -333,13 +332,14 @@ impl BootedStorage {
333332
let esp_dev = root_dev.find_first_colocated_esp()?;
334333
let esp_mount = mount_esp(&esp_dev.path())?;
335334

336-
let boot_dir = match get_bootloader()? {
337-
Bootloader::Grub => physical_root.open_dir("boot").context("Opening boot")?,
335+
let boot_dir = match get_bootloader()?.kind()? {
336+
BootloaderKind::GRUBClassic => {
337+
physical_root.open_dir("boot").context("Opening boot")?
338+
}
338339
// NOTE: Handle XBOOTLDR partitions here if and when we use it
339-
Bootloader::Systemd | Bootloader::GrubCC => {
340+
BootloaderKind::BLSCompatible => {
340341
esp_mount.fd.try_clone().context("Cloning fd")?
341342
}
342-
Bootloader::None => unreachable!("Checked at install time"),
343343
};
344344

345345
let storage = Storage {
@@ -527,16 +527,15 @@ impl Storage {
527527

528528
// boot dir in case of systemd-boot points to the ESP, but we store
529529
// the actual binaries inside ESP/EFI/Linux
530-
let boot_dir = match get_bootloader()? {
531-
Bootloader::Grub => boot_dir.try_clone()?,
532-
Bootloader::Systemd | Bootloader::GrubCC => {
530+
let boot_dir = match get_bootloader()?.kind()? {
531+
BootloaderKind::GRUBClassic => boot_dir.try_clone()?,
532+
BootloaderKind::BLSCompatible => {
533533
let boot_dir = boot_dir
534534
.open_dir(EFI_LINUX)
535535
.with_context(|| format!("Opening {EFI_LINUX}"))?;
536536

537537
boot_dir
538538
}
539-
Bootloader::None => anyhow::bail!("Unknown bootloader"),
540539
};
541540

542541
Ok(boot_dir)

0 commit comments

Comments
 (0)