Skip to content

Commit

Permalink
Merge pull request coreos#740 from travier/main-option-pttype
Browse files Browse the repository at this point in the history
bios: Handle empty pttype from lsblk output
  • Loading branch information
jmarrero authored Oct 4, 2024
2 parents e716cb5 + f8504ba commit b0e17ab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
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
}
]
}

0 comments on commit b0e17ab

Please sign in to comment.