How to mount a host directory inside a KVM virtual machine

When using QEMU/KVM, there are many methods to share data between the host system and guest virtual machines. In this tutorial, we focus on creating a filesystem passthrough, using virt-manager or by manually editing a virtual machine definition, and we learn how to mount a host directory inside a virtualized guest.

In this tutorial you will learn:

  • How to create a filesystem passthrough using virt-manager
  • How to create a filesystem passthrough by manually editing a virtual machine XML definition
  • How to mount a host directory on a virtualized guest system
How to mount a host directory inside a QEMU/KVM virtual machine
How to mount a host directory inside a QEMU/KVM virtual machine – Original image by DC Studio on Freepik
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution-agnostic
Software virt-manager, virsh
Other None
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Introduction

We talked about virtual machines in previous tutorials, when we saw how to create and manage KVM virtual machines from the command line and how to create snapshots of virtualized systems. In certain situations we may need to share data between the host and a guest system. There are many possible solutions to this problem; one consists in the creation of a filesystem passthrough. This is possible, however, only on system virtualized via the “system” libvirtd instance (qemu:///system); it doesn’t work on unprivileged libvirtd “session” instances (qemu:///session).

Creating a filesystem passthrough using virt-manager

In order to create a filesystem passthrough in an already exising QEMU/KVM guest system by using virt-manager , we click on the “lamp” icon in the toolbar of the virtual machine viewer window. This will give us access to the virtual machine configuration details:

QEMU/KVM virtual machine details
QEMU/KVM virtual machine details



The first thing we need to do, is to click on the “Memory” entry in the left column, then tick the “Enable shared memory” checkbox and apply the change by clicking on the “Apply button”, in the bottom right of the page. If the virtual machine is running, virt-manager will notify us that the changes will take effect after the next shutdown:

Enabling shared memory on the virtual machine
Enabling shared memory on the virtual machine

We now need to click on the “Add Hardware” button in the bottom left of the page, then, on “Filesystem” in the left column of the window that will be opened. To create the filesystem passthrough we have to provide a source and a target path. The source path is the directory on the host machine we want to mount inside the guest system; the target path, instead, it is not really a path, but the label we use to reference the passthrough from the virtual machine.



In the example below, we share the /home/doc/Downloads directory on the host machine, and use the “host_doc_downloads” label to reference it. If we want the directory to be accessible in readonly mode, perhaps for security reasons, we can tick the “Export filesystem as readonly mount” checkbox. When ready, we click on the “Finish” button and confirm we want to make the device available.

Creating the filesystem passthrough
Creating the filesystem passthrough

The passthrough will be available after the next “proper” system shutdown (we need to actually shutdown the machine; a reboot will not be effective).

Creating a filesystem passthrough by editing the virtual machine XML file

If we don’t use virt-manager, we can create a filesystem passthrough directly by editing the virtual machine XML definition. In order to get the list of all existing virtual machines, we can use the virsh script:

$ sudo virsh list --all

In this case, the command returns the following output:

Id Name    State
--------------------------
8 debian12 running



To edit the virtual machine definition, we can use the virsh-edit command, passing the virtual machine name as argument:

$ sudo virsh edit debian12

The XML file will be opened in the default editor. To enable shared memory, we add the following code under the “domain” section:

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

Then, to add the directory passthrough, we add the following code, this time inside the “devices” section:

<filesystem type="mount" accessmode="passthrough">
  <driver type="virtiofs"/> <source dir="/home/doc/Downloads"/>
  <target dir="host_doc_downloads"/>
  <address type="pci" domain="0x0000" bus="0x07" slot="0x00" function="0x0"/>
</filesystem>

Once ready, we save and close the file. To make the configuration effective, we need to shut down the guest system:

$ sudo virsh shutdown debian12

Mounting the host directory

Once we created the filesystem passthrough, we can mount the host directory with the “mount” command, specifying “virtiofs” as the filesystem type, and as we said before, by referencing the shared host path via the label we used in the configuration. In this case, to mount the host directory on /mnt, inside the guest system, we would run:

$ sudo mount -t virtiofs host_doc_downloads /mnt

Conclusions

In this tutorial we learned how to create a filesystem passthrough and mount a host directory inside a QEMU/KVM guest. We saw how to create the passthrough using virt-manager or by directly editing the virtualized system XML definition with the virsh edit command. Finally, we saw how to mount the shared directory inside the guest system.



Comments and Discussions
Linux Forum