| Crates.io | blkpath |
| lib.rs | blkpath |
| version | 0.1.0 |
| created_at | 2026-01-16 03:43:06.566993+00 |
| updated_at | 2026-01-16 03:43:06.566993+00 |
| description | Resolve the underlying block device path from a file path or file descriptor |
| homepage | |
| repository | https://github.com/SF-Zhou/blkpath |
| max_upload_size | |
| id | 2047799 |
| size | 41,679 |
Resolve the underlying block device path from a file path or file descriptor.
This crate provides a reliable way to determine which block device underlies a given file or directory. It uses a multi-step resolution strategy:
stat system call to get the device ID (major:minor numbers)/sys/dev/block/{major}:{minor}/proc/self/mountinfoAdd blkpath to your Cargo.toml:
[dependencies]
blkpath = "0.1"
Or install the CLI tool:
cargo install blkpath
Use the ResolveDevice trait to resolve device paths from Path or File:
use blkpath::ResolveDevice;
use std::path::Path;
fn main() -> Result<(), blkpath::DeviceResolveError> {
let path = Path::new("/home");
let device = path.resolve_device()?;
println!("Device: {}", device.display());
Ok(())
}
You can also use it with file descriptors:
use blkpath::ResolveDevice;
use std::fs::File;
fn main() -> Result<(), blkpath::DeviceResolveError> {
let file = File::open("/home")?;
let device = file.resolve_device()?;
println!("Device: {}", device.display());
Ok(())
}
Or use the convenience functions:
use blkpath::{resolve_device, resolve_device_from_file};
use std::path::Path;
use std::fs::File;
fn main() -> Result<(), blkpath::DeviceResolveError> {
// From path
let device = resolve_device(Path::new("/home"))?;
println!("Device: {}", device.display());
// From file
let file = File::open("/home")?;
let device = resolve_device_from_file(&file)?;
println!("Device: {}", device.display());
Ok(())
}
# Get the block device for a path
blkpath /home
# Output: /dev/sda1
# Get help
blkpath --help
The resolution process works as follows:
/sys/dev/block/{major}:{minor} for the device symlink/proc/self/mountinfo to find the mount sourceThis multi-step approach ensures reliability across different Linux configurations and container environments.
/sys/dev/block/ or /proc/self/mountinfo