1
0
mirror of https://github.com/containers/bootc.git synced 2026-02-05 06:45:13 +01:00

blockdev: find esp based on MBR or GPT

See https://github.com/bootc-dev/bootc/pull/1736#issuecomment-3497579510

Signed-off-by: Huijing Hei <hhei@redhat.com>
This commit is contained in:
Huijing Hei
2025-11-10 22:50:10 +08:00
committed by Colin Walters
parent 7e81f9ced7
commit e24660f65e

View File

@@ -12,6 +12,13 @@ use serde::Deserialize;
use bootc_utils::CommandRunExt;
/// EFI System Partition (ESP) on MBR
/// Refer to <https://en.wikipedia.org/wiki/Partition_type>
pub const ESP_ID_MBR: &[u8] = &[0x06, 0xEF];
/// EFI System Partition (ESP) for UEFI boot on GPT
pub const ESP: &str = "c12a7328-f81f-11d2-ba4b-00a0c93ec93b";
#[derive(Debug, Deserialize)]
struct DevicesOutput {
blockdevices: Vec<Device>,
@@ -176,6 +183,19 @@ impl PartitionTable {
pub fn find_partition_of_bootable(&self) -> Option<&Partition> {
self.partitions.iter().find(|p| p.is_bootable())
}
/// Find the esp partition.
pub fn find_partition_of_esp(&self) -> Result<Option<&Partition>> {
match &self.label {
PartitionType::Dos => Ok(self.partitions.iter().find(|b| {
u8::from_str_radix(&b.parttype, 16)
.map(|pt| ESP_ID_MBR.contains(&pt))
.unwrap_or(false)
})),
PartitionType::Gpt => Ok(self.find_partition_of_type(ESP)),
_ => Err(anyhow::anyhow!("Unsupported partition table type")),
}
}
}
impl Partition {
@@ -613,10 +633,14 @@ mod test {
.find_partition_of_type("00000000-0000-0000-0000-000000000000");
assert!(nonexistent.is_none());
// Find esp partition on GPT
let esp = table.partitiontable.find_partition_of_esp()?.unwrap();
assert_eq!(esp.node, "/dev/loop0p1");
Ok(())
}
#[test]
fn test_find_partition_of_bootable() -> Result<()> {
fn test_find_partition_of_type_mbr() -> Result<()> {
let fixture = indoc::indoc! { r#"
{
"partitiontable": {
@@ -641,7 +665,7 @@ mod test {
"node": "/dev/mmcblk0p3",
"start": 3125248,
"size": 121610240,
"type": "83"
"type": "ef"
}
]
}
@@ -656,6 +680,10 @@ mod test {
.find_partition_of_bootable()
.expect("bootable partition not found");
assert_eq!(esp.node, "/dev/mmcblk0p1");
// Find esp partition on MBR
let esp1 = table.partitiontable.find_partition_of_esp()?.unwrap();
assert_eq!(esp1.node, "/dev/mmcblk0p1");
Ok(())
}
}