Skip to content

Commit b2d0ddd

Browse files
committed
bios.rs: update get_bios_boot_partition() to use blockdev
Update `get_bios_boot_partition()`: - Return None if has multiple devices (currently not supported)
1 parent d3e146c commit b2d0ddd

File tree

1 file changed

+14
-60
lines changed

1 file changed

+14
-60
lines changed

src/bios.rs

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,17 @@ use std::io::prelude::*;
22
use std::path::Path;
33
use std::process::Command;
44

5+
use crate::blockdev;
56
use crate::component::*;
67
use crate::model::*;
78
use crate::packagesystem;
8-
use anyhow::{bail, Result};
9-
109
use crate::util;
11-
use serde::{Deserialize, Serialize};
10+
11+
use anyhow::{bail, Result};
1212

1313
// grub2-install file path
1414
pub(crate) const GRUB_BIN: &str = "usr/sbin/grub2-install";
1515

16-
#[derive(Serialize, Deserialize, Debug)]
17-
struct BlockDevice {
18-
path: String,
19-
pttype: Option<String>,
20-
parttypename: Option<String>,
21-
}
22-
23-
#[derive(Serialize, Deserialize, Debug)]
24-
struct Devices {
25-
blockdevices: Vec<BlockDevice>,
26-
}
27-
2816
#[derive(Default)]
2917
pub(crate) struct Bios {}
3018

@@ -115,37 +103,18 @@ impl Bios {
115103
}
116104

117105
// check bios_boot partition on gpt type disk
118-
fn get_bios_boot_partition(&self) -> Result<Option<String>> {
119-
let target = self.get_device()?;
120-
// lsblk to list children with bios_boot
121-
let output = Command::new("lsblk")
122-
.args([
123-
"--json",
124-
"--output",
125-
"PATH,PTTYPE,PARTTYPENAME",
126-
target.trim(),
127-
])
128-
.output()?;
129-
if !output.status.success() {
130-
std::io::stderr().write_all(&output.stderr)?;
131-
bail!("Failed to run lsblk");
106+
fn get_bios_boot_partition(&self) -> Option<Vec<String>> {
107+
let bios_boot_devices =
108+
blockdev::find_colocated_bios_boot("/").expect("get bios_boot devices");
109+
// Return None if has multiple devices
110+
if bios_boot_devices.len() > 1 {
111+
log::warn!("Find multiple devices which are currently not supported");
112+
return None;
132113
}
133-
134-
let output = String::from_utf8(output.stdout)?;
135-
// Parse the JSON string into the `Devices` struct
136-
let Ok(devices) = serde_json::from_str::<Devices>(&output) else {
137-
bail!("Could not deserialize JSON output from lsblk");
138-
};
139-
140-
// Find the device with the parttypename "BIOS boot"
141-
for device in devices.blockdevices {
142-
if let Some(parttypename) = &device.parttypename {
143-
if parttypename == "BIOS boot" && device.pttype.as_deref() == Some("gpt") {
144-
return Ok(Some(device.path));
145-
}
146-
}
114+
if !bios_boot_devices.is_empty() {
115+
return Some(bios_boot_devices);
147116
}
148-
Ok(None)
117+
None
149118
}
150119
}
151120

@@ -187,7 +156,7 @@ impl Component for Bios {
187156

188157
fn query_adopt(&self) -> Result<Option<Adoptable>> {
189158
#[cfg(target_arch = "x86_64")]
190-
if crate::efi::is_efi_booted()? && self.get_bios_boot_partition()?.is_none() {
159+
if crate::efi::is_efi_booted()? && self.get_bios_boot_partition().is_none() {
191160
log::debug!("Skip BIOS adopt");
192161
return Ok(None);
193162
}
@@ -235,18 +204,3 @@ impl Component for Bios {
235204
Ok(None)
236205
}
237206
}
238-
239-
#[cfg(test)]
240-
mod tests {
241-
use super::*;
242-
243-
#[test]
244-
fn test_deserialize_lsblk_output() {
245-
let data = include_str!("../tests/fixtures/example-lsblk-output.json");
246-
let devices: Devices = serde_json::from_str(&data).expect("JSON was not well-formatted");
247-
assert_eq!(devices.blockdevices.len(), 7);
248-
assert_eq!(devices.blockdevices[0].path, "/dev/sr0");
249-
assert!(devices.blockdevices[0].pttype.is_none());
250-
assert!(devices.blockdevices[0].parttypename.is_none());
251-
}
252-
}

0 commit comments

Comments
 (0)