|
| 1 | +use std::fmt::Write; |
| 2 | +use std::os::unix::prelude::OsStrExt; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
| 5 | +use anyhow::{anyhow, Context, Result}; |
| 6 | +use fn_error_context::context; |
| 7 | +use openat_ext::OpenatDirExt; |
| 8 | + |
| 9 | +/// The subdirectory of /boot we use |
| 10 | +const GRUB2DIR: &str = "grub2"; |
| 11 | +const CONFIGDIR: &str = "/usr/lib/bootupd/grub2-static"; |
| 12 | +const DROPINDIR: &str = "configs.d"; |
| 13 | + |
| 14 | +#[context("Locating EFI vendordir")] |
| 15 | +pub(crate) fn find_efi_vendordir(efidir: &openat::Dir) -> Result<PathBuf> { |
| 16 | + for d in efidir.list_dir(".")? { |
| 17 | + let d = d?; |
| 18 | + if d.file_name().as_bytes() == b"BOOT" { |
| 19 | + continue; |
| 20 | + } |
| 21 | + let meta = efidir.metadata(d.file_name())?; |
| 22 | + if !meta.is_dir() { |
| 23 | + continue; |
| 24 | + } |
| 25 | + return Ok(d.file_name().into()); |
| 26 | + } |
| 27 | + anyhow::bail!("Failed to find EFI vendor dir") |
| 28 | +} |
| 29 | + |
| 30 | +/// Install the static GRUB config files. |
| 31 | +#[context("Installing static GRUB configs")] |
| 32 | +pub(crate) fn install(target_root: &openat::Dir, efi: bool) -> Result<()> { |
| 33 | + let bootdir = &target_root.sub_dir("boot").context("Opening /boot")?; |
| 34 | + |
| 35 | + let mut config = std::fs::read_to_string(Path::new(CONFIGDIR).join("grub-static-pre.cfg"))?; |
| 36 | + |
| 37 | + let dropindir = openat::Dir::open(&Path::new(CONFIGDIR).join(DROPINDIR))?; |
| 38 | + // Sort the files for reproducibility |
| 39 | + let mut entries = dropindir |
| 40 | + .list_dir(".")? |
| 41 | + .map(|e| e.map_err(anyhow::Error::msg)) |
| 42 | + .collect::<Result<Vec<_>>>()?; |
| 43 | + entries.sort_by(|a, b| a.file_name().cmp(b.file_name())); |
| 44 | + for ent in entries { |
| 45 | + let name = ent.file_name(); |
| 46 | + let name = name |
| 47 | + .to_str() |
| 48 | + .ok_or_else(|| anyhow!("Invalid UTF-8: {name:?}"))?; |
| 49 | + if !name.ends_with(".cfg") { |
| 50 | + log::debug!("Ignoring {name}"); |
| 51 | + continue; |
| 52 | + } |
| 53 | + // SAFETY: Cannot fail |
| 54 | + writeln!(config, "source $prefix/{name}").unwrap(); |
| 55 | + dropindir.copy_file_at(name, bootdir, format!("{GRUB2DIR}/{name}"))?; |
| 56 | + println!("Installed {name}"); |
| 57 | + } |
| 58 | + |
| 59 | + { |
| 60 | + let post = std::fs::read_to_string(Path::new(CONFIGDIR).join("grub-static-post.cfg"))?; |
| 61 | + config.push_str(post.as_str()); |
| 62 | + } |
| 63 | + |
| 64 | + bootdir |
| 65 | + .write_file_contents(format!("{GRUB2DIR}/grub.cfg"), 0o644, config.as_bytes()) |
| 66 | + .context("Copying grub-static.cfg")?; |
| 67 | + println!("Installed: grub.cfg"); |
| 68 | + |
| 69 | + let efidir = efi |
| 70 | + .then(|| { |
| 71 | + target_root |
| 72 | + .sub_dir_optional("boot/efi/EFI") |
| 73 | + .context("Opening /boot/efi/EFI") |
| 74 | + }) |
| 75 | + .transpose()? |
| 76 | + .flatten(); |
| 77 | + if let Some(efidir) = efidir.as_ref() { |
| 78 | + let vendordir = find_efi_vendordir(efidir)?; |
| 79 | + log::debug!("vendordir={:?}", &vendordir); |
| 80 | + let target = &vendordir.join("grub.cfg"); |
| 81 | + efidir |
| 82 | + .copy_file(&Path::new(CONFIGDIR).join("grub-static-efi.cfg"), target) |
| 83 | + .context("Copying static EFI")?; |
| 84 | + println!("Installed: {target:?}"); |
| 85 | + } |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use super::*; |
| 93 | + |
| 94 | + #[test] |
| 95 | + #[ignore] |
| 96 | + fn test_install() -> Result<()> { |
| 97 | + env_logger::init(); |
| 98 | + let td = tempfile::tempdir()?; |
| 99 | + let tdp = td.path(); |
| 100 | + let td = openat::Dir::open(tdp)?; |
| 101 | + std::fs::create_dir_all(tdp.join("boot/grub2"))?; |
| 102 | + std::fs::create_dir_all(tdp.join("boot/efi/EFI/BOOT"))?; |
| 103 | + std::fs::create_dir_all(tdp.join("boot/efi/EFI/fedora"))?; |
| 104 | + install(&td, true).unwrap(); |
| 105 | + |
| 106 | + assert!(td.exists("boot/grub2/grub.cfg")?); |
| 107 | + assert!(td.exists("boot/efi/EFI/fedora/grub.cfg")?); |
| 108 | + Ok(()) |
| 109 | + } |
| 110 | +} |
0 commit comments