xref: /cloud-hypervisor/docs/fs.md (revision 1a09381d1e947a1e89d313d7fb8db09ad5d6dda4)
1c0336e83SSebastien Boeuf# How to use virtio-fs
2c0336e83SSebastien Boeuf
32dc9cfafSSebastien BoeufIn the context of virtualization, it is always convenient to be able to share a
42dc9cfafSSebastien Boeufdirectory from the host with the guest.
5c0336e83SSebastien Boeuf
62dc9cfafSSebastien Boeuf__virtio-fs__, also known as __vhost-user-fs__ is a virtual device defined by
72dc9cfafSSebastien Boeufthe VIRTIO specification which allows any VMM to perform filesystem sharing.
8c0336e83SSebastien Boeuf
9c0336e83SSebastien Boeuf## Pre-requisites
10c0336e83SSebastien Boeuf
11c0336e83SSebastien Boeuf### The daemon
12c0336e83SSebastien Boeuf
132dc9cfafSSebastien BoeufThis virtual device relies on the _vhost-user_ protocol, which assumes the
142dc9cfafSSebastien Boeufbackend (device emulation) is handled by a dedicated process running on the
152dc9cfafSSebastien Boeufhost. This daemon is called __virtiofsd__ and needs to be present on the host.
16c0336e83SSebastien Boeuf
17c81e808eSSebastien Boeuf_Build virtiofsd_
18c0336e83SSebastien Boeuf```bash
199acb69f3SRob Bradfordgit clone https://gitlab.com/virtio-fs/virtiofsd
209acb69f3SRob Bradfordpushd virtiofsd
219acb69f3SRob Bradfordcargo build --release
229acb69f3SRob Bradfordsudo setcap cap_sys_admin+epi target/release/virtiofsd
23c0336e83SSebastien Boeuf```
249acb69f3SRob Bradford
25c0336e83SSebastien Boeuf_Create shared directory_
26c0336e83SSebastien Boeuf```bash
27c0336e83SSebastien Boeufmkdir /tmp/shared_dir
28c0336e83SSebastien Boeuf```
29c0336e83SSebastien Boeuf_Run virtiofsd_
30c0336e83SSebastien Boeuf```bash
31c0336e83SSebastien Boeuf./virtiofsd \
32af958c94SWei Liu    --log-level debug \
33f427d944SSebastien Boeuf    --socket-path=/tmp/virtiofs \
349acb69f3SRob Bradford    --shared-dir=/tmp/shared_dir \
35*1a09381dSWei Liu    --cache=never \
36*1a09381dSWei Liu    --thread-pool-size=$N
37c0336e83SSebastien Boeuf```
38d9695a0fSSebastien Boeuf
392dc9cfafSSebastien BoeufThe `cache=never` option is the default when using `virtiofsd` with
402dc9cfafSSebastien BoeufCloud Hypervisor. This prevents from using the host page cache, reducing the
412dc9cfafSSebastien Boeufoverall footprint on host memory. This increases the maximum density of virtual
422dc9cfafSSebastien Boeufmachines that can be launched on a single host.
43d9695a0fSSebastien Boeuf
442dc9cfafSSebastien BoeufThe `cache=always` option will allow the host page cache to be used, which can
452dc9cfafSSebastien Boeufresult in better performance for the guest's workload at the cost of increasing
462dc9cfafSSebastien Boeufthe footprint on host memory.
47c0336e83SSebastien Boeuf
48*1a09381dSWei LiuThe `thread-pool-size` option controls how many IO threads are spawned. For
49*1a09381dSWei Liuvery fast storage like NVMe spawning enough worker threads is critical to
50*1a09381dSWei Liugetting an acceptable performance compared to native.
51*1a09381dSWei Liu
5271d68e4fSRob Bradford### Kernel support
53c0336e83SSebastien Boeuf
542dc9cfafSSebastien BoeufModern Linux kernels (at least v5.10) have support for virtio-fs. Use of older
552dc9cfafSSebastien Boeufkernels, with additional patches, are not supported.
56c0336e83SSebastien Boeuf
57c0336e83SSebastien Boeuf## How to share directories with cloud-hypervisor
58c0336e83SSebastien Boeuf
59c0336e83SSebastien Boeuf### Start the VM
60c0336e83SSebastien Boeuf
612dc9cfafSSebastien BoeufOnce the daemon is running, the option `--fs` from Cloud Hypervisor needs
622dc9cfafSSebastien Boeufto be used.
63c0336e83SSebastien Boeuf
642dc9cfafSSebastien BoeufBoth direct kernel boot and EFI firmware can be used to boot a VM with
652dc9cfafSSebastien Boeufvirtio-fs, given that the cloud image contains a recent enough kernel.
66c0336e83SSebastien Boeuf
672dc9cfafSSebastien BoeufCorrect functioning of `--fs` requires `--memory shared=on` to facilitate
682dc9cfafSSebastien Boeufinterprocess memory sharing.
692dc9cfafSSebastien Boeuf
702dc9cfafSSebastien BoeufAssuming you have `focal-server-cloudimg-amd64.raw` and `vmlinux` on your
712dc9cfafSSebastien Boeufsystem, here is the Cloud Hypervisor command you need to run:
72c0336e83SSebastien Boeuf```bash
73c0336e83SSebastien Boeuf./cloud-hypervisor \
74235eb522SSebastien Boeuf    --cpus boot=1 \
75235eb522SSebastien Boeuf    --memory size=1G,shared=on \
76a3342bdbSSebastien Boeuf    --disk path=focal-server-cloudimg-amd64.raw \
77235eb522SSebastien Boeuf    --kernel vmlinux \
78235eb522SSebastien Boeuf    --cmdline "console=hvc0 root=/dev/vda1 rw" \
79a8cdf2f0SBo Chen    --fs tag=myfs,socket=/tmp/virtiofs,num_queues=1,queue_size=512
80c0336e83SSebastien Boeuf```
81c0336e83SSebastien Boeuf
82c0336e83SSebastien Boeuf### Mount the shared directory
832dc9cfafSSebastien Boeuf
842dc9cfafSSebastien BoeufThe last step is to mount the shared directory inside the guest, using the
852dc9cfafSSebastien Boeuf`virtiofs` filesystem type.
862dc9cfafSSebastien Boeuf
87c0336e83SSebastien Boeuf```bash
88c0336e83SSebastien Boeufmkdir mount_dir
89afd9f17bSSebastien Boeufmount -t virtiofs myfs mount_dir/
90c0336e83SSebastien Boeuf```
91d9695a0fSSebastien Boeuf
922dc9cfafSSebastien BoeufThe `tag` needs to be consistent with what has been provided through the
932dc9cfafSSebastien BoeufCloud Hypervisor command line, which happens to be `myfs` in this example.
942dc9cfafSSebastien Boeuf
95afd9f17bSSebastien Boeuf## DAX feature
962dc9cfafSSebastien Boeuf
97afd9f17bSSebastien BoeufGiven the DAX feature is not stable yet from a daemon standpoint, it is not
98afd9f17bSSebastien Boeufavailable in Cloud Hypervisor.
99