Skip to content

Commit 2b7c3b3

Browse files
committed
blockdev: Backfill partition number from sysfs for older lsblk
The "partn" column was added in util-linux 2.39, which is newer than what CentOS 9 / RHEL 9 ship (2.37). Read the partition number from sysfs when lsblk doesn't provide it. Assisted-by: Claude Code (claude-opus-4-5-20251101) Signed-off-by: ckyrouac <ckyrouac@redhat.com>
1 parent e96362d commit 2b7c3b3

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

crates/blockdev/src/blockdev.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub struct Device {
3333
pub partlabel: Option<String>,
3434
pub parttype: Option<String>,
3535
pub partuuid: Option<String>,
36+
/// Partition number (1-indexed). None for whole disk devices.
37+
pub partn: Option<u32>,
3638
pub children: Option<Vec<Device>>,
3739
pub size: u64,
3840
#[serde(rename = "maj:min")]
@@ -82,10 +84,32 @@ impl Device {
8284
Ok(())
8385
}
8486

87+
// The "partn" column was added in util-linux 2.39, which is newer than
88+
// what CentOS 9 / RHEL 9 ship (2.37).
89+
fn backfill_partn(&mut self) -> Result<()> {
90+
let Some(majmin) = self.maj_min.as_deref() else {
91+
return Ok(());
92+
};
93+
let sysfs_partn_path = format!("/sys/dev/block/{majmin}/partition");
94+
if Utf8Path::new(&sysfs_partn_path).try_exists()? {
95+
let partn = std::fs::read_to_string(&sysfs_partn_path)
96+
.with_context(|| format!("Reading {sysfs_partn_path}"))?;
97+
tracing::debug!("backfilled partn to {partn}");
98+
self.partn = Some(
99+
partn
100+
.trim()
101+
.parse()
102+
.context("Parsing sysfs partition property")?,
103+
);
104+
}
105+
Ok(())
106+
}
107+
85108
/// Older versions of util-linux may be missing some properties. Backfill them if they're missing.
86109
pub fn backfill_missing(&mut self) -> Result<()> {
87110
// Add new properties to backfill here
88111
self.backfill_start()?;
112+
self.backfill_partn()?;
89113
// And recurse to child devices
90114
for child in self.children.iter_mut().flatten() {
91115
child.backfill_missing()?;

0 commit comments

Comments
 (0)