use crate::aleph;
#[cfg(any(target_arch = "x86_64", target_arch = "powerpc64"))]
use crate::bios;
use crate::bootloader::{get_bootloader, Bootloader};
use crate::cli::bootupd::InstallOpts;
use crate::component::{self, ComponentType};
use crate::component::{Component, ValidationResult};
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64"
))]
use crate::efi;
use crate::freezethaw::fsfreeze_thaw_cycle;
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "powerpc64",
target_arch = "riscv64"
))]
use crate::grubconfigs::{ensure_grub_permissions, GRUB2DIR};
use crate::model::{ComponentStatus, ComponentUpdatable, ContentMetadata, SavedState, Status};
use crate::{ostreeutil, util};
use anyhow::{anyhow, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use cap_std_ext::dirext::CapStdExtDirExt;
use clap::crate_version;
use fn_error_context::context;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fs::{self, File};
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use bootc_internal_blockdev::Device;
use cap_std::ambient_authority;
use cap_std::fs::{Dir, Permissions, PermissionsExt};
use cap_std_ext::cap_std;
pub(crate) enum ConfigMode {
None,
Static,
WithUUID,
}
impl ConfigMode {
pub(crate) fn enabled_with_uuid(&self) -> Option<bool> {
match self {
ConfigMode::None => None,
ConfigMode::Static => Some(false),
ConfigMode::WithUUID => Some(true),
}
}
}
pub(crate) fn install(opts: &InstallOpts, devices: &[Device], configs: ConfigMode) -> Result<()> {
let all_components = get_components_impl(opts.auto);
if all_components.is_empty() {
println!("No components available for this platform.");
return Ok(());
}
let source_root_dir = Dir::open_ambient_dir(&opts.src_root, ambient_authority())
.context("Opening source root")?;
let target_components = if let Some(target_components) = &opts.components {
assert!(!opts.auto);
target_components
.iter()
.map(|name| {
all_components
.get(name.as_str())
.ok_or_else(|| anyhow!("Unknown component: {name}"))
})
.collect::<Result<Vec<_>>>()?
} else {
all_components.values().collect()
};
if target_components.is_empty() && !opts.auto {
anyhow::bail!("No components specified");
}
log::debug!(
"Auto: {}, Target components: {:?}",
opts.auto,
target_components
.iter()
.map(|c| c.name())
.collect::<Vec<_>>()
);
let has_efi_component = target_components
.iter()
.any(|c| c.component_type() == ComponentType::Efi);
for b in Bootloader::iter() {
if !has_efi_component && b != Bootloader::Grub {
log::debug!("Skipping bootloader {b} as there's no EFI component");
continue;
}
SavedState::ensure_not_present(&opts.dest_root, b)
.context("failed to install, invalid re-install attempted")?;
}
#[cfg(efi_arch)]
let bootloader = match opts.bootloader {
Some(b) => b,
None => {
let mut efi_default = None;
let mut efi_component_update = None;
let mut bios_default = None;
for c in &target_components {
use crate::component::ComponentType;
match c.component_type() {
ComponentType::Bios => {
bios_default = Some(Bootloader::Grub);
}
ComponentType::Efi => {
efi_default = c.get_default_bootloader(&source_root_dir)?;
if efi_default.is_none() {
efi_component_update =
c.get_component_update(&source_root_dir, None)?;
}
}
}
}
match (bios_default, efi_default) {
(_, Some(eb)) => eb,
(Some(bb), None) => bb,
(None, None) => {
let Some(efi_component_update) = efi_component_update else {
anyhow::bail!("Could not determine bootloader. Default bootloader not set")
};
let available_bootloaders = efi_component_update.available_bootloaders();
if available_bootloaders.len() != 1 {
anyhow::bail!(
"Could not determine bootloader. Default bootloader not set. Multiple bootloaders found as install candidates"
)
}
available_bootloaders[0]
}
}
}
};
#[cfg(not(efi_arch))]
let bootloader = Bootloader::Grub;
let mut state = SavedState::default();
let mut installed_efi_vendor = None;
for &component in target_components.iter() {
if component.component_type() == ComponentType::Bios && devices.is_empty() {
println!(
"Skip installing component {} without target device",
component.name()
);
continue;
}
if !component.is_bootloader_supported(bootloader) {
println!(
"Skip installing component {} as it does not support bootloader {bootloader}",
component.name()
);
continue;
}
if component
.query_update(&source_root_dir, bootloader)?
.is_none()
{
println!(
"Skip installing component {} without update metadata",
component.name()
);
continue;
}
let devices_to_install: Vec<Option<&Device>> = if devices.is_empty() {
vec![None]
} else if component.component_type() == ComponentType::Efi {
let esp_devices: Vec<&Device> = devices
.iter()
.filter(|dev| match dev.find_partition_of_esp() {
Ok(_) => true,
Err(e) => {
log::warn!("Skipping device {} for EFI: {:#}", dev.path(), e);
false
}
})
.collect();
if esp_devices.is_empty() {
log::info!(
"No ESP partitions detected on specified devices; \
falling back to mounted ESP detection"
);
vec![None]
} else {
esp_devices.into_iter().map(Some).collect()
}
} else {
devices.iter().map(|dev| Some(dev)).collect()
};
for device in &devices_to_install {
let device_desc = device.map_or("(auto)".to_string(), |d| d.path());
let meta = component
.install(
&opts.src_root,
&opts.dest_root,
*device,
opts.update_firmware,
bootloader,
)
.with_context(|| {
format!(
"installing component {} to device {}",
component.name(),
device_desc,
)
})?;
log::info!(
"Installed {} {} to {}",
component.name(),
meta.meta.version,
device_desc,
);
if !state.installed.contains_key(component.name()) {
state.installed.insert(component.name().into(), meta);
}
if installed_efi_vendor.is_none() {
if let Some(vendor) = component.get_efi_vendor(Path::new(&opts.src_root))? {
installed_efi_vendor = Some(vendor);
}
}
}
}
let sysroot = &Dir::open_ambient_dir(&opts.dest_root, ambient_authority())?;
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "powerpc64",
target_arch = "riscv64"
))]
if bootloader == Bootloader::Grub {
match configs.enabled_with_uuid() {
Some(uuid) => {
let meta = get_static_config_meta()?;
state.static_configs = Some(meta);
let mut esp_dir = None;
#[cfg(efi_arch)]
{
for c in &target_components {
use crate::efi::Efi;
if let Some(efi) = c.as_any().downcast_ref::<Efi>() {
let mounted_esp_path = efi.get_esp_mountpoint();
let mounted_esp_path = mounted_esp_path.as_ref().ok_or_else(|| {
anyhow!("Mounted ESP not found to install static grub config")
})?;
esp_dir = Some(
Dir::open_ambient_dir(mounted_esp_path, ambient_authority())
.context("Opening mounted ESP dir")?,
);
break;
}
}
}
crate::grubconfigs::install(
sysroot,
Some(&source_root_dir),
installed_efi_vendor.as_deref(),
uuid,
esp_dir.as_ref(),
)?;
}
None => {}
}
}
drop(target_components);
let mut state_guard = SavedState::unlocked(opts.dest_root.clone().into(), sysroot.try_clone()?)
.context("failed to acquire write lock")?;
state_guard
.update_state(&state, bootloader)
.context("failed to update state")?;
Ok(())
}
#[context("Get static config metadata")]
fn get_static_config_meta() -> Result<ContentMetadata> {
let self_bin_meta = std::fs::metadata("/proc/self/exe").context("Querying self meta")?;
let self_meta = ContentMetadata {
timestamp: self_bin_meta.modified()?.into(),
version: crate_version!().into(),
versions: None,
#[cfg(efi_arch)]
default_bootloader: None,
};
Ok(self_meta)
}
type Components = BTreeMap<&'static str, Box<dyn Component>>;
#[allow(clippy::box_default)]
pub(crate) fn get_components_impl(_auto: bool) -> Components {
#[cfg(not(target_arch = "s390x"))]
let mut components = BTreeMap::new();
#[cfg(target_arch = "s390x")]
let components = BTreeMap::new();
fn insert_component(components: &mut Components, component: Box<dyn Component>) {
components.insert(component.name(), component);
}
#[cfg(target_arch = "x86_64")]
{
if _auto {
let is_efi_booted = crate::efi::is_efi_booted().unwrap();
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(any(target_arch = "aarch64", target_arch = "riscv64"))]
insert_component(&mut components, Box::new(efi::Efi::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<()> {
let updates_dir = Path::new(sysroot_path).join(crate::model::BOOTUPD_UPDATES_DIR);
std::fs::create_dir_all(&updates_dir)
.with_context(|| format!("Failed to create updates dir {:?}", &updates_dir))?;
for component in get_components().values() {
if let Some(v) = component.generate_update_metadata(sysroot_path)? {
println!(
"Generated update layout for {}: {}",
component.name(),
v.version,
);
} else {
println!(
"Generating update layout for {} was not possible, skipping.",
component.name(),
);
}
}
Ok(())
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum ComponentUpdateResult {
AtLatestVersion,
Updated {
previous: ContentMetadata,
interrupted: Option<ContentMetadata>,
new: ContentMetadata,
},
}
fn ensure_writable_boot() -> Result<()> {
util::ensure_writable_mount("/boot")
}
pub(crate) fn update(name: &str, rootcxt: &RootContext) -> Result<ComponentUpdateResult> {
let bootloader = get_bootloader()?;
let mut state = SavedState::load_from_disk("/", Some(bootloader))?.unwrap_or_default();
let component = component::new_from_name(name)?;
let inst = if let Some(inst) = state.installed.get(name) {
inst.clone()
} else {
anyhow::bail!("Component {} is not installed", name);
};
let sysroot = &rootcxt.sysroot;
let (update, updatable) = component
.query_updatable(rootcxt.as_sysroot(), &inst, bootloader)
.with_context(|| format!("Failed to check {} for updates", name))?;
let update = match (update, &updatable) {
(Some(p), ComponentUpdatable::Upgradable) => p,
(Some(_), ComponentUpdatable::NoUpdateAvailable) => {
anyhow::bail!("update metadata exists but the component reports NoUpdateAvailable");
}
(None, _) => {
log::debug!("Skipping update, updatable: {updatable:?}");
component.query_requires_update(sysroot)?;
return Ok(ComponentUpdateResult::AtLatestVersion);
}
(Some(_), ComponentUpdatable::AtLatestVersion) => {
log::debug!("Skipping update, updatable: {updatable:?}");
return Ok(ComponentUpdateResult::AtLatestVersion);
}
};
ensure_writable_boot()?;
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "powerpc64",
target_arch = "riscv64"
))]
{
if bootloader == Bootloader::Grub {
let grub2dir = &sysroot
.open_dir(format!("boot/{GRUB2DIR}"))
.context("Opening /boot/grub2")?;
ensure_grub_permissions(grub2dir)?;
}
}
let mut pending_container = state.pending.take().unwrap_or_default();
let interrupted = pending_container.get(component.name()).cloned();
pending_container.insert(component.name().into(), update.clone());
let sysroot = sysroot.try_clone()?;
let mut state_guard = SavedState::acquire_write_lock(rootcxt.path.clone(), sysroot)
.context("Failed to acquire write lock")?;
state_guard
.update_state(&state, bootloader)
.context("Failed to update state")?;
let newinst = component
.run_update(rootcxt, &inst)
.with_context(|| format!("Failed to update {}", component.name()))?;
state.installed.insert(component.name().into(), newinst);
pending_container.remove(component.name());
state_guard.update_state(&state, bootloader)?;
Ok(ComponentUpdateResult::Updated {
previous: inst.meta,
interrupted,
new: update.clone(),
})
}
pub(crate) fn adopt_and_update(
name: &str,
rootcxt: &RootContext,
with_static_config: bool,
) -> Result<Option<ContentMetadata>> {
let bootloader = get_bootloader()?;
let sysroot = &rootcxt.sysroot;
let mut state = SavedState::load_from_disk("/", Some(bootloader))?.unwrap_or_default();
let component = component::new_from_name(name)?;
if state.installed.contains_key(name) {
anyhow::bail!("Component {} is already installed", name);
};
ensure_writable_boot()?;
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "powerpc64",
target_arch = "riscv64"
))]
{
if bootloader == Bootloader::Grub {
let grub2dir = &sysroot
.open_dir(format!("boot/{GRUB2DIR}"))
.context("Opening /boot/grub2")?;
ensure_grub_permissions(grub2dir)?;
}
}
let Some(update) = component.query_update(sysroot, bootloader)? else {
anyhow::bail!("Component {} has no available update", name);
};
let sysroot = sysroot.try_clone()?;
let mut state_guard = SavedState::acquire_write_lock(rootcxt.path.clone(), sysroot)
.context("Failed to acquire write lock")?;
let inst = component
.adopt_update(&rootcxt, &update, with_static_config, bootloader)
.context("Failed adopt and update")?;
if let Some(inst) = inst {
state.installed.insert(component.name().into(), inst);
if with_static_config && state.static_configs.is_none() {
let meta = get_static_config_meta()?;
state.static_configs = Some(meta);
ostreeutil::set_ostree_bootloader("none")?;
println!("Static GRUB configuration has been adopted successfully.");
}
state_guard.update_state(&state, bootloader)?;
return Ok(Some(update));
} else {
log::info!("Component '{}' skipped adoption", component.name());
return Ok(None);
}
}
#[context("Finding block device from boot or sysroot")]
pub(crate) fn list_dev_current_root() -> Result<Device> {
let auth = cap_std::ambient_authority();
for path in ["/boot", "/sysroot"] {
if let Ok(dir) = Dir::open_ambient_dir(path, auth) {
if let Ok(dev) = bootc_internal_blockdev::list_dev_by_dir(&dir) {
return Ok(dev);
}
}
}
anyhow::bail!("Failed to find block device from /boot or /sysroot")
}
pub(crate) fn validate(name: &str) -> Result<ValidationResult> {
let state = SavedState::load_from_disk("/", Some(get_bootloader()?))?.unwrap_or_default();
let component = component::new_from_name(name)?;
let Some(inst) = state.installed.get(name) else {
anyhow::bail!("Component {} is not installed", name);
};
let device = list_dev_current_root()?;
component.validate(inst, &device)
}
pub(crate) fn status() -> Result<Status> {
let mut ret: Status = Default::default();
let mut known_components = get_components();
let path_root = Utf8Path::new("/");
let root = Dir::open_ambient_dir(path_root, ambient_authority())?;
ret.aleph_version =
aleph::get_aleph_version(path_root.as_std_path())?.and_then(|a| a.aleph.version);
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64"
))]
{
ret.boot_method = Some(if efi::is_efi_booted()? { "EFI" } else { "BIOS" }.into());
}
let bootloader = get_bootloader()?;
let state = SavedState::load_from_disk("/", Some(bootloader))?;
if let Some(state) = state {
let sysroot = Sysroot {
dir: &root,
path: path_root,
};
for (name, ic) in state.installed.iter() {
log::trace!("Gathering status for installed component: {}", name);
let component = known_components
.remove(name.as_str())
.ok_or_else(|| anyhow!("Unknown component installed: {}", name))?;
let component = component.as_ref();
let interrupted = state.pending.as_ref().and_then(|p| p.get(name.as_str()));
let (update, updatable) = component
.query_updatable(sysroot, &ic, bootloader)
.with_context(|| format!("Querying update status for component {}", name))?;
let adopted_from = ic.adopted_from.clone();
ret.components.insert(
name.to_string(),
ComponentStatus {
installed: ic.meta.clone(),
interrupted: interrupted.cloned(),
update,
updatable,
adopted_from,
},
);
}
} else {
log::trace!("No saved state");
}
log::trace!("Remaining known components: {}", known_components.len());
for (name, _) in known_components {
if let Some(adopt_ver) = component::query_adopt_state()? {
let component = component::new_from_name(&name)?;
if component.query_update(&root, bootloader)?.is_none() {
continue;
};
ret.adoptable.insert(name.to_string(), adopt_ver);
} else {
log::trace!("Not adoptable: {}", name);
}
}
Ok(ret)
}
pub(crate) fn print_status_avail(status: &Status) -> Result<()> {
let mut avail = Vec::new();
for (name, component) in status.components.iter() {
if let ComponentUpdatable::Upgradable = component.updatable {
avail.push(name.as_str());
}
}
for (name, adoptable) in status.adoptable.iter() {
if adoptable.confident {
avail.push(name.as_str());
}
}
if !avail.is_empty() {
println!("Updates available: {}", avail.join(" "));
}
Ok(())
}
pub(crate) fn print_status(status: &Status) -> Result<()> {
if status.components.is_empty() {
println!("No components installed.");
}
for (name, component) in status.components.iter() {
println!("Component {}", name);
println!(" Installed: {}", component.installed.version);
if let Some(i) = component.interrupted.as_ref() {
println!(
" WARNING: Previous update to {} was interrupted",
i.version
);
}
let msg = match component.updatable {
ComponentUpdatable::NoUpdateAvailable => Cow::Borrowed("No update found"),
ComponentUpdatable::AtLatestVersion => Cow::Borrowed("At latest version"),
ComponentUpdatable::Upgradable => Cow::Owned(format!(
"Available: {}",
component.update.as_ref().expect("update").version
)),
};
println!(" Update: {}", msg);
}
if status.adoptable.is_empty() {
println!("No components are adoptable.");
}
for (name, adopt) in status.adoptable.iter() {
let ver = &adopt.version.version;
if adopt.confident {
println!("Detected: {}: {}", name, ver);
} else {
println!("Adoptable: {}: {}", name, ver);
}
}
if let Some(aleph_version) = &status.aleph_version {
println!("Aleph version: {}", aleph_version);
}
if let Some(boot_method) = &status.boot_method {
println!("Boot method: {}", boot_method);
}
Ok(())
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Sysroot<'a> {
pub(crate) dir: &'a Dir,
pub(crate) path: &'a Utf8Path,
}
pub struct RootContext {
pub sysroot: Dir,
pub path: Utf8PathBuf,
pub device: Device,
}
impl RootContext {
fn new(sysroot: Dir, path: &str, device: Device) -> Self {
Self {
sysroot,
path: Utf8Path::new(path).into(),
device,
}
}
pub(crate) fn as_sysroot(&self) -> Sysroot<'_> {
Sysroot {
dir: &self.sysroot,
path: &self.path,
}
}
}
fn prep_before_update() -> Result<RootContext> {
let path = "/";
let sysroot = Dir::open_ambient_dir(path, ambient_authority()).context("Opening root dir")?;
let device = list_dev_current_root()?;
Ok(RootContext::new(sysroot, path, device))
}
pub(crate) fn client_run_update() -> Result<()> {
crate::try_fail_point!("update");
let rootcxt = prep_before_update()?;
let status: Status = status()?;
if status.components.is_empty() && status.adoptable.is_empty() {
println!("No components installed.");
return Ok(());
}
let mut updated = false;
for (name, cstatus) in status.components.iter() {
match cstatus.updatable {
ComponentUpdatable::Upgradable => {}
_ => continue,
};
match update(name, &rootcxt)? {
ComponentUpdateResult::AtLatestVersion => {
eprintln!(
"warning: Expected update for {}, raced with a different client?",
name
);
continue;
}
ComponentUpdateResult::Updated {
previous,
interrupted,
new,
} => {
if let Some(i) = interrupted {
eprintln!(
"warning: Continued from previous interrupted update: {}",
i.version,
);
}
println!("Previous {}: {}", name, previous.version);
println!("Updated {}: {}", name, new.version);
}
}
updated = true;
}
for (name, adoptable) in status.adoptable.iter() {
if adoptable.confident {
if let Some(r) = adopt_and_update(name, &rootcxt, false)? {
println!("Adopted and updated: {}: {}", name, r.version);
updated = true;
}
} else {
println!("Component {} requires explicit adopt-and-update", name);
}
}
if !updated {
println!("No update available for any component.");
}
Ok(())
}
pub(crate) fn client_run_adopt_and_update(with_static_config: bool) -> Result<()> {
let rootcxt = prep_before_update()?;
let status: Status = status()?;
if status.adoptable.is_empty() {
println!("No components are adoptable.");
} else {
for (name, _) in status.adoptable.iter() {
if let Some(r) = adopt_and_update(name, &rootcxt, with_static_config)? {
println!("Adopted and updated: {}: {}", name, r.version);
}
}
}
Ok(())
}
pub(crate) fn client_run_validate() -> Result<()> {
let status: Status = status()?;
if status.components.is_empty() {
println!("No components installed.");
return Ok(());
}
let mut caught_validation_error = false;
for (name, _) in status.components.iter() {
match validate(name)? {
ValidationResult::Valid => {
println!("Validated: {}", name);
}
ValidationResult::Skip => {
println!("Skipped: {}", name);
}
ValidationResult::Errors(errs) => {
for err in errs {
eprintln!("{}", err);
}
caught_validation_error = true;
}
}
}
if caught_validation_error {
anyhow::bail!("Caught validation errors");
}
Ok(())
}
#[context("Migrating to a static GRUB config")]
pub(crate) fn client_run_migrate_static_grub_config() -> Result<()> {
if let Some(bootloader) = ostreeutil::get_ostree_bootloader()? {
if bootloader == "none" {
println!("Already using a static GRUB config");
return Ok(());
}
println!(
"ostree repo 'sysroot.bootloader' config option is currently set to: '{}'",
bootloader
);
} else {
println!("ostree repo 'sysroot.bootloader' config option is not set yet");
}
ensure_writable_boot()?;
let grub_config_dir = PathBuf::from("/boot/grub2");
let dirfd = Dir::open_ambient_dir(&grub_config_dir, ambient_authority())
.context("Opening /boot/grub2")?;
println!("Marking bootloader as BLS capable...");
_ = File::create("/boot/grub2/.grub2-blscfg-supported");
let grub_config_filename = PathBuf::from("/boot/grub2/grub.cfg");
match dirfd.read_link("grub.cfg") {
Err(_) => {
println!(
"'{}' is not a symlink, nothing to migrate",
grub_config_filename.display()
);
}
Ok(path) => {
println!("Migrating to a static GRUB config...");
let mut current_config = grub_config_dir.clone();
current_config.push(path);
let backup_config = PathBuf::from("/boot/grub2/grub.cfg.backup");
println!(
"Creating a backup of the current GRUB config '{}' in '{}'...",
current_config.display(),
backup_config.display()
);
fs::copy(¤t_config, &backup_config).context("Failed to backup GRUB config")?;
println!("Stripping ostree generated entries from GRUB config...");
let stripped_config = "grub.cfg.stripped";
let current_config_file =
File::open(current_config).context("Could not open current GRUB config")?;
let content = BufReader::new(current_config_file);
strip_grub_config_file(content, &dirfd, stripped_config)?;
dirfd
.rename(stripped_config, &dirfd, "grub.cfg")
.context("Failed to replace symlink with current GRUB config")?;
fsfreeze_thaw_cycle(dirfd.reopen_as_ownedfd()?)?;
println!("GRUB config symlink successfully replaced with the current config");
}
};
println!("Setting 'sysroot.bootloader' to 'none' in ostree repo config...");
ostreeutil::set_ostree_bootloader("none")?;
println!("Static GRUB config migration completed successfully");
Ok(())
}
fn strip_grub_config_file(
current_config_content: impl BufRead,
dirfd: &Dir,
stripped_config_name: &str,
) -> Result<()> {
let mut writer = vec![];
let mut skip = false;
for line in current_config_content.lines() {
let line = line.context("Failed to read line from GRUB config")?;
if line == "### END /etc/grub.d/15_ostree ###" {
skip = false;
continue;
}
if skip {
continue;
}
if line == "### BEGIN /etc/grub.d/15_ostree ###" {
skip = true;
continue;
}
writer
.write_all(line.as_bytes())
.context("Failed to write stripped GRUB config")?;
writer
.write_all(b"\n")
.context("Failed to write stripped GRUB config")?;
}
dirfd
.atomic_write_with_perms(stripped_config_name, writer, Permissions::from_mode(0o600))
.with_context(|| format!("Writing to {stripped_config_name}"))?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_failpoint_update() {
let guard = fail::FailScenario::setup();
fail::cfg("update", "return").unwrap();
let r = client_run_update();
assert_eq!(r.is_err(), true);
guard.teardown();
}
#[test]
fn test_strip_grub_config_file() -> Result<()> {
let root: &tempfile::TempDir = &tempfile::tempdir()?;
let root_path = root.path();
let rootd = Dir::open_ambient_dir(root_path, ambient_authority())?;
let stripped_config = "stripped";
let content = r"
### BEGIN /etc/grub.d/10_linux ###
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/15_ostree ###
menuentry 'Red Hat Enterprise Linux CoreOS 4 (ostree)' --class gnu-linux --class gnu --class os --unrestricted 'ostree-0-a92522f9-74dc-456a-ae0c-05ba22bca976' {
load_video
set gfxpayload=keep
insmod gzio
insmod part_gpt
insmod ext2
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root a92522f9-74dc-456a-ae0c-05ba22bca976
else
search --no-floppy --fs-uuid --set=root a92522f9-74dc-456a-ae0c-05ba22bca976
fi
linuxefi /ostree/rhcos-bf3b382/vmlinuz console=tty0 console=ttyS0,115200n8 rootflags=defaults,prjquota rw $ignition_firstboot root=UUID=cbac8cdc
initrdefi /ostree/rhcos-bf3b382/initramfs.img
}
### END /etc/grub.d/15_ostree ###
### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###";
strip_grub_config_file(
BufReader::new(std::io::Cursor::new(content)),
&rootd,
stripped_config,
)?;
let stripped_content = rootd.read_to_string(stripped_config)?;
let expected = r"
### BEGIN /etc/grub.d/10_linux ###
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###
";
assert_eq!(expected, stripped_content);
Ok(())
}
}