use crate::bootloader::Bootloader;
use crate::bootupd::{self, ConfigMode};
use crate::varlink;
use anyhow::{Context, Result};
use camino::Utf8Path;
use cap_std::ambient_authority;
use cap_std::fs::Dir;
use cap_std_ext::cap_std;
use clap::Parser;
use log::LevelFilter;
#[derive(Debug, Parser)]
#[clap(name = "bootupd", about = "Bootupd backend commands", version)]
pub struct DCommand {
#[clap(short = 'v', action = clap::ArgAction::Count, global = true)]
verbosity: u8,
#[clap(subcommand)]
pub cmd: DVerb,
}
impl DCommand {
pub(crate) fn loglevel(&self) -> LevelFilter {
match self.verbosity {
0 => LevelFilter::Warn,
1 => LevelFilter::Info,
2 => LevelFilter::Debug,
_ => LevelFilter::Trace,
}
}
}
#[derive(Debug, Parser)]
pub enum DVerb {
#[clap(name = "generate-update-metadata", about = "Generate metadata")]
GenerateUpdateMetadata(GenerateOpts),
#[clap(name = "install", about = "Install components")]
Install(InstallOpts),
#[cfg(efi_arch)]
SetDefaultBootloader(DefaultBootloaderOpts),
#[clap(name = "varlink", hide = true, about = "Run the varlink service")]
Varlink,
}
#[derive(Debug, Parser)]
pub(crate) struct InstallOpts {
#[clap(long, value_parser, default_value_t = String::from("/"))]
pub(crate) src_root: String,
#[clap(value_parser)]
pub(crate) dest_root: String,
#[clap(long, action = clap::ArgAction::Append, conflicts_with = "filesystem")]
pub(crate) device: Vec<String>,
#[clap(long)]
pub(crate) filesystem: Option<String>,
#[clap(long)]
pub(crate) with_static_configs: bool,
#[clap(long)]
pub(crate) write_uuid: bool,
#[clap(long)]
pub(crate) update_firmware: bool,
#[clap(long = "component", conflicts_with = "auto")]
pub(crate) components: Option<Vec<String>>,
#[clap(long)]
pub(crate) auto: bool,
#[clap(long)]
pub(crate) bootloader: Option<Bootloader>,
}
#[derive(Debug, Parser)]
pub struct GenerateOpts {
#[clap(value_parser)]
sysroot: Option<String>,
}
#[derive(Debug, Parser)]
pub struct DefaultBootloaderOpts {
#[clap(long)]
pub(crate) sysroot: Option<String>,
pub(crate) bootloader: Bootloader,
}
impl DCommand {
pub fn run(self) -> Result<()> {
match self.cmd {
DVerb::Install(opts) => Self::run_install(opts),
DVerb::GenerateUpdateMetadata(opts) => Self::run_generate_meta(opts),
DVerb::Varlink => Self::run_varlink_service(),
#[cfg(efi_arch)]
DVerb::SetDefaultBootloader(opts) => Self::set_default_bootloader(opts),
}
}
pub(crate) fn run_generate_meta(opts: GenerateOpts) -> Result<()> {
let sysroot = opts.sysroot.as_deref().unwrap_or("/");
if sysroot != "/" {
anyhow::bail!("Using a non-default sysroot is not supported: {}", sysroot);
}
bootupd::generate_update_metadata(sysroot).context("generating metadata failed")?;
Ok(())
}
pub(crate) fn run_install(opts: InstallOpts) -> Result<()> {
let configmode = if opts.write_uuid {
ConfigMode::WithUUID
} else if opts.with_static_configs {
ConfigMode::Static
} else {
ConfigMode::None
};
let devices = if let Some(ref fs_path) = opts.filesystem {
let dir = Dir::open_ambient_dir(fs_path, ambient_authority())
.with_context(|| format!("Opening filesystem path {fs_path}"))?;
let device = bootc_internal_blockdev::list_dev_by_dir(&dir)?;
device.find_all_roots()?
} else {
opts.device
.iter()
.map(|d| bootc_internal_blockdev::list_dev(Utf8Path::new(d)))
.collect::<Result<Vec<_>>>()?
};
bootupd::install(&opts, &devices, configmode).context("boot data installation failed")?;
Ok(())
}
#[cfg(efi_arch)]
pub(crate) fn set_default_bootloader(opts: DefaultBootloaderOpts) -> Result<()> {
let all_components = crate::bootupd::get_components();
let root_path = opts.sysroot.as_deref().unwrap_or("/");
let root = Dir::open_ambient_dir(root_path, ambient_authority())
.with_context(|| format!("Opening {root_path}"))?;
let mut target_components = Vec::new();
for component in all_components.values() {
if !component.is_bootloader_supported(opts.bootloader) {
log::info!(
"{} is not supported for {}. Skipping...",
opts.bootloader,
component.name()
);
continue;
}
if component.get_component_update(&root, None)?.is_none() {
log::info!("{} has no update metadata. Skipping...", component.name());
continue;
}
target_components.push(component);
}
if !target_components.iter().any(|c| c.name() == "EFI") {
anyhow::bail!(
"Cannot set the default bootloader: there is no EFI update metadata in {root_path}"
);
}
for component in target_components {
component.set_default_bootloader(&opts)?;
}
Ok(())
}
pub(crate) fn run_varlink_service() -> Result<()> {
varlink::run_varlink_service()
}
}