Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bios: Handle empty pttype from lsblk output #740

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 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 @@ -113,12 +113,14 @@ impl Bios {

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

// 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 +215,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());
}
}
33 changes: 33 additions & 0 deletions tests/fixtures/example-lsblk-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"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
}
]
}
Loading