Struct linapi::system::devices::block::Block

source ·
pub struct Block { /* private fields */ }
Expand description

A Block Device

Implementations§

source§

impl Block

source

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
source

pub fn from_dev(path: &Path) -> Result<Self>

Create from a device file in /dev

Errors
source

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.

source

pub fn dev_path(&self) -> Result<Option<PathBuf>>

Path to the device file, usually in /dev.

source

pub fn name(&self) -> &str

Kernel name for this device.

This does not have to match whats in /dev

source

pub fn partitions(&self) -> Result<Vec<Partition>>

Get this devices partitions, if any.

Errors
  • If I/O does
source

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
source

pub fn major(&self) -> u64

Device major number

source

pub fn minor(&self) -> u64

Device minor number

source

pub fn size(&self) -> Result<u64>

Get the byte size of the device, if possible.

source

pub fn capability(&self) -> Result<BlockCap>

Get device capabilities.

Unknown flags are preserved

See BlockCap for more details.

source

pub fn power(&self) -> Power<'_>

Get device power information

See Power for details

source

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.

source

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());
source

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.

source

pub fn model(&self) -> Result<Option<String>>

Get device model, if it exists.

source

pub fn logical_block_size(&self) -> Result<u64>

Device logical block size, the smallest unit the device can address.

This is usually 512

Trait Implementations§

source§

impl Clone for Block

source§

fn clone(&self) -> Block

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Block

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Block

§

impl Send for Block

§

impl Sync for Block

§

impl Unpin for Block

§

impl UnwindSafe for Block

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.