11use std:: fmt:: Write ;
2+ use std:: io:: Read ;
23use std:: os:: unix:: io:: AsRawFd ;
34use std:: path:: { Path , PathBuf } ;
45
@@ -25,6 +26,7 @@ const GRUBCONFIG_FILE_MODE: u32 = 0o600;
2526#[ context( "Installing static GRUB configs" ) ]
2627pub ( crate ) fn install (
2728 target_root : & openat:: Dir ,
29+ src_root : Option < & openat:: Dir > ,
2830 installed_efi_vendor : Option < & str > ,
2931 write_uuid : bool ,
3032) -> Result < ( ) > {
@@ -40,12 +42,23 @@ pub(crate) fn install(
4042 bootdir. create_dir ( GRUB2DIR , 0o700 ) ?;
4143 }
4244
45+ let configdir = if let Some ( src_root) = src_root {
46+ // strip the leading `/` to make the path relative to the given
47+ // source root
48+ src_root. sub_dir ( Path :: new ( CONFIGDIR ) . strip_prefix ( "/" ) ?) ?
49+ } else {
50+ openat:: Dir :: open ( Path :: new ( CONFIGDIR ) ) ?
51+ } ;
52+
4353 let mut config = String :: from ( "# Generated by bootupd / do not edit\n \n " ) ;
4454
45- let pre = std:: fs:: read_to_string ( Path :: new ( CONFIGDIR ) . join ( "grub-static-pre.cfg" ) ) ?;
55+ let mut pre = String :: new ( ) ;
56+ configdir
57+ . open_file ( "grub-static-pre.cfg" ) ?
58+ . read_to_string ( & mut pre) ?;
4659 config. push_str ( pre. as_str ( ) ) ;
4760
48- let dropindir = openat :: Dir :: open ( & Path :: new ( CONFIGDIR ) . join ( DROPINDIR ) ) ?;
61+ let dropindir = configdir . sub_dir ( Path :: new ( DROPINDIR ) ) ?;
4962 // Sort the files for reproducibility
5063 let mut entries = dropindir
5164 . list_dir ( "." ) ?
@@ -62,7 +75,9 @@ pub(crate) fn install(
6275 continue ;
6376 }
6477 writeln ! ( config, "\n ### BEGIN {name} ###" ) ?;
65- let dropin = std:: fs:: read_to_string ( Path :: new ( CONFIGDIR ) . join ( DROPINDIR ) . join ( name) ) ?;
78+ let mut dropin = String :: new ( ) ;
79+ dropindir. open_file ( name) ?. read_to_string ( & mut dropin) ?;
80+
6681 config. push_str ( dropin. as_str ( ) ) ;
6782 writeln ! ( config, "### END {name} ###" ) ?;
6883 println ! ( "Added {name}" ) ;
@@ -103,8 +118,8 @@ pub(crate) fn install(
103118 . sub_dir_optional ( "boot/efi/EFI" )
104119 . context ( "Opening /boot/efi/EFI" ) ?;
105120 if let Some ( efidir) = dest_efidir {
106- efidir
107- . copy_file ( & Path :: new ( CONFIGDIR ) . join ( "grub-static-efi.cfg" ) , target)
121+ configdir
122+ . copy_file_at ( "grub-static-efi.cfg" , & efidir , target)
108123 . context ( "Copying static EFI" ) ?;
109124 println ! ( "Installed: {target:?}" ) ;
110125 if let Some ( uuid_path) = uuid_path {
@@ -155,7 +170,7 @@ mod tests {
155170 std:: fs:: create_dir_all ( tdp. join ( "boot/grub2" ) ) ?;
156171 std:: fs:: create_dir_all ( tdp. join ( "boot/efi/EFI/BOOT" ) ) ?;
157172 std:: fs:: create_dir_all ( tdp. join ( "boot/efi/EFI/fedora" ) ) ?;
158- install ( & td, Some ( "fedora" ) , false ) . unwrap ( ) ;
173+ install ( & td, None , Some ( "fedora" ) , false ) . unwrap ( ) ;
159174
160175 assert ! ( td. exists( "boot/grub2/grub.cfg" ) ?) ;
161176 assert ! ( td. exists( "boot/efi/EFI/fedora/grub.cfg" ) ?) ;
0 commit comments