Skip to content

Commit c6cb003

Browse files
committed
blockdev: Backfill start if missing
This way we continue to support the version of util-linux in C9S today. Signed-off-by: Colin Walters <[email protected]>
1 parent 909ece6 commit c6cb003

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

lib/src/blockdev.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,39 @@ impl Device {
5151
pub(crate) fn has_children(&self) -> bool {
5252
self.children.as_ref().map_or(false, |v| !v.is_empty())
5353
}
54+
55+
// The "start" parameter was only added in a version of util-linux that's only
56+
// in Fedora 40 as of this writing.
57+
fn backfill_start(&mut self) -> Result<()> {
58+
let Some(majmin) = self.maj_min.as_deref() else {
59+
// This shouldn't happen
60+
return Ok(());
61+
};
62+
let sysfs_start_path = format!("/sys/dev/block/{majmin}/start");
63+
if Utf8Path::new(&sysfs_start_path).try_exists()? {
64+
let start = std::fs::read_to_string(&sysfs_start_path)
65+
.with_context(|| format!("Reading {sysfs_start_path}"))?;
66+
tracing::debug!("backfilled start to {start}");
67+
self.start = Some(
68+
start
69+
.trim()
70+
.parse()
71+
.context("Parsing sysfs start property")?,
72+
);
73+
}
74+
Ok(())
75+
}
76+
77+
/// Older versions of util-linux may be missing some properties. Backfill them if they're missing.
78+
pub(crate) fn backfill_missing(&mut self) -> Result<()> {
79+
// Add new properties to backfill here
80+
self.backfill_start()?;
81+
// And recurse to child devices
82+
for child in self.children.iter_mut().flatten() {
83+
child.backfill_missing()?;
84+
}
85+
Ok(())
86+
}
5487
}
5588

5689
#[context("Failed to wipe {dev}")]
@@ -70,7 +103,10 @@ fn list_impl(dev: Option<&Utf8Path>) -> Result<Vec<Device>> {
70103
if !o.status.success() {
71104
return Err(anyhow::anyhow!("Failed to list block devices"));
72105
}
73-
let devs: DevicesOutput = serde_json::from_reader(&*o.stdout)?;
106+
let mut devs: DevicesOutput = serde_json::from_reader(&*o.stdout)?;
107+
for dev in devs.blockdevices.iter_mut() {
108+
dev.backfill_missing()?;
109+
}
74110
Ok(devs.blockdevices)
75111
}
76112

0 commit comments

Comments
 (0)