Skip to content

Commit

Permalink
bios: Handle empty pttype from lsblk output
Browse files Browse the repository at this point in the history
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: coreos#739
Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
travier committed Oct 4, 2024
1 parent 3e55890 commit cb0daa1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/bios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) const GRUB_BIN: &str = "usr/sbin/grub2-install";
#[derive(Serialize, Deserialize, Debug)]
struct BlockDevice {
path: String,
pttype: String,
pttype: Option<String>,
parttypename: Option<String>,
}

Expand Down Expand Up @@ -118,7 +118,7 @@ impl Bios {
// Find the device with the parttypename "BIOS boot"
for device in devices.blockdevices {
if let Some(parttypename) = &device.parttypename {
if parttypename == "BIOS boot" && device.pttype == "gpt" {
if parttypename == "BIOS boot" && device.pttype.as_deref() == Some("gpt") {
return Ok(Some(device.path));
}
}
Expand Down Expand Up @@ -213,3 +213,18 @@ impl Component for Bios {
Ok(None)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_deserialize_lsblk_output() {
let data = include_str!("../tests/fixtures/example-lsblk-output.json");
let devices: Devices = serde_json::from_str(&data).expect("JSON was not well-formatted");
assert_eq!(devices.blockdevices.len(), 7);
assert_eq!(devices.blockdevices[0].path, "/dev/sr0");
assert!(devices.blockdevices[0].pttype.is_none());
assert!(devices.blockdevices[0].parttypename.is_none());
}
}
34 changes: 34 additions & 0 deletions tests/fixtures/example-lsblk-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"blockdevices": [
{
"path": "/dev/sr0",
"pttype": null,
"parttypename": null
},{
"path": "/dev/zram0",
"pttype": null,
"parttypename": null
},{
"path": "/dev/vda",
"pttype": "gpt",
"parttypename": null
},{
"path": "/dev/vda1",
"pttype": "gpt",
"parttypename": "EFI System"
},{
"path": "/dev/vda2",
"pttype": "gpt",
"parttypename": "Linux extended boot"
},{
"path": "/dev/vda3",
"pttype": "gpt",
"parttypename": "Linux filesystem"
},{
"path": "/dev/mapper/luks-df2d5f95-5725-44dd-83e1-81bc4cdc49b8",
"pttype": null,
"parttypename": null
}
]
}

0 comments on commit cb0daa1

Please sign in to comment.