KVM in Practice | Share a Host Folder with VMs using Virtiofs on KVM/QEMU

This tutorial shows how to share a host directory with Linux guests on a local KVM/QEMU setup using **Virtiofs**—the simplest built-in shared-folder option with near-native performance.

Introduction

Why Virtiofs

Virtiofs is a convenient way to share a directory from a Linux host to a Linux guest.

Compared with network-based sharing methods such as Samba or NFS, virtiofs is often simpler for a local VM setup because no network service needs to be configured. It is also designed specifically for virtualized local filesystem sharing.

Some advantages are:

  • No networking setup required
  • Near-native performance
  • Proper UNIX permission passthrough
  • Designed for **local Linux host + Linux guest** workflows

Compatibility

The setup documented in this post has been tested on the following environments:

  • Host OS: Ubuntu 26.04, 24.04
  • Guest OS: Ubuntu 24.04, 20.04

In this article, I use the following example host directory:

/mnt/datastore

and expose it to the guest using the virtiofs mount tag:

datastore

Inside the guest, it will be mounted at:

/mnt/datastore

Of course, these names can be changed according to your own setup.

Method

Step 1: Install virtiofsd on the host

On Ubuntu/Debian host:

sudo apt install virtiofsd

Make sure the host directory exists:

sudo mkdir -p /mnt/datastore

Optionally, set permissions for easy access:

sudo chmod 777 /mnt/datastore

Step 2: Power off the VM

Before changing the VM hardware configuration, power off the guest.

From *virt-manager*, shut down the VM normally.

Or from CLI:

virsh shutdown <vm-name>

Check the VM state:

virsh list --all

Step 3: Add the shared folder to the VM

There are two common ways to add the virtiofs shared folder: 1 using *virt-manager*, 2 editing the libvirt XML from CLI.

The two methods are equivalent. *virt-manager* is easier to use interactively, while the XML method is more explicit and easier to reproduce.

#### Option A: Add the shared folder using virt-manager

Ensure the VM is powered off, then:

  1. Open **virt-manager**,
  2. Select the VM and open **Details**,
  3. Click **Memory** tab,
  4. Enable **Shared memory** checkbox,
  5. Apply changes;
  6. Click **Add Hardware** button -> select **Filesystem**,
  7. Configure:
    • **Driver:** `virtiofs`
    • **Source path:** `/mnt/datastore`
    • **Target path:** `datastore`
    • **Access mode:** `Passthrough`
  8. Apply changes.

#### Option B: Add the shared folder from CLI using libvirt XML

Alternatively, edit the VM XML directly:

virsh edit <vm-name>

First, add the following top-level `memoryBacking` section under `<domain>`, outside `<devices>`:

<memoryBacking>
  <source type='memfd'/>
  <access mode='shared'/>
</memoryBacking>

Then, inside the <devices> section, add the virtiofs filesystem device:

<filesystem type='mount' accessmode='passthrough'>
  <driver type='virtiofs' queue='1024'/>
  <source dir='/mnt/datastore'/>
  <target dir='datastore'/>
</filesystem>

Save and exit. `virsh edit` will validate and apply the XML change.

The final shape should be similar to:

<domain type='kvm'>
  [...]
  <memoryBacking>
    <source type='memfd'/>
    <access mode='shared'/>
  </memoryBacking>
  [...]
  <devices>
    [...]
    <filesystem type='mount' accessmode='passthrough'>
      <driver type='virtiofs' queue='1024'/>
      <source dir='/mnt/datastore'/>
      <target dir='datastore'/>
    </filesystem>
    [...]
  </devices>
</domain>

Step 4: Start the VM

Start the VM from *virt-manager* or CLI:

virsh start <vm-name>

Step 5: Mount the shared folder in the guest

Inside the guest, create a mount point:

sudo mkdir -p /mnt/datastore

Then mount the virtiofs filesystem:

sudo mount -t virtiofs datastore /mnt/datastore

Here, datastore is the mount tag specified in the VM configuration:

<target dir='datastore'/>

It is not the host path.

After mounting, the host directory `/mnt/datastore` is available inside the guest at `/mnt/datastore` with read/write access.

Step 6: Make the mount persistent

To mount it automatically when the guest boots, add the following line to `/etc/fstab` inside the guest:

datastore  /mnt/datastore  virtiofs  defaults  0  0

Then test the `fstab` entry:

sudo mount -a

Check that it is mounted:

findmnt /mnt/datastore

Extra Notes

About the virtiofs mount configuration

The meaning of the key fields in the xml configuration is:

  • `<source dir='/mnt/datastore'/>` is the directory on the host,
  • `<target dir='datastore'/>` is the mount tag used inside the guest,
  • `<driver type='virtiofs'/>` selects virtiofs,
  • `accessmode='passthrough'` makes file ownership and permissions pass through naturally,

The virtiofs mount command uses the mount tag, not the host directory path.

The `queue='1024'` option does not mean 1024 queues. It means that the virtiofs request queue can hold up to 1024 request entries. In most cases, leaving it as 1024 is fine.

About shared memory

Virtiofs requires the guest memory to be shared with the host-side virtiofs process. This is enabled by the `<memoryBacking>` section in the XML, and the "Shared memory" checkbox in virt-manager.

About UID/GID and permissions

File ownership and permissions are enforced by the host filesystem.

For the smoothest experience, it is recommended to use the same UID/GID for the normal user on both the host and the guest.

Use `id` command on both host and guest to check the UID/GID of your user. If the user has UID 1000 on both sides, permission handling is usually straightforward.

About choosing virtiofs vs a virtual disk

Use virtiofs when the directory should be naturally shared between host and guest. Use a normal virtual disk when the data should belong primarily to the VM.

Reference

  1. [libvirt: Sharing files with Virtiofs](https://libvirt.org/kbase/virtiofs.html)
  2. [libvirt: Domain XML format](https://libvirt.org/formatdomain.html)
  3. [virtio-fs: A shared file system for virtual machines](https://virtio-fs.gitlab.io/)