rsblkid

⚠️ WARNING: This library is still in development, thus not yet suitable for
use in production.
The rsblkid library is a safe Rust wrapper around util-linux/libblkid.
rsblkid can identify disks (block devices), the file systems they use to
store content, as well as extract additional information such as:
- File system labels,
- Volume names,
- Unique identifiers,
- Serial numbers,
- Device sizes,
- Minimum and optimal I/O sizes,
- etc.
Usage
This crate requires libblkid version 2.39.2 or later.
Add the following to your Cargo.toml:
[dependencies]
rsblkid = "0.4.1"
Then install the system packages below before running cargo build:
util-linux: to generate Rust bindings from libblkid's header files.
libclang: to satisfy the dependency of bindgen on libclang.
pkg-config: to detect system libraries.
Read the installation instructions below to
install the required dependencies on your system.
Example
Extract device metadata about /dev/vda.
use rsblkid::probe::{Probe, ScanResult};
fn main() -> rsblkid::Result<()> {
let mut probe = Probe::builder()
.scan_device("/dev/vda")
.scan_device_superblocks(true)
.scan_device_partitions(true)
.scan_partitions_for_partition_tables(Filter::In,
vec![
PartitionTableType::DOS,
PartitionTableType::GPT,
])
.scan_device_topology(true)
.build()?;
match probe.find_device_properties() {
ScanResult::FoundProperties => {
for property in probe.iter_device_properties() {
println!("{property}")
}
println!();
println!("Partition table");
println!("{} {:>10} {:>10} {:>10}\n----", "number", "start", "size", "part_type");
for partition in probe.iter_partitions() {
let number = partition.number();
let start = partition.location_in_sectors();
let size = partition.size_in_sectors();
let part_type = partition.partition_type();
println!("#{}: {:>10} {:>10} 0x{:x}", number, start, size, part_type)
}
println!();
let topology = probe.topology()?;
let alignment_offset = topology.alignment_offset_in_bytes();
let dax_support = if topology.supports_dax() { "yes" } else { "no" };
let minimum_io_size = topology.minimum_io_size();
let optimal_io_size = topology.optimal_io_size();
let logical_sector_size = topology.logical_sector_size();
let physical_sector_size = topology.physical_sector_size();
println!("Alignment offset (bytes): {}", alignment_offset);
println!("Direct Access support (DAX): {}", dax_support);
println!("Minimum I/O size (bytes): {}", minimum_io_size);
println!("Optimal I/O size (bytes): {}", optimal_io_size);
println!("Logical sector size (bytes): {}", logical_sector_size);
println!("Physical sector size (bytes): {}", physical_sector_size);
}
_ => eprintln!("could not find device properties"),
}
Ok(())
}
Install required dependencies
Alpine Linux
As root, issue the following command:
apk add util-linux-dev clang-libclang pkgconfig
NixOS
Install the packages in a temporary environment with:
nix-shell -p util-linux.dev libclang.lib pkg-config
or permanently with:
nix-env -iA nixos.util-linux.dev nixos.libclang.lib nixos.pkg-config
License
This project is licensed under either of:
Files in the third-party/ and web-snapshots/ directories are subject
to their own licenses and/or copyrights.
SPDX-License-Identifier: Apache-2.0 OR MIT
Copyright (c) 2023 Nick Piaddo