@@ -14,6 +14,7 @@ use bootc_internal_utils::CommandRunExt;
1414use camino:: { Utf8Path , Utf8PathBuf } ;
1515use cap_std:: fs:: Dir ;
1616use cap_std_ext:: cap_std;
17+ use cap_std_ext:: dirext:: CapStdExtDirExt ;
1718use chrono:: prelude:: * ;
1819use fn_error_context:: context;
1920use openat_ext:: OpenatDirExt ;
@@ -451,20 +452,41 @@ impl Component for Efi {
451452 }
452453
453454 fn generate_update_metadata ( & self , sysroot : & str ) -> Result < ContentMetadata > {
454- let sysroot_path = Utf8Path :: new ( sysroot) ;
455+ let sysroot_path = Path :: new ( sysroot) ;
456+ let sysroot_dir = Dir :: open_ambient_dir ( sysroot_path, cap_std:: ambient_authority ( ) ) ?;
455457
456- let efilib_path = sysroot_path. join ( EFILIB ) ;
457- let efi_comps = if efilib_path. exists ( ) {
458- get_efi_component_from_usr ( & sysroot_path, EFILIB ) ?
459- } else {
460- None
461- } ;
458+ if let Some ( ostreeboot) = sysroot_dir
459+ . open_dir_optional ( ostreeutil:: BOOT_PREFIX )
460+ . context ( "Opening usr/lib/ostree-boot" ) ?
461+ {
462+ let cruft = [ "loader" , "grub2" ] ;
463+ for p in cruft. iter ( ) {
464+ ostreeboot. remove_all_optional ( p) ?;
465+ }
466+
467+ let efisrc = sysroot_path. join ( ostreeutil:: BOOT_PREFIX ) . join ( "efi/EFI" ) ;
468+ if efisrc. exists ( ) {
469+ let files = WalkDir :: new ( & efisrc)
470+ . into_iter ( )
471+ . filter_map ( Result :: ok)
472+ . filter ( |e| e. file_type ( ) . is_file ( ) )
473+ . map ( |e| e. path ( ) . to_path_buf ( ) )
474+ . collect :: < Vec < _ > > ( ) ;
475+
476+ if !files. is_empty ( ) {
477+ transfter_ostree_boot_to_usr ( sysroot_path, & files) ?;
478+ }
479+ // Remove usr/lib/ostree-boot/efi/EFI dir (after transfer) or if it is empty
480+ ostreeboot. remove_all_optional ( "efi/EFI" ) ?;
481+ }
482+ }
462483
463484 // copy EFI files to updates dir from usr/lib/efi
464- let meta = if let Some ( efi_components) = efi_comps {
485+ if let Some ( efi_components) =
486+ get_efi_component_from_usr ( Utf8Path :: from_path ( sysroot_path) . unwrap ( ) , EFILIB ) ?
487+ {
465488 let mut packages = Vec :: new ( ) ;
466489 let mut modules_vec: Vec < Module > = vec ! [ ] ;
467- let sysroot_dir = Dir :: open_ambient_dir ( sysroot_path, cap_std:: ambient_authority ( ) ) ?;
468490 for efi in efi_components {
469491 Command :: new ( "cp" )
470492 . args ( [ "-rp" , "--reflink=auto" ] )
@@ -482,50 +504,16 @@ impl Component for Efi {
482504
483505 // change to now to workaround https://github.com/coreos/bootupd/issues/933
484506 let timestamp = std:: time:: SystemTime :: now ( ) ;
485- ContentMetadata {
507+ let meta = ContentMetadata {
486508 timestamp : chrono:: DateTime :: < Utc > :: from ( timestamp) ,
487509 version : packages. join ( "," ) ,
488510 versions : Some ( modules_vec) ,
489- }
511+ } ;
512+ write_update_metadata ( sysroot, self , & meta) ?;
513+ Ok ( meta)
490514 } else {
491- let ostreebootdir = sysroot_path. join ( ostreeutil:: BOOT_PREFIX ) ;
492-
493- // move EFI files to updates dir from /usr/lib/ostree-boot
494- if ostreebootdir. exists ( ) {
495- let cruft = [ "loader" , "grub2" ] ;
496- for p in cruft. iter ( ) {
497- let p = ostreebootdir. join ( p) ;
498- if p. exists ( ) {
499- std:: fs:: remove_dir_all ( & p) ?;
500- }
501- }
502-
503- let efisrc = ostreebootdir. join ( "efi/EFI" ) ;
504- if !efisrc. exists ( ) {
505- bail ! ( "Failed to find {:?}" , & efisrc) ;
506- }
507-
508- let dest_efidir = component_updatedir ( sysroot, self ) ;
509- let dest_efidir =
510- Utf8PathBuf :: from_path_buf ( dest_efidir) . expect ( "Path is invalid UTF-8" ) ;
511- // Fork off mv() because on overlayfs one can't rename() a lower level
512- // directory today, and this will handle the copy fallback.
513- Command :: new ( "mv" ) . args ( [ & efisrc, & dest_efidir] ) . run ( ) ?;
514-
515- let efidir = openat:: Dir :: open ( dest_efidir. as_std_path ( ) )
516- . with_context ( || format ! ( "Opening {}" , dest_efidir) ) ?;
517- let files = crate :: util:: filenames ( & efidir) ?. into_iter ( ) . map ( |mut f| {
518- f. insert_str ( 0 , "/boot/efi/EFI/" ) ;
519- f
520- } ) ;
521- query_files ( sysroot, files) ?
522- } else {
523- anyhow:: bail!( "Failed to find {ostreebootdir}" ) ;
524- }
525- } ;
526-
527- write_update_metadata ( sysroot, self , & meta) ?;
528- Ok ( meta)
515+ anyhow:: bail!( "Failed to find EFI components" ) ;
516+ }
529517 }
530518
531519 fn query_update ( & self , sysroot : & openat:: Dir ) -> Result < Option < ContentMetadata > > {
0 commit comments