virtiofs/
resolver.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Defines the resource resolver for virtiofs devices.
5
6use crate::VirtioFs;
7use crate::virtio::VirtioFsDevice;
8use lxutil::LxVolumeOptions;
9use virtio::resolve::ResolvedVirtioDevice;
10use virtio::resolve::VirtioResolveInput;
11use virtio_resources::fs::VirtioFsBackend;
12use virtio_resources::fs::VirtioFsHandle;
13use vm_resource::ResolveResource;
14use vm_resource::declare_static_resolver;
15use vm_resource::kind::VirtioDeviceHandle;
16
17/// Resolver for virtiofs devices.
18pub struct VirtioFsResolver;
19
20declare_static_resolver! {
21    VirtioFsResolver,
22    (VirtioDeviceHandle, VirtioFsHandle),
23}
24
25impl ResolveResource<VirtioDeviceHandle, VirtioFsHandle> for VirtioFsResolver {
26    type Output = ResolvedVirtioDevice;
27    type Error = anyhow::Error;
28
29    fn resolve(
30        &self,
31        resource: VirtioFsHandle,
32        input: VirtioResolveInput<'_>,
33    ) -> Result<Self::Output, Self::Error> {
34        let device = match &resource.fs {
35            VirtioFsBackend::HostFs {
36                root_path,
37                mount_options,
38            } => VirtioFsDevice::new(
39                input.driver_source,
40                &resource.tag,
41                VirtioFs::new(
42                    root_path,
43                    Some(&LxVolumeOptions::from_option_string(mount_options)),
44                )?,
45                input.guest_memory.clone(),
46                0,
47                None,
48            ),
49            #[cfg(windows)]
50            VirtioFsBackend::SectionFs { root_path } => {
51                VirtioFsDevice::new(
52                    input.driver_source,
53                    &resource.tag,
54                    crate::SectionFs::new(root_path)?,
55                    input.guest_memory.clone(),
56                    8 * 1024 * 1024 * 1024, // 8GB of shared memory,
57                    None,
58                )
59            }
60            #[cfg(not(windows))]
61            VirtioFsBackend::SectionFs { .. } => {
62                anyhow::bail!("section fs not supported on this platform")
63            }
64        };
65        Ok(device.into())
66    }
67}