Skip to content

Commit

Permalink
Merge pull request #555 from cgwalters/auto
Browse files Browse the repository at this point in the history
Add `backend install --auto`
  • Loading branch information
jmarrero committed Nov 1, 2023
2 parents 86fc2f8 + 5ee0483 commit 303ebb8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
38 changes: 32 additions & 6 deletions src/bootupd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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| {
Expand Down Expand Up @@ -104,24 +107,47 @@ pub(crate) fn install(

type Components = BTreeMap<&'static str, Box<dyn Component>>;

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<dyn Component>) {
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);
Expand Down
10 changes: 9 additions & 1 deletion src/cli/bootupd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,

/// 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)]
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit 303ebb8

Please sign in to comment.