Skip to main content

Crate blockdev

Crate blockdev 

Source
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§

BlockDevice
Represents a single block device as reported by lsblk.
BlockDevices
Represents the entire JSON object produced by lsblk --json.
Descendants
Iterator returned by BlockDevice::descendants.
MajMin
Represents the major and minor device numbers (maj:min in lsblk output).
ParseMajMinError
Error returned when MajMin::from_str is given a string that is not of the form "<major>:<minor>".

Enums§

BlockDevError
Error type for blockdev operations.
DeviceType
Represents the type of a block device, as reported by lsblk in the "type" field.

Functions§

get_devices
Runs lsblk --json --bytes, captures its output, and parses it into a BlockDevices struct.
parse_lsblk
Parses a JSON string (produced by lsblk --json) into a BlockDevices struct.