User Tools

Site Tools


linux:kvmqemu:mounthostdir

KVM / QEMU Mount a host directory in a VM

With the VM shut down, open the VM settings (View Details), select “OS Information” on the list, change to XML and under <currentMemory unit=“KiB”>2097152</currentMemory> add

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

Apply the changes.

Then add Hardware and select Filesystem
in the XML text set

<filesystem type="mount" accessmode="passthrough">
   <driver type="virtiofs"/>
   <binary path="/usr/lib/qemu/virtiofsd"/>
   <source dir="/home/test/shared"/>
   <target dir="host_shared"/>
   <alias name="fs0"/>
   <address type="pci"/>
</filesystem>

KVM VMM will automatically assign parameters to the address line

<address type="pci" domain="0x0000" bus="0x01" slot="0x01" function="0x0"/>

If any other parameter than type=“pci” is set, no auto-assignment is applied.

Set

<source dir="/mnt/shared"/>

to the directory you want to share on the host and

<target dir="host_shared"/>

to a name you use in the VM to mount it. Then, apply the changes.

Start the VM. in the vm the directory can now be mounted (/mnt/shared must exist):

sudo mount -t virtiofs host_shared /mnt/shared

To have the share mounted at boot add to /etc/fstab

host_shared /mnt/shared virtiofs rw,relatime 0 0

If the vm should not be able to change files on the host, use ro instead of rw.

linux/kvmqemu/mounthostdir.txt · Last modified: 2023/02/12 16:55 by olaf