Skip to content

Commit 42f5536

Browse files
authored
Merge pull request #990 from jbtrystram/src_root_static_configs
install: pull grubconfigs from the source root
2 parents cf75e14 + e855c6c commit 42f5536

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/bios.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Component for Bios {
190190
.context("Failed to backup GRUB config")?;
191191
}
192192

193-
crate::grubconfigs::install(&destdir, None, true)?;
193+
crate::grubconfigs::install(&destdir, None, None, true)?;
194194

195195
// Remove the real config if it is symlink and will not
196196
// if /boot/grub2/grub.cfg is file

src/bootupd.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ pub(crate) fn install(
116116
target_arch = "powerpc64",
117117
target_arch = "riscv64"
118118
))]
119-
crate::grubconfigs::install(sysroot, installed_efi_vendor.as_deref(), uuid)?;
119+
crate::grubconfigs::install(
120+
sysroot,
121+
Some(&source_root),
122+
installed_efi_vendor.as_deref(),
123+
uuid,
124+
)?;
120125
// On other architectures, assume that there's nothing to do.
121126
}
122127
None => {}

src/efi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl Component for Efi {
273273
.context("Failed to backup GRUB config")?;
274274
}
275275

276-
grubconfigs::install(&sysroot, Some(&vendor), true)?;
276+
grubconfigs::install(&sysroot, None, Some(&vendor), true)?;
277277
// Synchronize the filesystem containing /boot/efi/EFI/{vendor} to disk.
278278
fsfreeze_thaw_cycle(efidir.open_file(".")?)?;
279279

src/grubconfigs.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::Write;
2+
use std::io::Read;
23
use std::os::unix::io::AsRawFd;
34
use std::path::{Path, PathBuf};
45

@@ -25,6 +26,7 @@ const GRUBCONFIG_FILE_MODE: u32 = 0o600;
2526
#[context("Installing static GRUB configs")]
2627
pub(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

Comments
 (0)