1// Copyright 2023 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.
4use ramdevice_client::{RamdiskClient, RamdiskClientBuilder};
5use zx::Vmo;
67const RAMDISK_BLOCK_SIZE: u64 = 512;
89/// Loads a test resource image into a VMO and bind it as a ramdisk to /dev.
10///
11/// When the returned RamdiskClient is Dropped it will schedule the ramdisk to unbind. Callers
12/// must keep a reference to the RamdiskClient while the ramdisk is needed.
13pub async fn mount_image_as_ramdisk(resource_path: &str) -> RamdiskClient {
14let image_buffer = fuchsia_fs::file::read_in_namespace(resource_path).await.unwrap();
15let image_size = image_buffer.len();
16let image_vmo = Vmo::create(image_size.try_into().unwrap()).unwrap();
17 image_vmo.write(&image_buffer, 0).unwrap();
1819let ramdisk_client = RamdiskClientBuilder::new_with_vmo(image_vmo, Some(RAMDISK_BLOCK_SIZE))
20 .build()
21 .await
22.unwrap();
2324 ramdisk_client
25}