From 5ee0483a64d5dcadc8c94b49951f2df6146dbb50 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 31 Oct 2023 14:50:03 -0400 Subject: [PATCH] Add `backend install --auto` This drains the code from https://github.com/containers/bootc/pull/155/commits/20aa19ce6da58078999368e4f6dd948b2e3d990b Because it will also be what Anaconda wants to do by default. --- src/bootupd.rs | 38 ++++++++++++++++++++++++++++++++------ src/cli/bootupd.rs | 10 +++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/bootupd.rs b/src/bootupd.rs index b7a92ffa..bbb77191 100644 --- a/src/bootupd.rs +++ b/src/bootupd.rs @@ -32,6 +32,7 @@ pub(crate) fn install( device: Option<&str>, with_static_configs: bool, target_components: Option<&[String]>, + auto_components: bool, ) -> Result<()> { // TODO: Change this to an Option<&str>; though this probably balloons into having // DeviceComponent and FileBasedComponent @@ -40,12 +41,14 @@ pub(crate) fn install( SavedState::ensure_not_present(dest_root) .context("failed to install, invalid re-install attempted")?; - let all_components = get_components(); + let all_components = get_components_impl(auto_components); if all_components.is_empty() { println!("No components available for this platform."); return Ok(()); } let target_components = if let Some(target_components) = target_components { + // Checked by CLI parser + assert!(!auto_components); target_components .iter() .map(|name| { @@ -104,24 +107,47 @@ pub(crate) fn install( type Components = BTreeMap<&'static str, Box>; -pub(crate) fn get_components() -> Components { +#[allow(clippy::box_default)] +/// Return the set of known components; if `auto` is specified then the system +/// filters to the target booted state. +pub(crate) fn get_components_impl(auto: bool) -> Components { let mut components = BTreeMap::new(); fn insert_component(components: &mut Components, component: Box) { components.insert(component.name(), component); } - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - #[allow(clippy::box_default)] + #[cfg(target_arch = "x86_64")] + { + if auto { + let is_efi_booted = Path::new("/sys/firmware/efi").exists(); + log::info!( + "System boot method: {}", + if is_efi_booted { "EFI" } else { "BIOS" } + ); + if is_efi_booted { + insert_component(&mut components, Box::new(efi::Efi::default())); + } else { + insert_component(&mut components, Box::new(bios::Bios::default())); + } + } else { + insert_component(&mut components, Box::new(bios::Bios::default())); + insert_component(&mut components, Box::new(efi::Efi::default())); + } + } + #[cfg(target_arch = "aarch64")] insert_component(&mut components, Box::new(efi::Efi::default())); - #[cfg(any(target_arch = "x86_64", target_arch = "powerpc64"))] - #[allow(clippy::box_default)] + #[cfg(target_arch = "powerpc64")] insert_component(&mut components, Box::new(bios::Bios::default())); components } +pub(crate) fn get_components() -> Components { + get_components_impl(false) +} + pub(crate) fn generate_update_metadata(sysroot_path: &str) -> Result<()> { // create bootupd update dir which will save component metadata files for both components let updates_dir = Path::new(sysroot_path).join(crate::model::BOOTUPD_UPDATES_DIR); diff --git a/src/cli/bootupd.rs b/src/cli/bootupd.rs index f7b08678..b2cfd598 100644 --- a/src/cli/bootupd.rs +++ b/src/cli/bootupd.rs @@ -56,9 +56,16 @@ pub struct InstallOpts { #[clap(long)] with_static_configs: bool, - #[clap(long = "component")] + #[clap(long = "component", conflicts_with = "auto")] /// Only install these components components: Option>, + + /// Automatically choose components based on booted host state. + /// + /// For example on x86_64, if the host system is booted via EFI, + /// then only enable installation to the ESP. + #[clap(long)] + auto: bool, } #[derive(Debug, Parser)] @@ -96,6 +103,7 @@ impl DCommand { opts.device.as_deref(), opts.with_static_configs, opts.components.as_deref(), + opts.auto, ) .context("boot data installation failed")?; Ok(())