use crate::bootupd::{self, ConfigMode};
use anyhow::{Context, Result};
use camino::Utf8Path;
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),
}
#[derive(Debug, Parser)]
pub struct InstallOpts {
#[clap(long, value_parser, default_value_t = String::from("/"))]
src_root: String,
#[clap(value_parser)]
dest_root: String,
#[clap(long, action = clap::ArgAction::Append, conflicts_with = "filesystem")]
device: Vec<String>,
#[clap(long)]
filesystem: Option<String>,
#[clap(long)]
with_static_configs: bool,
#[clap(long)]
write_uuid: bool,
#[clap(long)]
update_firmware: bool,
#[clap(long = "component", conflicts_with = "auto")]
components: Option<Vec<String>>,
#[clap(long)]
auto: bool,
}
#[derive(Debug, Parser)]
pub struct GenerateOpts {
#[clap(value_parser)]
sysroot: Option<String>,
}
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),
}
}
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, cap_std::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.src_root,
&opts.dest_root,
&devices,
configmode,
opts.update_firmware,
opts.components.as_deref(),
opts.auto,
)
.context("boot data installation failed")?;
Ok(())
}
}