Skip to content

Commit 136964b

Browse files
esp: Handle non-BLS bootloaders
Older versions of Grub, the one in c10s and c9s, don't fully implement the BLS spec, which means we don't get the efivar LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f. In such a case, go through all the ESPs and try to find bootc owned BLS config file in `ESP/loader/entries` Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent b078198 commit 136964b

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

‎crates/lib/src/bootc_composefs/boot.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ use serde::{Deserialize, Serialize};
101101
use crate::bootc_composefs::state::{get_booted_bls, write_composefs_state};
102102
use crate::bootc_composefs::status::build_composefs_karg;
103103
use crate::bootc_kargs::compute_new_kargs;
104-
use crate::composefs_consts::{TYPE1_BOOT_DIR_PREFIX, TYPE1_ENT_PATH, TYPE1_ENT_PATH_STAGED};
104+
use crate::composefs_consts::{
105+
BLS_ENTRY_FILE_PREFIX, TYPE1_BOOT_DIR_PREFIX, TYPE1_ENT_PATH, TYPE1_ENT_PATH_STAGED,
106+
};
105107
use crate::parsers::bls_config::{BLSConfig, BLSConfigType, EFIKey};
106108
use crate::spec::BootloaderKind;
107109
use crate::store::find_booted_from_esp;
@@ -422,7 +424,7 @@ const ESP_MOUNT_DATA: &std::ffi::CStr = c"fmask=0177,dmask=0077";
422424
/// is already mounted in the current mount namespace; callers should use
423425
/// [`mount_esp_readonly`] or [`mount_esp_writable`] instead of this primitive
424426
/// so that pre-existing mounts are handled.
425-
fn mount_esp(device: &str) -> Result<TempMount> {
427+
pub fn mount_esp(device: &str) -> Result<TempMount> {
426428
TempMount::mount_dev(device, "vfat", ESP_MOUNT_FLAGS, Some(ESP_MOUNT_DATA))
427429
}
428430

@@ -519,7 +521,7 @@ pub fn type1_entry_conf_file_name(
519521
priority: &str,
520522
) -> String {
521523
let os_id_safe = os_id.replace('-', "_");
522-
format!("bootc_{os_id_safe}-{version}-{priority}.conf")
524+
format!("{BLS_ENTRY_FILE_PREFIX}{os_id_safe}-{version}-{priority}.conf")
523525
}
524526

525527
/// Generate sort key for the primary (new/upgraded) boot entry.

‎crates/lib/src/composefs_consts.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub(crate) const BOOTC_FINALIZE_STAGED_SERVICE: &str = "bootc-finalize-staged.se
4242
/// The prefix for the directories containing kernel + initrd
4343
pub(crate) const TYPE1_BOOT_DIR_PREFIX: &str = "bootc_composefs-";
4444

45+
/// The prefix for BLS entry config files
46+
pub(crate) const BLS_ENTRY_FILE_PREFIX: &str = "bootc_";
47+
4548
/// The prefix for names of UKI and UKI Addons
4649
pub(crate) const UKI_NAME_PREFIX: &str = TYPE1_BOOT_DIR_PREFIX;
4750

‎crates/lib/src/store/mod.rs‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,14 @@ use composefs::repository::{RepositoryConfig, RepositoryOpenError};
117117
use composefs_ctl::composefs;
118118

119119
use crate::bootc_composefs::backwards_compat::bcompat_boot::prepend_custom_prefix;
120-
use crate::bootc_composefs::boot::{EFI_LINUX, mount_esp_readonly, mount_esp_writable};
120+
use crate::bootc_composefs::boot::{EFI_LINUX, mount_esp, mount_esp_readonly, mount_esp_writable};
121121
use crate::bootc_composefs::status::{ComposefsCmdline, composefs_booted, get_bootloader};
122+
use crate::composefs_consts::{BLS_ENTRY_FILE_PREFIX, TYPE1_ENT_PATH};
122123
use crate::install::BOOT;
123124
use crate::lsm;
124125
use crate::podstorage::CStorage;
125126
use crate::spec::{BootloaderKind, ImageStatus};
126-
use crate::utils::{deployment_fd, open_dir_remount_rw, read_uefi_var};
127+
use crate::utils::{EfiError, deployment_fd, open_dir_remount_rw, read_uefi_var};
127128

128129
/// See <https://github.com/containers/composefs-rs/issues/159>
129130
pub type ComposefsRepository = composefs::repository::Repository<Sha512HashValue>;
@@ -499,12 +500,64 @@ const LOADER_DEVICE_PART_UUID: &str = "LoaderDevicePartUUID-4a67b082-0a4c-41cf-b
499500
/// the wrong ESP in dual booted systems
500501
#[context("Finding ESP used for booting")]
501502
pub(crate) fn find_booted_from_esp(physical_root: &Dir) -> Result<String> {
502-
let device_uuid = read_uefi_var(LOADER_DEVICE_PART_UUID)
503-
.map_err(|e| anyhow::anyhow!("Reading {LOADER_DEVICE_PART_UUID}: {e:?}"))?;
504-
505503
let root_dev = bootc_blockdev::list_dev_by_dir(&physical_root)?;
506504
let all_roots = root_dev.find_all_roots()?;
507505

506+
let device_uuid = match read_uefi_var(LOADER_DEVICE_PART_UUID) {
507+
Ok(u) => u,
508+
// Older version of Grub (in c10s) do not include this efivar
509+
// We can't rely on ESP being mounted on /boot, /boot/efi as older Grub
510+
// doesn't fully support BLS spec
511+
Err(EfiError::MissingVar) => {
512+
tracing::debug!("Missing {LOADER_DEVICE_PART_UUID}, searching through all ESPs...");
513+
514+
let all_esps = root_dev
515+
.find_colocated_esps()?
516+
.ok_or_else(|| anyhow::anyhow!("No ESP found"))?;
517+
518+
if all_esps.len() == 1 {
519+
// If only one ESP, there's no ambiguity
520+
return Ok(all_esps.first().unwrap().path());
521+
}
522+
523+
for esp in all_esps {
524+
let tmp_mount = mount_esp(&esp.path()).context("Mounting ESP")?;
525+
526+
// Check if we have bootc owned entries in loader/entries
527+
let entries = tmp_mount
528+
.fd
529+
.open_dir_optional(TYPE1_ENT_PATH)
530+
.context("Opening entries dir")?;
531+
532+
let Some(entries_dir) = entries else {
533+
tracing::debug!("No entries found in ESP {}", esp.path());
534+
continue;
535+
};
536+
537+
for entry in entries_dir
538+
.entries_utf8()
539+
.context("Reading directory entries")?
540+
{
541+
let entry = entry?;
542+
543+
// We can be fairly sure we put our entries in this ESP
544+
if entry.file_name()?.starts_with(BLS_ENTRY_FILE_PREFIX) {
545+
return Ok(esp.path());
546+
}
547+
}
548+
549+
tracing::debug!("ESP {} not owned by us", esp.path());
550+
}
551+
552+
anyhow::bail!("Failed to find bootc owned ESP");
553+
}
554+
Err(e) => {
555+
anyhow::bail!("Reading {LOADER_DEVICE_PART_UUID}: {e:?}")
556+
}
557+
};
558+
559+
tracing::debug!("Found {device_uuid} in {LOADER_DEVICE_PART_UUID}");
560+
508561
for root in all_roots {
509562
let Some(children) = root.children else {
510563
continue;

0 commit comments

Comments
 (0)