1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use 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};
1314pub use storage_isolated_driver_manager::Guid;
1516async fn create_ramdisk(vmo: &Vmo, ramdisk_block_size: u64) -> RamdiskClient {
17let duplicated_handle = vmo.as_handle_ref().duplicate(Rights::SAME_RIGHTS).unwrap();
18let duplicated_vmo = Vmo::from(duplicated_handle);
1920// Create the ramdisks
21RamdiskClientBuilder::new_with_vmo(duplicated_vmo, Some(ramdisk_block_size))
22 .build()
23 .await
24.unwrap()
25}
2627/// This structs holds processes of component manager, isolated-devmgr
28/// and the fvm driver.
29///
30/// NOTE: The order of fields in this struct is important.
31/// Destruction happens top-down. Test must be destroyed last.
32pub struct FvmInstance {
33/// A proxy to fuchsia.hardware.block.VolumeManager protocol
34 /// Used to create new FVM volumes
35volume_manager: VolumeManagerProxy,
3637/// Manages the ramdisk device that is backed by a VMO
38ramdisk: RamdiskClient,
39}
4041impl FvmInstance {
42/// Start an isolated FVM driver against the given VMO.
43 /// If `init` is true, initialize the VMO with FVM layout first.
44pub async fn new(init: bool, vmo: &Vmo, fvm_slice_size: u64, ramdisk_block_size: u64) -> Self {
45let ramdisk = create_ramdisk(&vmo, ramdisk_block_size).await;
4647if init {
48 fvm::format_for_fvm(
49&ramdisk.open().expect("invalid ramdisk").into_proxy(),
50 fvm_slice_size as usize,
51 )
52 .unwrap();
53 }
5455let 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");
6162Self { ramdisk, volume_manager }
63 }
6465/// Create a new FVM volume with the given name and type GUID.
66 /// Returns the instance GUID used to uniquely identify this volume.
67pub async fn new_volume(
68&mut self,
69 name: &str,
70 type_guid: &Guid,
71 initial_volume_size: Option<u64>,
72 ) -> Guid {
73let instance_guid = create_random_guid();
7475 fvm::create_fvm_volume(
76&self.volume_manager,
77 name,
78 type_guid,
79&instance_guid,
80 initial_volume_size,
810,
82 )
83 .await
84.unwrap();
8586 instance_guid
87 }
8889/// Returns the number of bytes the FVM partition has available.
90pub async fn free_space(&self) -> u64 {
91let (status, info) = self.volume_manager.get_info().await.unwrap();
92 Status::ok(status).unwrap();
93let info = info.unwrap();
9495 (info.slice_count - info.assigned_slice_count) * info.slice_size
96 }
9798/// Returns a reference to the ramdisk DirectoryProxy.
99pub fn ramdisk_get_dir(&self) -> Option<&fio::DirectoryProxy> {
100self.ramdisk.as_dir()
101 }
102103/// Shuts down the FVM instance and ramdisk that's hosting it.
104pub async fn shutdown(self) {
105self.ramdisk.destroy_and_wait_for_removal().await.expect("failed to shutdown ramdisk");
106 }
107}
108109/// Gets the full path to a volume matching the given instance GUID at the given
110/// /dev/class/block path. This function will wait until a matching volume is found.
111pub async fn get_volume_path(instance_guid: &Guid) -> PathBuf {
112 wait_for_block_device_devfs(&[BlockDeviceMatcher::InstanceGuid(instance_guid)]).await.unwrap()
113}