11use anyhow:: { bail, Context , Result } ;
22use camino:: Utf8PathBuf ;
33use openat_ext:: OpenatDirExt ;
4- #[ cfg( target_arch = "powerpc64" ) ]
5- use std:: borrow:: Cow ;
64use std:: io:: prelude:: * ;
75use std:: path:: Path ;
86use std:: process:: Command ;
@@ -20,27 +18,22 @@ use crate::packagesystem;
2018pub ( crate ) const GRUB_BIN : & str = "usr/sbin/grub2-install" ;
2119
2220#[ cfg( target_arch = "powerpc64" ) ]
23- fn target_device ( device : & str ) -> Result < Cow < str > > {
21+ fn target_device ( device : & Device ) -> Result < String > {
2422 const PREPBOOT_GUID : & str = "9E1A2D38-C612-4316-AA26-8B49521E5A8B" ;
2523 /// We make a best-effort to support MBR partitioning too.
2624 const PREPBOOT_MBR_TYPE : & str = "41" ;
2725
28- // Here we use lsblk to see if the device has any partitions at all
29- let dev = bootc_internal_blockdev:: list_dev ( device. into ( ) ) ?;
30- if dev. children . is_none ( ) {
31- return Ok ( device. into ( ) ) ;
32- } ;
33- // If it does, directly call `sfdisk` and bypass lsblk because inside a container
34- // we may not have all the cached udev state (that I think is in /run).
35- let device = bootc_internal_blockdev:: partitions_of ( device. into ( ) ) ?;
26+ if !device. has_children ( ) {
27+ return Ok ( device. path ( ) ) ;
28+ }
29+
3630 let prepdev = device
37- . partitions
38- . iter ( )
39- . find ( |p| matches ! ( p. parttype. as_str( ) , PREPBOOT_GUID | PREPBOOT_MBR_TYPE ) )
31+ . find_partition_of_type ( PREPBOOT_GUID )
32+ . or_else ( || device. find_partition_of_type ( PREPBOOT_MBR_TYPE ) )
4033 . ok_or_else ( || {
4134 anyhow:: anyhow!( "Failed to find PReP partition with GUID {PREPBOOT_GUID}" )
4235 } ) ?;
43- Ok ( prepdev. path ( ) . as_str ( ) . to_owned ( ) . into ( ) )
36+ Ok ( prepdev. path ( ) )
4437}
4538
4639#[ derive( Default ) ]
@@ -64,7 +57,7 @@ impl Bios {
6457 }
6558
6659 // Run grub2-install
67- fn run_grub_install ( & self , dest_root : & str , device : & str ) -> Result < ( ) > {
60+ fn run_grub_install ( & self , dest_root : & str , device : & Device ) -> Result < ( ) > {
6861 if !self . check_grub_modules ( ) ? {
6962 bail ! ( "Failed to find grub2-modules" ) ;
7063 }
@@ -82,15 +75,15 @@ impl Bios {
8275 cmd. args ( [ "--target" , "i386-pc" ] )
8376 . args ( [ "--boot-directory" , boot_dir. to_str ( ) . unwrap ( ) ] )
8477 . args ( [ "--modules" , "mdraid1x part_gpt" ] )
85- . arg ( device) ;
78+ . arg ( & device. path ( ) ) ;
8679
8780 #[ cfg( target_arch = "powerpc64" ) ]
8881 {
8982 let device = target_device ( device) ?;
9083 cmd. args ( & [ "--target" , "powerpc-ieee1275" ] )
9184 . args ( & [ "--boot-directory" , boot_dir. to_str ( ) . unwrap ( ) ] )
9285 . arg ( "--no-nvram" )
93- . arg ( & * device) ;
86+ . arg ( & device) ;
9487 }
9588
9689 let cmdout = cmd. output ( ) ?;
@@ -122,7 +115,7 @@ impl Component for Bios {
122115 anyhow:: bail!( "No update metadata for component {} found" , self . name( ) ) ;
123116 } ;
124117
125- self . run_grub_install ( dest_root, & device. path ( ) )
118+ self . run_grub_install ( dest_root, device)
126119 . with_context ( || format ! ( "installing GRUB on {}" , device. path( ) ) ) ?;
127120
128121 Ok ( InstalledContent {
@@ -228,7 +221,7 @@ impl Component for Bios {
228221 // multi-disk setups (e.g. RAID), grub must be written to every backing
229222 // disk's MBR/BIOS-boot partition so the system can boot from any one.
230223 for parent in rootcxt. device . find_all_roots ( ) ? {
231- self . run_grub_install ( rootcxt. path . as_str ( ) , & parent. path ( ) ) ?;
224+ self . run_grub_install ( rootcxt. path . as_str ( ) , & parent) ?;
232225 log:: debug!( "Installed grub modules on {}" , parent. path( ) ) ;
233226 }
234227
@@ -265,7 +258,7 @@ impl Component for Bios {
265258 . expect ( "update available" ) ;
266259
267260 for parent in rootcxt. device . find_all_roots ( ) ? {
268- self . run_grub_install ( rootcxt. path . as_str ( ) , & parent. path ( ) ) ?;
261+ self . run_grub_install ( rootcxt. path . as_str ( ) , & parent) ?;
269262 log:: debug!( "Installed grub modules on {}" , parent. path( ) ) ;
270263 }
271264
0 commit comments