@@ -117,13 +117,14 @@ use composefs::repository::{RepositoryConfig, RepositoryOpenError};
117117use composefs_ctl:: composefs;
118118
119119use 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} ;
121121use crate :: bootc_composefs:: status:: { ComposefsCmdline , composefs_booted, get_bootloader} ;
122+ use crate :: composefs_consts:: { BLS_ENTRY_FILE_PREFIX , TYPE1_ENT_PATH } ;
122123use crate :: install:: BOOT ;
123124use crate :: lsm;
124125use crate :: podstorage:: CStorage ;
125126use 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>
129130pub 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" ) ]
501502pub ( 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