Skip to content

Commit babb20d

Browse files
committed
blockdev: add bootable property to find ESP partition
As Fedora IoT on the Raspberry Pi 3 is using `MBR` by default, we need to find ESP partition via `bootable`. See coreos/bootupd#1019 Signed-off-by: Huijing Hei <[email protected]>
1 parent a64ae89 commit babb20d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

crates/blockdev/src/blockdev.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub struct Partition {
117117
pub parttype: String,
118118
pub uuid: Option<String>,
119119
pub name: Option<String>,
120+
pub bootable: Option<bool>,
120121
}
121122

122123
#[derive(Debug, Deserialize, PartialEq, Eq)]
@@ -169,6 +170,10 @@ impl PartitionTable {
169170
pub fn find_partition_of_type(&self, uuid: &str) -> Option<&Partition> {
170171
self.partitions.iter().find(|p| p.parttype_matches(uuid))
171172
}
173+
174+
pub fn find_partition_of_bootable(&self) -> Option<&Partition> {
175+
self.partitions.iter().find(|p| p.is_bootable())
176+
}
172177
}
173178

174179
impl Partition {
@@ -184,6 +189,11 @@ impl Partition {
184189
pub fn parttype_matches(&self, uuid: &str) -> bool {
185190
self.parttype.eq_ignore_ascii_case(uuid)
186191
}
192+
193+
/// Check this partition's bootable property.
194+
pub fn is_bootable(&self) -> bool {
195+
self.bootable.unwrap_or(false)
196+
}
187197
}
188198

189199
#[context("Listing partitions of {dev}")]
@@ -531,6 +541,7 @@ mod test {
531541
parttype: "c12a7328-f81f-11d2-ba4b-00a0c93ec93b".to_string(), // lowercase ESP UUID
532542
uuid: Some("58A4C5F0-BD12-424C-B563-195AC65A25DD".to_string()),
533543
name: Some("EFI System".to_string()),
544+
bootable: None,
534545
};
535546

536547
// Test exact match (lowercase)
@@ -602,4 +613,46 @@ mod test {
602613

603614
Ok(())
604615
}
616+
#[test]
617+
fn test_find_partition_of_bootable() -> Result<()> {
618+
let fixture = indoc::indoc! { r#"
619+
{
620+
"partitiontable": {
621+
"label": "dos",
622+
"id": "0xc1748067",
623+
"device": "/dev/mmcblk0",
624+
"unit": "sectors",
625+
"sectorsize": 512,
626+
"partitions": [
627+
{
628+
"node": "/dev/mmcblk0p1",
629+
"start": 2048,
630+
"size": 1026048,
631+
"type": "6",
632+
"bootable": true
633+
},{
634+
"node": "/dev/mmcblk0p2",
635+
"start": 1028096,
636+
"size": 2097152,
637+
"type": "83"
638+
},{
639+
"node": "/dev/mmcblk0p3",
640+
"start": 3125248,
641+
"size": 121610240,
642+
"type": "83"
643+
}
644+
]
645+
}
646+
}
647+
"# };
648+
let table: SfDiskOutput = serde_json::from_str(fixture).unwrap();
649+
650+
// Find ESP partition using bootalbe is true
651+
if table.partitiontable.label == PartitionType::Dos {
652+
let esp = table.partitiontable.find_partition_of_bootable();
653+
assert!(esp.is_some());
654+
assert_eq!(esp.unwrap().node, "/dev/mmcblk0p1");
655+
}
656+
Ok(())
657+
}
605658
}

0 commit comments

Comments
 (0)