Skip to content

Commit 0eb1e31

Browse files
committed
efi: update install() and update() according to split change
1 parent 0221565 commit 0eb1e31

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

src/blockdev.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn get_devices<P: AsRef<Path>>(target_root: P) -> Result<Vec<String>> {
2323
}
2424

2525
// Get single device for the target root
26+
#[allow(dead_code)]
2627
pub fn get_single_device<P: AsRef<Path>>(target_root: P) -> Result<String> {
2728
let mut devices = get_devices(&target_root)?.into_iter();
2829
let Some(parent) = devices.next() else {
@@ -37,7 +38,6 @@ pub fn get_single_device<P: AsRef<Path>>(target_root: P) -> Result<String> {
3738

3839
/// Find esp partition on the same device
3940
/// using sfdisk to get partitiontable
40-
#[allow(dead_code)]
4141
pub fn get_esp_partition(device: &str) -> Result<Option<String>> {
4242
const ESP_TYPE_GUID: &str = "C12A7328-F81F-11D2-BA4B-00A0C93EC93B";
4343
let device_info: PartitionTable = bootc_blockdev::partitions_of(Utf8Path::new(device))?;
@@ -51,21 +51,20 @@ pub fn get_esp_partition(device: &str) -> Result<Option<String>> {
5151
Ok(None)
5252
}
5353

54-
/// Find all ESP partitions on the devices with mountpoint boot
55-
#[allow(dead_code)]
56-
pub fn find_colocated_esps<P: AsRef<Path>>(target_root: P) -> Result<Vec<String>> {
57-
// first, get the parent device
58-
let devices = get_devices(&target_root).with_context(|| "while looking for colocated ESPs")?;
59-
60-
// now, look for all ESPs on those devices
54+
/// Find all ESP partitions on the devices
55+
pub fn find_colocated_esps(devices: &Vec<String>) -> Result<Option<Vec<String>>> {
56+
// look for all ESPs on those devices
6157
let mut esps = Vec::new();
6258
for device in devices {
6359
if let Some(esp) = get_esp_partition(&device)? {
6460
esps.push(esp)
6561
}
6662
}
63+
if esps.is_empty() {
64+
return Ok(None);
65+
}
6766
log::debug!("Find esp partitions: {esps:?}");
68-
Ok(esps)
67+
Ok(Some(esps))
6968
}
7069

7170
/// Find bios_boot partition on the same device
@@ -82,20 +81,18 @@ pub fn get_bios_boot_partition(device: &str) -> Result<Option<String>> {
8281
Ok(None)
8382
}
8483

85-
/// Find all bios_boot partitions on the devices with mountpoint boot
86-
#[allow(dead_code)]
87-
pub fn find_colocated_bios_boot<P: AsRef<Path>>(target_root: P) -> Result<Vec<String>> {
88-
// first, get the parent device
89-
let devices =
90-
get_devices(&target_root).with_context(|| "looking for colocated bios_boot parts")?;
91-
92-
// now, look for all bios_boot parts on those devices
84+
/// Find all bios_boot partitions on the devices
85+
pub fn find_colocated_bios_boot(devices: &Vec<String>) -> Result<Option<Vec<String>>> {
86+
// look for all bios_boot parts on those devices
9387
let mut bios_boots = Vec::new();
9488
for device in devices {
9589
if let Some(bios) = get_bios_boot_partition(&device)? {
9690
bios_boots.push(bios)
9791
}
9892
}
93+
if bios_boots.is_empty() {
94+
return Ok(None);
95+
}
9996
log::debug!("Find bios_boot partitions: {bios_boots:?}");
100-
Ok(bios_boots)
97+
Ok(Some(bios_boots))
10198
}

src/efi.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use walkdir::WalkDir;
2020
use widestring::U16CString;
2121

2222
use crate::bootupd::RootContext;
23-
use crate::filetree;
23+
use crate::{blockdev, filetree};
2424
use crate::model::*;
2525
use crate::ostreeutil;
2626
use crate::util::{self, CommandRunExt};
@@ -306,7 +306,6 @@ impl Component for Efi {
306306
})
307307
}
308308

309-
// TODO: Remove dest_root; it was never actually used
310309
fn install(
311310
&self,
312311
src_root: &openat::Dir,
@@ -320,18 +319,22 @@ impl Component for Efi {
320319
log::debug!("Found metadata {}", meta.version);
321320
let srcdir_name = component_updatedirname(self);
322321
let ft = crate::filetree::FileTree::new_from_dir(&src_root.sub_dir(&srcdir_name)?)?;
323-
let destdir = &self.ensure_mounted_esp(Path::new(dest_root))?;
324-
325-
let destd = &openat::Dir::open(destdir)
326-
.with_context(|| format!("opening dest dir {}", destdir.display()))?;
322+
// get esp device via /dev/disk/by-partlabel
323+
let esp_device = self
324+
.get_esp_device()
325+
.ok_or_else(|| anyhow::anyhow!("Failed to find ESP device"))?;
326+
let destpath = &self.ensure_mounted_esp(Path::new(dest_root), &esp_device)?;
327+
328+
let destd = &openat::Dir::open(destpath)
329+
.with_context(|| format!("opening dest dir {}", destpath.display()))?;
327330
validate_esp(destd)?;
328331

329332
// TODO - add some sort of API that allows directly setting the working
330333
// directory to a file descriptor.
331334
std::process::Command::new("cp")
332335
.args(["-rp", "--reflink=auto"])
333336
.arg(&srcdir_name)
334-
.arg(destdir)
337+
.arg(destpath)
335338
.current_dir(format!("/proc/self/fd/{}", src_root.as_raw_fd()))
336339
.run()?;
337340
if update_firmware {
@@ -355,15 +358,31 @@ impl Component for Efi {
355358
.filetree
356359
.as_ref()
357360
.ok_or_else(|| anyhow::anyhow!("No filetree for installed EFI found!"))?;
358-
let sysroot = &sysroot.sysroot;
359-
let updatemeta = self.query_update(sysroot)?.expect("update available");
360-
let updated = sysroot
361+
let sysroot_dir = &sysroot.sysroot;
362+
let updatemeta = self.query_update(sysroot_dir)?.expect("update available");
363+
let updated = sysroot_dir
361364
.sub_dir(&component_updatedirname(self))
362365
.context("opening update dir")?;
363366
let updatef = filetree::FileTree::new_from_dir(&updated).context("reading update dir")?;
364-
let diff = currentf.diff(&updatef)?;
365-
self.ensure_mounted_esp(Path::new("/"))?;
366-
let destdir = self.open_esp().context("opening EFI dir")?;
367+
let diff = currentf.diff(&updatef)?;
368+
369+
let Some(esp_devices) = blockdev::find_colocated_esps(&sysroot.devices)? else {
370+
anyhow::bail!("Failed to find all esp devices");
371+
};
372+
let mut devices = esp_devices.iter();
373+
let Some(esp) = devices.next() else {
374+
anyhow::bail!("Failed to find esp device");
375+
};
376+
377+
if let Some(next_esp) = devices.next() {
378+
anyhow::bail!(
379+
"Found multiple esp devices {esp} and {next_esp}; not currently supported"
380+
);
381+
}
382+
let destpath = &self.ensure_mounted_esp(sysroot.path.as_ref(), Path::new(&esp))?;
383+
384+
let destdir = &openat::Dir::open(&destpath.join("EFI"))
385+
.with_context(|| format!("opening EFI dir {}", destpath.display()))?;
367386
validate_esp(&destdir)?;
368387
log::trace!("applying diff: {}", &diff);
369388
filetree::apply_diff(&updated, &destdir, &diff, None)

0 commit comments

Comments
 (0)