2022-08-13 00:16:19 +00:00
---
2023-05-29 09:14:35 +00:00
title: Share a host directory with a guest using virtiofs
2022-08-13 00:16:19 +00:00
description:
published: true
2023-05-29 09:20:07 +00:00
date: 2023-05-29T09:20:05.658Z
2022-08-13 00:16:19 +00:00
tags:
editor: markdown
dateCreated: 2022-08-13T00:16:17.437Z
---
2023-05-29 09:14:35 +00:00
# Sharing a directory between the host and the guest
2022-08-13 00:16:19 +00:00
2023-05-29 09:14:53 +00:00
## Virtio-fs in a nutshell
2022-08-13 00:16:19 +00:00
2023-05-29 09:14:35 +00:00
[Virtio-fs ](https://virtio-fs.gitlab.io/ ), shorts for *Virtio shared FileSystem* , allows for a directory located on the host to be shared with a guest.
2023-01-24 16:27:18 +00:00
2023-05-29 09:14:35 +00:00
It is designed to be fast and optimized for local usage, when the host and the guest are located on the same physical machine. It is therefore a perfect fit for Phyllome OS.
2023-01-24 16:49:39 +00:00
2023-05-29 09:14:35 +00:00
Just as with other `virtio` devices, `virtio-fs` requires specialized drivers to be written for the host and the guest operating system.
2023-02-02 19:05:43 +00:00
2023-05-29 09:17:29 +00:00
## Guest configuration
2023-01-24 16:27:18 +00:00
2023-05-29 09:14:35 +00:00
> For KVM/QEMU, as of January 2023, virtio-fs is only available for virtual machines managed by the system libvirt instance (`qemu:///system`)
{.is-warning}
> As of January 2023, `virtio-fs` does not support read-only mode, meaning a guest will be able to write to the host's folder.
{.is-warning}
2023-01-24 16:27:18 +00:00
### Edit XML configuration
* Memory backing needs to be added to the XML definition:
2022-08-13 00:16:19 +00:00
```
< domain type = "kvm" >
[...]
< memoryBacking >
< source type = "memfb" / >
< access mode = "shared/>"
< / memoryBacking >
[...]
2023-01-24 16:27:18 +00:00
< / domain >
2022-08-13 00:16:19 +00:00
```
2023-02-02 19:13:10 +00:00
* Add the filesystem device to the guest. In this the following case, the directory `/opt/share/` will be shared with the guest:
2022-08-13 00:16:19 +00:00
```
< domain type = "kvm" >
[...]
< devices >
[...]
< filesystem type = "mount" accessmode = "passthrough" >
< driver type = "virtiofs" / >
2023-02-02 19:05:43 +00:00
< source dir = "/opt/share" > # The host directory to be shared with the guest
2023-02-02 19:13:10 +00:00
< target dir = "share" > # The target dir value refers to the mount tag used inside the guest, not the target dir inside the guest
2022-08-13 00:16:19 +00:00
< / filesystem >
[...]
< / devices >
[...]
< / domain >
2023-01-24 16:27:18 +00:00
```
### Mount the folder inside the guest
2023-02-02 19:13:10 +00:00
* Inside the guest VM, mount the folder using the following command to mount the `/opt/share` host directory to the guest, using also the `/mnt` point:
2023-01-24 16:27:18 +00:00
2023-05-29 09:17:29 +00:00
```
# mount -t virtiofs share /mnt/
```
2023-01-24 16:27:18 +00:00
2023-05-29 09:17:29 +00:00
* To make it permanent, add the following line to `/etc/fstab` :
2023-02-02 19:13:10 +00:00
```
2023-05-29 09:14:35 +00:00
share /mnt/ virtiofs rw,noatime,_netdev 0 2
2023-02-02 19:13:10 +00:00
```
2023-05-29 09:19:35 +00:00
* Make sure it works before rebooting the guest virtual machine, by unmounting the share and reloading the `systemd` daemon`
2023-02-02 19:13:10 +00:00
2023-05-29 09:17:29 +00:00
```
2023-05-29 09:19:35 +00:00
# umount /mnt/ && systemctl daemon-reload
2023-05-29 09:17:29 +00:00
```
2023-02-02 19:13:10 +00:00
2023-05-29 09:17:29 +00:00
* and then mounting all share available in `fstab` :
2023-02-02 19:13:10 +00:00
2023-05-29 09:17:29 +00:00
```
2023-05-29 09:20:07 +00:00
# mount -all
2023-05-29 09:17:29 +00:00
```
2023-02-02 19:13:10 +00:00
2023-01-24 16:27:18 +00:00
## Resources
* Official website: https://virtio-fs.gitlab.io/index.html#status
* In the context of a Windows guest: https://github.com/virtio-win/kvm-guest-drivers-windows/wiki/VirtIO-FS:-Shared-file-system
2023-01-24 16:49:39 +00:00
* https://wiki.archlinux.org/title/Libvirt#Sharing_data_between_host_and_guest
2023-01-24 16:27:18 +00:00
2023-05-27 19:34:15 +00:00
---
*[**Go to parent page**](https://wiki.phyllo.me/)*