Skip to content

Commit fd099cb

Browse files
Use OnceLock to store the detected bootloader
We use the `get_bootloader` function frequently and the value will never change so just cache it Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent c29c97e commit fd099cb

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

crates/lib/src/bootc_composefs/status.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -405,22 +405,30 @@ pub(crate) async fn get_container_manifest_and_config(
405405

406406
#[context("Getting bootloader")]
407407
pub(crate) fn get_bootloader() -> Result<Bootloader> {
408-
match read_uefi_var(EFI_LOADER_INFO) {
408+
static BOOTLOADER: OnceLock<Bootloader> = OnceLock::new();
409+
410+
if let Some(bootloader) = BOOTLOADER.get() {
411+
return Ok(*bootloader);
412+
}
413+
414+
let bootloader = match read_uefi_var(EFI_LOADER_INFO) {
409415
Ok(loader) => {
410416
if loader.to_lowercase().contains("systemd-boot") {
411-
return Ok(Bootloader::Systemd);
417+
Bootloader::Systemd
418+
} else {
419+
Bootloader::Grub
412420
}
413-
414-
return Ok(Bootloader::Grub);
415421
}
416422

417423
Err(efi_error) => match efi_error {
418-
EfiError::SystemNotUEFI => return Ok(Bootloader::Grub),
419-
EfiError::MissingVar => return Ok(Bootloader::Grub),
420-
421-
e => return Err(anyhow::anyhow!("Failed to read EfiLoaderInfo: {e:?}")),
424+
EfiError::SystemNotUEFI | EfiError::MissingVar => Bootloader::Grub,
425+
e => anyhow::bail!("Failed to read EfiLoaderInfo: {e:?}"),
422426
},
423-
}
427+
};
428+
429+
BOOTLOADER.get_or_init(|| bootloader);
430+
431+
return Ok(bootloader);
424432
}
425433

426434
/// Retrieves the OCI manifest and config for a deployment from the composefs repository.

crates/lib/src/spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ pub struct BootEntryOstree {
230230

231231
/// Bootloader type to determine whether system was booted via Grub or Systemd
232232
#[derive(
233-
clap::ValueEnum, Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema,
233+
clap::ValueEnum, Debug, Default, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema,
234234
)]
235235
#[serde(rename_all = "kebab-case")]
236236
pub enum Bootloader {

0 commit comments

Comments
 (0)