Skip to content

Commit

Permalink
Add support for installing static grub configs
Browse files Browse the repository at this point in the history
Currently these are duplicated in osbuild and coreos-assembler.
We will aim to deduplicate them here.

Ideally we'd add support for "day 2" updates of these; I started
on a patch for that but it's sadly messy.

This is an incremental improvement.
  • Loading branch information
cgwalters committed Oct 3, 2023
1 parent 85e6262 commit 62fe513
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/bootupd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) fn install(
source_root: &str,
dest_root: &str,
device: Option<&str>,
with_static_configs: bool,
target_components: Option<&[String]>,
) -> Result<()> {
// TODO: Change this to an Option<&str>; though this probably balloons into having
Expand Down Expand Up @@ -62,6 +63,7 @@ pub(crate) fn install(
}

let mut state = SavedState::default();
let mut installed_efi = false;
for component in target_components {
// skip for BIOS if device is empty
if component.name() == "BIOS" && device.is_empty() {
Expand All @@ -77,10 +79,18 @@ pub(crate) fn install(
.with_context(|| format!("installing component {}", component.name()))?;
log::info!("Installed {} {}", component.name(), meta.meta.version);
state.installed.insert(component.name().into(), meta);
if component.name() == "EFI" {
installed_efi = true;
}
}
let sysroot = &openat::Dir::open(dest_root)?;

if with_static_configs {
crate::grubconfigs::install(sysroot, installed_efi)?;
}

let sysroot = openat::Dir::open(dest_root)?;
let mut state_guard = SavedState::unlocked(sysroot).context("failed to acquire write lock")?;
let mut state_guard =
SavedState::unlocked(sysroot.try_clone()?).context("failed to acquire write lock")?;
state_guard
.update_state(&state)
.context("failed to update state")?;
Expand Down
5 changes: 5 additions & 0 deletions src/cli/bootupd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub struct InstallOpts {
#[clap(long)]
device: Option<String>,

/// Enable installation of the built-in static config files
#[clap(long)]
with_static_configs: bool,

#[clap(long = "component")]
/// Only install these components
components: Option<Vec<String>>,
Expand Down Expand Up @@ -90,6 +94,7 @@ impl DCommand {
&opts.src_root,
&opts.dest_root,
opts.device.as_deref(),
opts.with_static_configs,
opts.components.as_deref(),
)
.context("boot data installation failed")?;
Expand Down
51 changes: 51 additions & 0 deletions src/grubconfigs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::{
os::unix::prelude::OsStrExt,
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use openat_ext::OpenatDirExt;

const CONFIGDIR: &str = "/usr/lib/bootupd/grub2-static";

pub(crate) fn find_efi_vendordir(efidir: &openat::Dir) -> Result<PathBuf> {
for d in efidir.list_self()? {
let d = d?;
if d.file_name().as_bytes() == b"BOOT" {
continue;
}
let meta = efidir.metadata(d.file_name())?;
if !meta.is_dir() {
continue;
}
return Ok(d.file_name().into());
}
anyhow::bail!("Failed to find EFI vendor dir")
}

/// Install the static GRUB config files.
pub(crate) fn install(target_root: &openat::Dir, efi: bool) -> Result<()> {
let bootdir = &target_root.sub_dir("boot").context("Opening /boot")?;

bootdir
.copy_file(&Path::new(CONFIGDIR).join("grub-static.cfg"), "grub.cfg")
.context("Copying grub-static.cfg")?;
println!("Installed: grub.cfg");

let efidir = efi
.then(|| {
target_root
.sub_dir_optional("boot/efi")
.context("Opening /boot/efi")
})
.transpose()?
.flatten();
if let Some(efidir) = efidir.as_ref() {
let vendordir = find_efi_vendordir(efidir)?;
let target = &vendordir.join("grub.cfg");
efidir.copy_file(&Path::new(CONFIGDIR).join("grub-static-efi.cfg"), target)?;
println!("Installed: {target:?}");
}

Ok(())
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ mod coreos;
mod daemon;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
mod efi;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64"))]
mod grubconfigs;
mod filetree;
mod ipc;
mod model;
Expand Down

0 comments on commit 62fe513

Please sign in to comment.