blkpath
Resolve the underlying block device path from a file path or file descriptor.
Overview
This crate provides a reliable way to determine which block device underlies a given file or directory. It uses a multi-step resolution strategy:
- First, it uses the
statsystem call to get the device ID (major:minor numbers) - Then, it looks up the device path via
/sys/dev/block/{major}:{minor} - If that fails, it falls back to parsing
/proc/self/mountinfo
Installation
Add blkpath to your Cargo.toml:
[]
= "0.1"
Or install the CLI tool:
Usage
Library Usage
Use the ResolveDevice trait to resolve device paths from Path or File:
use ResolveDevice;
use Path;
You can also use it with file descriptors:
use ResolveDevice;
use File;
Or use the convenience functions:
use ;
use Path;
use File;
CLI Usage
# Get the block device for a path
# Output: /dev/sda1
# Get help
How It Works
The resolution process works as follows:
- Stat the file/path: Get the device ID (major:minor numbers) from file metadata
- Sysfs lookup: Check
/sys/dev/block/{major}:{minor}for the device symlink - Mountinfo fallback: If sysfs fails, parse
/proc/self/mountinfoto find the mount source
This multi-step approach ensures reliability across different Linux configurations and container environments.
Requirements
- Linux operating system
- Access to
/sys/dev/block/or/proc/self/mountinfo