Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bios.rs: add adopt-and-update #723

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/bios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,22 @@ impl Component for Bios {
}

fn query_adopt(&self) -> Result<Option<Adoptable>> {
Ok(None)
crate::component::query_adopt_state()
}

#[allow(unused_variables)]
fn adopt_update(
&self,
sysroot: &openat::Dir,
update: &ContentMetadata,
) -> Result<InstalledContent> {
todo!();
fn adopt_update(&self, _: &openat::Dir, update: &ContentMetadata) -> Result<InstalledContent> {
let Some(meta) = self.query_adopt()? else {
anyhow::bail!("Failed to find adoptable system")
};

let device = self.get_device()?;
let device = device.trim();
self.run_grub_install("/", device)?;
Ok(InstalledContent {
meta: update.clone(),
filetree: None,
adopted_from: Some(meta.version),
})
}

fn query_update(&self, sysroot: &openat::Dir) -> Result<Option<ContentMetadata>> {
Expand Down
32 changes: 32 additions & 0 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,38 @@ pub(crate) fn get_component_update(
}
}

#[context("Querying adoptable state")]
pub(crate) fn query_adopt_state() -> Result<Option<Adoptable>> {
// This would be extended with support for other operating systems later
if let Some(coreos_aleph) = crate::coreos::get_aleph_version(Path::new("/"))? {
let meta = ContentMetadata {
timestamp: coreos_aleph.ts,
version: coreos_aleph.aleph.version,
};
log::trace!("Adoptable: {:?}", &meta);
return Ok(Some(Adoptable {
version: meta,
confident: true,
}));
} else {
log::trace!("No CoreOS aleph detected");
}
let ostree_deploy_dir = Path::new("/ostree/deploy");
if ostree_deploy_dir.exists() {
let btime = ostree_deploy_dir.metadata()?.created()?;
let timestamp = chrono::DateTime::from(btime);
let meta = ContentMetadata {
timestamp,
version: "unknown".to_string(),
};
return Ok(Some(Adoptable {
version: meta,
confident: true,
}));
}
Ok(None)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
38 changes: 5 additions & 33 deletions src/efi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn string_from_utf16_bytes(slice: &[u8]) -> String {
fn read_efi_var_utf16_string(name: &str) -> Option<String> {
let efivars = Path::new("/sys/firmware/efi/efivars");
if !efivars.exists() {
log::warn!("No efivars mount at {:?}", efivars);
log::trace!("No efivars mount at {:?}", efivars);
return None;
}
let path = efivars.join(name);
Expand Down Expand Up @@ -239,39 +239,13 @@ impl Component for Efi {
log::trace!("No ESP detected");
return Ok(None);
};
// This would be extended with support for other operating systems later
if let Some(coreos_aleph) = crate::coreos::get_aleph_version(Path::new("/"))? {
let meta = ContentMetadata {
timestamp: coreos_aleph.ts,
version: coreos_aleph.aleph.version,
};
log::trace!("EFI adoptable: {:?}", &meta);
return Ok(Some(Adoptable {
version: meta,
confident: true,
}));
} else {
log::trace!("No CoreOS aleph detected");
}

// Don't adopt if the system is booted with systemd-boot or
// systemd-stub since those will be managed with bootctl.
if skip_systemd_bootloaders() {
return Ok(None);
}
let ostree_deploy_dir = Path::new("/ostree/deploy");
if ostree_deploy_dir.exists() {
let btime = ostree_deploy_dir.metadata()?.created()?;
let timestamp = chrono::DateTime::from(btime);
let meta = ContentMetadata {
timestamp,
version: "unknown".to_string(),
};
return Ok(Some(Adoptable {
version: meta,
confident: true,
}));
}
Ok(None)
crate::component::query_adopt_state()
}

/// Given an adoptable system and an update, perform the update.
Expand All @@ -280,10 +254,8 @@ impl Component for Efi {
sysroot: &openat::Dir,
updatemeta: &ContentMetadata,
) -> Result<InstalledContent> {
let meta = if let Some(meta) = self.query_adopt()? {
meta
} else {
anyhow::bail!("Failed to find adoptable system");
let Some(meta) = self.query_adopt()? else {
anyhow::bail!("Failed to find adoptable system")
};

let esp = self.open_esp()?;
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e-update/e2e-update-in-vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ if test -s out.txt; then
fi
ok update not avail

mount -o remount,rw /boot
rm -f /boot/bootupd-state.json
bootupctl adopt-and-update | tee out.txt
assert_file_has_content out.txt "Adopted and updated: BIOS: .*"
assert_file_has_content out.txt "Adopted and updated: EFI: .*"
ok adopt-and-update

tap_finish
touch /run/testtmp/success
sync
Expand Down
Loading