storage_stress_test_utils/
fvm.rs1use fidl_fuchsia_hardware_block_volume::VolumeManagerProxy;
6use fidl_fuchsia_io as fio;
7use ramdevice_client::{RamdiskClient, RamdiskClientBuilder};
8use std::path::PathBuf;
9use storage_isolated_driver_manager::{
10 create_random_guid, fvm, wait_for_block_device_devfs, BlockDeviceMatcher,
11};
12use zx::{AsHandleRef, Rights, Status, Vmo};
13
14pub use storage_isolated_driver_manager::Guid;
15
16async fn create_ramdisk(vmo: &Vmo, ramdisk_block_size: u64) -> RamdiskClient {
17 let duplicated_handle = vmo.as_handle_ref().duplicate(Rights::SAME_RIGHTS).unwrap();
18 let duplicated_vmo = Vmo::from(duplicated_handle);
19
20 RamdiskClientBuilder::new_with_vmo(duplicated_vmo, Some(ramdisk_block_size))
22 .build()
23 .await
24 .unwrap()
25}
26
27pub struct FvmInstance {
33 volume_manager: VolumeManagerProxy,
36
37 ramdisk: RamdiskClient,
39}
40
41impl FvmInstance {
42 pub async fn new(init: bool, vmo: &Vmo, fvm_slice_size: u64, ramdisk_block_size: u64) -> Self {
45 let ramdisk = create_ramdisk(&vmo, ramdisk_block_size).await;
46
47 if init {
48 fvm::format_for_fvm(
49 &ramdisk.open().expect("invalid ramdisk").into_proxy(),
50 fvm_slice_size as usize,
51 )
52 .unwrap();
53 }
54
55 let volume_manager = fvm::start_fvm_driver(
56 ramdisk.as_controller().expect("invalid controller"),
57 ramdisk.as_dir().expect("invalid directory proxy"),
58 )
59 .await
60 .expect("failed to start fvm driver");
61
62 Self { ramdisk, volume_manager }
63 }
64
65 pub async fn new_volume(
68 &mut self,
69 name: &str,
70 type_guid: &Guid,
71 initial_volume_size: Option<u64>,
72 ) -> Guid {
73 let instance_guid = create_random_guid();
74
75 fvm::create_fvm_volume(
76 &self.volume_manager,
77 name,
78 type_guid,
79 &instance_guid,
80 initial_volume_size,
81 0,
82 )
83 .await
84 .unwrap();
85
86 instance_guid
87 }
88
89 pub async fn free_space(&self) -> u64 {
91 let (status, info) = self.volume_manager.get_info().await.unwrap();
92 Status::ok(status).unwrap();
93 let info = info.unwrap();
94
95 (info.slice_count - info.assigned_slice_count) * info.slice_size
96 }
97
98 pub fn ramdisk_get_dir(&self) -> Option<&fio::DirectoryProxy> {
100 self.ramdisk.as_dir()
101 }
102
103 pub async fn shutdown(self) {
105 self.ramdisk.destroy_and_wait_for_removal().await.expect("failed to shutdown ramdisk");
106 }
107}
108
109pub async fn get_volume_path(instance_guid: &Guid) -> PathBuf {
112 wait_for_block_device_devfs(&[BlockDeviceMatcher::InstanceGuid(instance_guid)]).await.unwrap()
113}