pub struct Block { /* private fields */ }
Expand description
A Block Device
Implementations§
source§impl Block
impl Block
sourcepub fn get_connected() -> Result<Vec<Self>>
pub fn get_connected() -> Result<Vec<Self>>
Get connected Block Devices.
Note
Partitions are not included. Use Block::partitions
.
The returned Vec is sorted by kernel name.
Errors
Error::Io
for I/O errors
sourcepub fn from_dev(path: &Path) -> Result<Self>
pub fn from_dev(path: &Path) -> Result<Self>
Create from a device file in /dev
Errors
Error::InvalidArg
ifpath
is not a block deviceError::InvalidArg
ifpath
is a partitionError::Io
for I/O errors
sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
Canonical path to the block device.
You normally shouldn’t need this, but it could be useful if you want to manually access information not exposed by this crate.
sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Kernel name for this device.
This does not have to match whats in /dev
sourcepub fn partitions(&self) -> Result<Vec<Partition>>
pub fn partitions(&self) -> Result<Vec<Partition>>
sourcepub fn open(&self) -> Result<Option<File>>
pub fn open(&self) -> Result<Option<File>>
Open the device special file in /dev
associated with this block
device, if it exists.
The device file is opened for reading and writing
Errors
- If I/O does
sourcepub fn capability(&self) -> Result<BlockCap>
pub fn capability(&self) -> Result<BlockCap>
sourcepub fn add_partition(&mut self, num: u64, start_end: Range<i64>) -> Result<()>
pub fn add_partition(&mut self, num: u64, start_end: Range<i64>) -> Result<()>
Tell linux that partition num
exists in the range start_end
.
start_end
is a byte range within the whole device.
This range is NOT inclusive of end
.
This does NOT modify partitions or anything on disk, only the kernels view of the device.
This can be useful in cases where the kernel doesn’t support your partition table, you can read it yourself and tell it.
Examples
Add a partition
let mut block = Block::get_connected().unwrap().remove(0);
// Tell Linux there is one partition, starting at (1024 * 512) bytes
// and covering the whole device.
block.add_partition(0, 1024*512..block.size().unwrap() as i64);
Errors
- If the ioctl does.
Implementation
This uses the ioctls from include/linux/blkpg.h
.
sourcepub fn remove_partition(&mut self, num: u64) -> Result<()>
pub fn remove_partition(&mut self, num: u64) -> Result<()>
Tell Linux to forget about partition num
.
Examples
Remove a partition
let mut block = Block::get_connected().unwrap().remove(0);
let part = block.partitions().unwrap().remove(0);
block.remove_partition(part.number().unwrap());
sourcepub fn remove_existing_partitions(&mut self) -> Result<()>
pub fn remove_existing_partitions(&mut self) -> Result<()>
Convenience function for looping through Block::partitions
yourself.
Implementation
For now this is slightly more efficient than doing it manually, opening the device only once instead of for each partition.
sourcepub fn logical_block_size(&self) -> Result<u64>
pub fn logical_block_size(&self) -> Result<u64>
Device logical block size, the smallest unit the device can address.
This is usually 512