Skip to content

Commit 4b83fa3

Browse files
HuijingHeicgwalters
authored andcommitted
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 e5249d1 commit 4b83fa3

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

crates/blockdev/src/blockdev.rs

Lines changed: 55 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,11 @@ 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+
/// Find the partition with bootable is 'true'.
175+
pub fn find_partition_of_bootable(&self) -> Option<&Partition> {
176+
self.partitions.iter().find(|p| p.is_bootable())
177+
}
172178
}
173179

174180
impl Partition {
@@ -184,6 +190,11 @@ impl Partition {
184190
pub fn parttype_matches(&self, uuid: &str) -> bool {
185191
self.parttype.eq_ignore_ascii_case(uuid)
186192
}
193+
194+
/// Check this partition's bootable property.
195+
pub fn is_bootable(&self) -> bool {
196+
self.bootable.unwrap_or(false)
197+
}
187198
}
188199

189200
#[context("Listing partitions of {dev}")]
@@ -531,6 +542,7 @@ mod test {
531542
parttype: "c12a7328-f81f-11d2-ba4b-00a0c93ec93b".to_string(), // lowercase ESP UUID
532543
uuid: Some("58A4C5F0-BD12-424C-B563-195AC65A25DD".to_string()),
533544
name: Some("EFI System".to_string()),
545+
bootable: None,
534546
};
535547

536548
// Test exact match (lowercase)
@@ -602,4 +614,47 @@ mod test {
602614

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

0 commit comments

Comments
 (0)