Skip to content

Commit

Permalink
Merge pull request coreos#723 from HuijingHei/adopt-bios
Browse files Browse the repository at this point in the history
bios.rs: add `adopt-and-update`
  • Loading branch information
HuijingHei committed Sep 18, 2024
2 parents 241b28c + 7cc055f commit 4c5a514
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
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

0 comments on commit 4c5a514

Please sign in to comment.