storage_stress_test_utils/
fvm.rsuse fidl_fuchsia_hardware_block_volume::VolumeManagerProxy;
use fidl_fuchsia_io as fio;
use ramdevice_client::{RamdiskClient, RamdiskClientBuilder};
use std::path::PathBuf;
use storage_isolated_driver_manager::{
create_random_guid, fvm, wait_for_block_device, BlockDeviceMatcher,
};
use zx::{AsHandleRef, Rights, Status, Vmo};
pub use storage_isolated_driver_manager::Guid;
async fn create_ramdisk(vmo: &Vmo, ramdisk_block_size: u64) -> RamdiskClient {
let duplicated_handle = vmo.as_handle_ref().duplicate(Rights::SAME_RIGHTS).unwrap();
let duplicated_vmo = Vmo::from(duplicated_handle);
RamdiskClientBuilder::new_with_vmo(duplicated_vmo, Some(ramdisk_block_size))
.build()
.await
.unwrap()
}
pub struct FvmInstance {
volume_manager: VolumeManagerProxy,
ramdisk: RamdiskClient,
}
impl FvmInstance {
pub async fn new(init: bool, vmo: &Vmo, fvm_slice_size: u64, ramdisk_block_size: u64) -> Self {
let ramdisk = create_ramdisk(&vmo, ramdisk_block_size).await;
if init {
fvm::format_for_fvm(
ramdisk.as_dir().expect("invalid directory proxy"),
fvm_slice_size as usize,
)
.unwrap();
}
let volume_manager = fvm::start_fvm_driver(
ramdisk.as_controller().expect("invalid controller"),
ramdisk.as_dir().expect("invalid directory proxy"),
)
.await
.expect("failed to start fvm driver");
Self { ramdisk, volume_manager }
}
pub async fn new_volume(
&mut self,
name: &str,
type_guid: &Guid,
initial_volume_size: Option<u64>,
) -> Guid {
let instance_guid = create_random_guid();
fvm::create_fvm_volume(
&self.volume_manager,
name,
type_guid,
&instance_guid,
initial_volume_size,
0,
)
.await
.unwrap();
instance_guid
}
pub async fn free_space(&self) -> u64 {
let (status, info) = self.volume_manager.get_info().await.unwrap();
Status::ok(status).unwrap();
let info = info.unwrap();
(info.slice_count - info.assigned_slice_count) * info.slice_size
}
pub fn ramdisk_get_dir(&self) -> Option<&fio::DirectoryProxy> {
self.ramdisk.as_dir()
}
pub async fn shutdown(self) {
self.ramdisk.destroy_and_wait_for_removal().await.expect("failed to shutdown ramdisk");
}
}
pub async fn get_volume_path(instance_guid: &Guid) -> PathBuf {
wait_for_block_device(&[BlockDeviceMatcher::InstanceGuid(instance_guid)]).await.unwrap()
}