TestBlockDevice

Struct TestBlockDevice 

Source
pub struct TestBlockDevice;
Expand description

Main API for creating and managing test block devices.

TestBlockDevice provides static methods for creating, deleting, and inspecting ublk-based test block devices with procedurally generated data.

§Examples

use test_bd::{TestBlockDevice, TestBlockDeviceConfig};

let config = TestBlockDeviceConfig {
    dev_id: -1,
    size: 1024 * 1024 * 1024,  // 1 GiB
    seed: 42,
    fill_percent: 40,
    duplicate_percent: 30,
    random_percent: 30,
    segments: 100,
    unprivileged: false,
};

// This will block until the device is stopped
match TestBlockDevice::run(config) {
    Ok(dev_id) => println!("Device created: /dev/ublkb{}", dev_id),
    Err(e) => eprintln!("Error: {}", e),
}

Implementations§

Source§

impl TestBlockDevice

Source

pub fn run(config: TestBlockDeviceConfig) -> Result<i32, String>

Creates and runs a test block device with the given configuration.

This method blocks until the device is stopped (e.g., via delete()). The device will appear as /dev/ublkb{dev_id} and can be used like any other block device.

§Arguments
  • config - Configuration specifying device size, data patterns, etc.
§Returns
  • Ok(i32) - The assigned device ID on success
  • Err(String) - Error message if device creation fails
§Examples
use test_bd::{TestBlockDevice, TestBlockDeviceConfig};

let config = TestBlockDeviceConfig {
    dev_id: -1,
    size: 10 * 1024 * 1024,  // 10 MiB
    seed: 42,
    fill_percent: 50,
    duplicate_percent: 25,
    random_percent: 25,
    segments: 20,
    unprivileged: false,
};

let dev_id = TestBlockDevice::run(config).expect("Failed to create device");
println!("Created device: /dev/ublkb{}", dev_id);
Source

pub fn run_with_callback<F>( config: TestBlockDeviceConfig, on_ready: Option<F>, ) -> Result<i32, String>
where F: FnOnce(i32, Vec<SegmentInfo>) + 'static,

Creates and runs a test block device with a callback when ready.

Similar to run(), but invokes a callback function once the device is ready for I/O operations. This is useful for coordinating with other tasks that need to wait for device initialization.

§Arguments
  • config - Configuration specifying device size, data patterns, etc.
  • on_ready - Optional callback invoked with device ID and segment info
§Returns
  • Ok(i32) - The assigned device ID on success
  • Err(String) - Error message if device creation fails
§Examples
use test_bd::{TestBlockDevice, TestBlockDeviceConfig, SegmentInfo};

let config = TestBlockDeviceConfig {
    dev_id: -1,
    size: 10 * 1024 * 1024,
    seed: 42,
    fill_percent: 50,
    duplicate_percent: 25,
    random_percent: 25,
    segments: 20,
    unprivileged: false,
};

TestBlockDevice::run_with_callback(config, Some(|dev_id, segments: Vec<SegmentInfo>| {
    println!("Device {} ready with {} segments", dev_id, segments.len());
})).expect("Failed to create device");
Source

pub fn delete(dev_id: i32, _async_del: bool) -> Result<(), String>

Deletes (stops and removes) a test block device.

This stops the device and removes it from the system. The device node /dev/ublkb{dev_id} will be removed.

§Arguments
  • dev_id - The device ID to delete
  • _async_del - Reserved for future use (currently ignored)
§Returns
  • Ok(()) on success
  • Err(String) if deletion fails
§Examples
use test_bd::TestBlockDevice;

// Delete device 0
TestBlockDevice::delete(0, false).expect("Failed to delete device");
Source

pub fn dump(dev_id: i32) -> Result<(), String>

Dumps information about a test block device to the log.

This is primarily useful for debugging and displays internal device state.

§Arguments
  • dev_id - The device ID to inspect
§Returns
  • Ok(()) on success
  • Err(String) if the device cannot be opened
§Examples
use test_bd::TestBlockDevice;

TestBlockDevice::dump(0).expect("Failed to dump device info");

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V