Skip to content

Commit f8504ba

Browse files
committed
bios: Handle empty pttype from lsblk output
zram, sr0 (CD/DVD) and LUKS devices generally don't use partitions, thus don't have a partition table and thus don't have a partition table type so this field may be null. Fixes: #739 Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 3e55890 commit f8504ba

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/bios.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) const GRUB_BIN: &str = "usr/sbin/grub2-install";
1616
#[derive(Serialize, Deserialize, Debug)]
1717
struct BlockDevice {
1818
path: String,
19-
pttype: String,
19+
pttype: Option<String>,
2020
parttypename: Option<String>,
2121
}
2222

@@ -113,12 +113,14 @@ impl Bios {
113113

114114
let output = String::from_utf8(output.stdout)?;
115115
// Parse the JSON string into the `Devices` struct
116-
let devices: Devices = serde_json::from_str(&output).expect("JSON was not well-formatted");
116+
let Ok(devices) = serde_json::from_str::<Devices>(&output) else {
117+
bail!("Could not deserialize JSON output from lsblk");
118+
};
117119

118120
// Find the device with the parttypename "BIOS boot"
119121
for device in devices.blockdevices {
120122
if let Some(parttypename) = &device.parttypename {
121-
if parttypename == "BIOS boot" && device.pttype == "gpt" {
123+
if parttypename == "BIOS boot" && device.pttype.as_deref() == Some("gpt") {
122124
return Ok(Some(device.path));
123125
}
124126
}
@@ -213,3 +215,18 @@ impl Component for Bios {
213215
Ok(None)
214216
}
215217
}
218+
219+
#[cfg(test)]
220+
mod tests {
221+
use super::*;
222+
223+
#[test]
224+
fn test_deserialize_lsblk_output() {
225+
let data = include_str!("../tests/fixtures/example-lsblk-output.json");
226+
let devices: Devices = serde_json::from_str(&data).expect("JSON was not well-formatted");
227+
assert_eq!(devices.blockdevices.len(), 7);
228+
assert_eq!(devices.blockdevices[0].path, "/dev/sr0");
229+
assert!(devices.blockdevices[0].pttype.is_none());
230+
assert!(devices.blockdevices[0].parttypename.is_none());
231+
}
232+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"blockdevices": [
3+
{
4+
"path": "/dev/sr0",
5+
"pttype": null,
6+
"parttypename": null
7+
},{
8+
"path": "/dev/zram0",
9+
"pttype": null,
10+
"parttypename": null
11+
},{
12+
"path": "/dev/vda",
13+
"pttype": "gpt",
14+
"parttypename": null
15+
},{
16+
"path": "/dev/vda1",
17+
"pttype": "gpt",
18+
"parttypename": "EFI System"
19+
},{
20+
"path": "/dev/vda2",
21+
"pttype": "gpt",
22+
"parttypename": "Linux extended boot"
23+
},{
24+
"path": "/dev/vda3",
25+
"pttype": "gpt",
26+
"parttypename": "Linux filesystem"
27+
},{
28+
"path": "/dev/mapper/luks-df2d5f95-5725-44dd-83e1-81bc4cdc49b8",
29+
"pttype": null,
30+
"parttypename": null
31+
}
32+
]
33+
}

0 commit comments

Comments
 (0)