Skip to content

Commit a29282c

Browse files
committed
blockdev: find esp based on MBR or GPT
See #1736 (comment) Signed-off-by: Huijing Hei <hhei@redhat.com>
1 parent 71dc8e5 commit a29282c

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

crates/blockdev/src/blockdev.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ use serde::Deserialize;
1212

1313
use bootc_utils::CommandRunExt;
1414

15+
/// EFI System Partition (ESP) on MBR
16+
pub const ESP_ID_MBR: &[&str] = &["6", "ef"];
17+
18+
/// EFI System Partition (ESP) for UEFI boot on GPT
19+
pub const ESP_ID_GPT: &str = "c12a7328-f81f-11d2-ba4b-00a0c93ec93b";
20+
1521
#[derive(Debug, Deserialize)]
1622
struct DevicesOutput {
1723
blockdevices: Vec<Device>,
@@ -175,6 +181,20 @@ impl PartitionTable {
175181
pub fn find_partition_of_bootable(&self) -> Option<&Partition> {
176182
self.partitions.iter().find(|p| p.is_bootable())
177183
}
184+
185+
/// Find the esp partition.
186+
pub fn find_partition_of_esp(&self) -> Result<Option<&Partition>> {
187+
match &self.label {
188+
PartitionType::Dos => Ok(ESP_ID_MBR
189+
.iter()
190+
.find_map(|&t| self.partitions.iter().find(|p| p.parttype_matches(t)))),
191+
PartitionType::Gpt => Ok(self
192+
.partitions
193+
.iter()
194+
.find(|p| p.parttype_matches(ESP_ID_GPT))),
195+
_ => Err(anyhow::anyhow!("Unsupported partition table type")),
196+
}
197+
}
178198
}
179199

180200
impl Partition {
@@ -612,6 +632,10 @@ mod test {
612632
.find_partition_of_type("00000000-0000-0000-0000-000000000000");
613633
assert!(nonexistent.is_none());
614634

635+
// Find esp partition on gpt
636+
let esp = table.partitiontable.find_partition_of_esp()?.unwrap();
637+
assert_eq!(esp.node, "/dev/loop0p1");
638+
615639
Ok(())
616640
}
617641
#[test]
@@ -636,11 +660,6 @@ mod test {
636660
"start": 1028096,
637661
"size": 2097152,
638662
"type": "83"
639-
},{
640-
"node": "/dev/mmcblk0p3",
641-
"start": 3125248,
642-
"size": 121610240,
643-
"type": "83"
644663
}
645664
]
646665
}
@@ -655,6 +674,9 @@ mod test {
655674
.find_partition_of_bootable()
656675
.expect("bootable partition not found");
657676
assert_eq!(esp.node, "/dev/mmcblk0p1");
677+
678+
let esp1 = table.partitiontable.find_partition_of_esp()?.unwrap();
679+
assert_eq!(esp1.node, "/dev/mmcblk0p1");
658680
Ok(())
659681
}
660682
}

0 commit comments

Comments
 (0)