Expand description
§blockdev
A lightweight, type-safe library for parsing lsblk --json output on Linux.
blockdev turns the JSON produced by util-linux’s lsblk into strongly
typed Rust structs (BlockDevices, BlockDevice, DeviceType,
MajMin) and exposes ergonomic helpers for inspecting device hierarchies,
separating the root-filesystem disk from the rest, and locating devices by
name.
§Quick start
use blockdev::get_devices;
let devices = get_devices()?;
for device in devices.non_system() {
if device.is_disk() {
println!("available disk: {} ({} bytes)", device.name, device.size);
}
}§Parsing pre-captured JSON
parse_lsblk accepts any string produced by lsblk --json (with or without
--bytes). Both numeric byte values and human-readable size strings such as
"3.5T" are accepted for the size field, and both single-value
"mountpoint": null and array-form "mountpoints": [...] representations
are supported transparently.
Structs§
- Block
Device - Represents a single block device as reported by
lsblk. - Block
Devices - Represents the entire JSON object produced by
lsblk --json. - Descendants
- Iterator returned by
BlockDevice::descendants. - MajMin
- Represents the major and minor device numbers (
maj:mininlsblkoutput). - Parse
MajMin Error - Error returned when
MajMin::from_stris given a string that is not of the form"<major>:<minor>".
Enums§
- Block
DevError - Error type for
blockdevoperations. - Device
Type - Represents the type of a block device, as reported by
lsblkin the"type"field.
Functions§
- get_
devices - Runs
lsblk --json --bytes, captures its output, and parses it into aBlockDevicesstruct. - parse_
lsblk - Parses a JSON string (produced by
lsblk --json) into aBlockDevicesstruct.