2019-06-27 17:24:36 +00:00
|
|
|
# How to use virtio-fs
|
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
In the context of virtualization, it is always convenient to be able to share a
|
|
|
|
directory from the host with the guest.
|
2019-06-27 17:24:36 +00:00
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
__virtio-fs__, also known as __vhost-user-fs__ is a virtual device defined by
|
|
|
|
the VIRTIO specification which allows any VMM to perform filesystem sharing.
|
2019-06-27 17:24:36 +00:00
|
|
|
|
|
|
|
## Pre-requisites
|
|
|
|
|
|
|
|
### The daemon
|
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
This virtual device relies on the _vhost-user_ protocol, which assumes the
|
|
|
|
backend (device emulation) is handled by a dedicated process running on the
|
|
|
|
host. This daemon is called __virtiofsd__ and needs to be present on the host.
|
2019-06-27 17:24:36 +00:00
|
|
|
|
2019-12-03 20:37:30 +00:00
|
|
|
_Build virtiofsd_
|
2019-06-27 17:24:36 +00:00
|
|
|
```bash
|
2022-02-18 14:29:23 +00:00
|
|
|
git clone https://gitlab.com/virtio-fs/virtiofsd
|
|
|
|
pushd virtiofsd
|
|
|
|
cargo build --release
|
|
|
|
sudo setcap cap_sys_admin+epi target/release/virtiofsd
|
2019-06-27 17:24:36 +00:00
|
|
|
```
|
2022-02-18 14:29:23 +00:00
|
|
|
|
2019-06-27 17:24:36 +00:00
|
|
|
_Create shared directory_
|
|
|
|
```bash
|
|
|
|
mkdir /tmp/shared_dir
|
|
|
|
```
|
|
|
|
_Run virtiofsd_
|
|
|
|
```bash
|
|
|
|
./virtiofsd \
|
|
|
|
-d \
|
2020-01-13 06:51:18 +00:00
|
|
|
--socket-path=/tmp/virtiofs \
|
2022-02-18 14:29:23 +00:00
|
|
|
--shared-dir=/tmp/shared_dir \
|
|
|
|
--cache=never
|
2019-06-27 17:24:36 +00:00
|
|
|
```
|
2019-11-15 16:53:16 +00:00
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
The `cache=never` option is the default when using `virtiofsd` with
|
|
|
|
Cloud Hypervisor. This prevents from using the host page cache, reducing the
|
|
|
|
overall footprint on host memory. This increases the maximum density of virtual
|
|
|
|
machines that can be launched on a single host.
|
2019-11-15 16:53:16 +00:00
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
The `cache=always` option will allow the host page cache to be used, which can
|
|
|
|
result in better performance for the guest's workload at the cost of increasing
|
|
|
|
the footprint on host memory.
|
2019-06-27 17:24:36 +00:00
|
|
|
|
2021-04-27 07:49:00 +00:00
|
|
|
### Kernel support
|
2019-06-27 17:24:36 +00:00
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
Modern Linux kernels (at least v5.10) have support for virtio-fs. Use of older
|
|
|
|
kernels, with additional patches, are not supported.
|
2019-06-27 17:24:36 +00:00
|
|
|
|
|
|
|
## How to share directories with cloud-hypervisor
|
|
|
|
|
|
|
|
### Start the VM
|
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
Once the daemon is running, the option `--fs` from Cloud Hypervisor needs
|
|
|
|
to be used.
|
2019-06-27 17:24:36 +00:00
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
Both direct kernel boot and EFI firmware can be used to boot a VM with
|
|
|
|
virtio-fs, given that the cloud image contains a recent enough kernel.
|
2019-06-27 17:24:36 +00:00
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
Correct functioning of `--fs` requires `--memory shared=on` to facilitate
|
|
|
|
interprocess memory sharing.
|
|
|
|
|
|
|
|
Assuming you have `focal-server-cloudimg-amd64.raw` and `vmlinux` on your
|
|
|
|
system, here is the Cloud Hypervisor command you need to run:
|
2019-06-27 17:24:36 +00:00
|
|
|
```bash
|
|
|
|
./cloud-hypervisor \
|
2021-04-28 08:37:59 +00:00
|
|
|
--cpus boot=1 \
|
|
|
|
--memory size=1G,shared=on \
|
2020-07-02 16:30:16 +00:00
|
|
|
--disk path=focal-server-cloudimg-amd64.raw \
|
2021-04-28 08:37:59 +00:00
|
|
|
--kernel vmlinux \
|
|
|
|
--cmdline "console=hvc0 root=/dev/vda1 rw" \
|
2020-06-04 19:19:24 +00:00
|
|
|
--fs tag=myfs,socket=/tmp/virtiofs,num_queues=1,queue_size=512
|
2019-06-27 17:24:36 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Mount the shared directory
|
2022-03-17 16:48:03 +00:00
|
|
|
|
|
|
|
The last step is to mount the shared directory inside the guest, using the
|
|
|
|
`virtiofs` filesystem type.
|
|
|
|
|
2019-06-27 17:24:36 +00:00
|
|
|
```bash
|
|
|
|
mkdir mount_dir
|
2022-03-24 14:11:00 +00:00
|
|
|
mount -t virtiofs myfs mount_dir/
|
2019-06-27 17:24:36 +00:00
|
|
|
```
|
2019-11-15 16:53:16 +00:00
|
|
|
|
2022-03-17 16:48:03 +00:00
|
|
|
The `tag` needs to be consistent with what has been provided through the
|
|
|
|
Cloud Hypervisor command line, which happens to be `myfs` in this example.
|
|
|
|
|
2022-03-24 14:11:00 +00:00
|
|
|
## DAX feature
|
2022-03-17 16:48:03 +00:00
|
|
|
|
2022-03-24 14:11:00 +00:00
|
|
|
Given the DAX feature is not stable yet from a daemon standpoint, it is not
|
|
|
|
available in Cloud Hypervisor.
|