1
1
use std:: fmt:: Write ;
2
+ use std:: io:: Read ;
2
3
use std:: os:: unix:: io:: AsRawFd ;
3
4
use std:: path:: { Path , PathBuf } ;
4
5
@@ -25,6 +26,7 @@ const GRUBCONFIG_FILE_MODE: u32 = 0o600;
25
26
#[ context( "Installing static GRUB configs" ) ]
26
27
pub ( crate ) fn install (
27
28
target_root : & openat:: Dir ,
29
+ src_root : Option < & openat:: Dir > ,
28
30
installed_efi_vendor : Option < & str > ,
29
31
write_uuid : bool ,
30
32
) -> Result < ( ) > {
@@ -40,12 +42,23 @@ pub(crate) fn install(
40
42
bootdir. create_dir ( GRUB2DIR , 0o700 ) ?;
41
43
}
42
44
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
+
43
53
let mut config = String :: from ( "# Generated by bootupd / do not edit\n \n " ) ;
44
54
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) ?;
46
59
config. push_str ( pre. as_str ( ) ) ;
47
60
48
- let dropindir = openat :: Dir :: open ( & Path :: new ( CONFIGDIR ) . join ( DROPINDIR ) ) ?;
61
+ let dropindir = configdir . sub_dir ( Path :: new ( DROPINDIR ) ) ?;
49
62
// Sort the files for reproducibility
50
63
let mut entries = dropindir
51
64
. list_dir ( "." ) ?
@@ -62,7 +75,9 @@ pub(crate) fn install(
62
75
continue ;
63
76
}
64
77
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
+
66
81
config. push_str ( dropin. as_str ( ) ) ;
67
82
writeln ! ( config, "### END {name} ###" ) ?;
68
83
println ! ( "Added {name}" ) ;
@@ -103,8 +118,8 @@ pub(crate) fn install(
103
118
. sub_dir_optional ( "boot/efi/EFI" )
104
119
. context ( "Opening /boot/efi/EFI" ) ?;
105
120
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)
108
123
. context ( "Copying static EFI" ) ?;
109
124
println ! ( "Installed: {target:?}" ) ;
110
125
if let Some ( uuid_path) = uuid_path {
@@ -155,7 +170,7 @@ mod tests {
155
170
std:: fs:: create_dir_all ( tdp. join ( "boot/grub2" ) ) ?;
156
171
std:: fs:: create_dir_all ( tdp. join ( "boot/efi/EFI/BOOT" ) ) ?;
157
172
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 ( ) ;
159
174
160
175
assert ! ( td. exists( "boot/grub2/grub.cfg" ) ?) ;
161
176
assert ! ( td. exists( "boot/efi/EFI/fedora/grub.cfg" ) ?) ;
0 commit comments