Skip to content

Share Files Between KVM Host and Linux Guest Using Virtiofs

Share Files Between KVM Host and Linux Guest Using Virtiofs - Feature Image

In KVM, file sharing between the host and its Linux and Windows guests is completely different. For Linux guests, you can use the NFS or the Virtiofs file system to share files, while for Windows guests, you can use Samba file shares. In this post, I’ll show how to share files between KVM host and Linux guest using virtiofs.

Existing remote file systems, such as NFS and 9P, that are used to share files with Linux guests will have some communication overhead between the host and the guest since they were designed for network architecture.

Since virtualization allows several guests to operate on the same physical host, their proximity to the physical host allows them to quickly access shared memory. As a result, a new file system called virtiofs was developed to take advantage of this proximity in order to achieve semantics and performance comparable to local file systems.

To illustrate this, on the host system, I will share my home directory (/home/madhu) with the Debian guest virtual machine. You can choose any other location on the host or even a different Linux distribution for the guest VM.

So let’s begin.

The shared directory can be set graphically or via the command line. I’ll show you both ways. Choose the one that best suits your needs.

Share Directory Using Virtual Machine Manager

You need to add two XML components to share a directory in Virtual Machine Manager. However, as XML editing is disabled by default in Virtual Machine Manager, you have to enable it first.

So, open the Virtual Machine Manager and enable XML editing.

Share Files Between KVM Host and Linux Guest Using Virtiofs - XML Enable

Then, select the guest to which you want to mount the host’s shared directory and press the Open button. In the new window that appears, click on View, followed by Details.

Share Files Between KVM Host and Linux Guest Using Virtiofs - Open Details

First, add the XML component for shared Memory Backing. Memory Backing enables how virtual memory pages are backed by host pages.

So, select the Overview option, then the XML tab, and in the <domain> section and just under the <currentMemory> element, add the XML component for the shared Memory Backing. Then press the Apply button.

Share Files Between KVM Host and Linux Guest Using Virtiofs - Shared Memory

The XML component for the shared memory backing is:

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

In the above XML component,

  • source type="memfd" -> This defines memory backing source type. memfd is a specialized anonymous memory-backed file.
  • access mode="shared" -> This enables memory to be shared rather than kept private.

Next, add the XML component for sharing the host directory with the Linux guest. If you want to share several directories, repeat the steps below for each shared directory.

From the main guest window, click Add Hardware, then in the new window that appears, select Filesystem, then the XML tab, and then add/edit the XML component required to share the hosts' directory. Finally, hit the Finish button to complete the process.

Share Files Between KVM Host and Linux Guest Using Virtiofs - Shared Directory

Update: The last time I checked, in Virtual Machine Manager v4.0.0, you can now add shared directory details directly into the "Details" tab without having to type in XML components.

The XML component to share the directory is:

<filesystem type="mount" accessmode="passthrough">
  <driver type="virtiofs"/>
  <source dir="/home/madhu"/>
  <target dir="my_host_home"/>
</filesystem>

In the above XML component,

  • type="mount" -> A host directory to mount in the guest. This is the default type if one is not specified.
  • accessmode="passthrough" -> The source is accessed with the permissions of the user inside the guest.
  • driver type="virtiofs" -> The hypervisor driver used to provide the filesystem, in this case, virtiofs.
  • source dir="/home/madhu" -> The directory in the host you would like to share with the Linux guest. In this case, it is the home directory on the KVM host computer.
  • target dir="my_host_home" -> The arbitrary string used to identify the shared directory to be mounted within the guest. I named it "my_host_home", but you name it whatever you want.

Finally, start the Debian-11 guest virtual machine by selecting Virtual Machine from the menu and then selecting the Run option.

Share Directory Using Virsh Command-Line Interface

If your host system lacks a graphical user interface or you just prefer to use the command-line interface to configure your shared directory, you can use the virsh command-line tool.

The following method is similar to what you did in the last section using the Virtual Machine Manager, but this time via a command-line interface.

So, first, list the available guest virtual machines.

[[email protected] ~]$ sudo virsh list --all
 Id   Name        State
----------------------------
 -    Debian-11   shut off

Open the Debian-11 guest XML configuration file for modification.

[[email protected] ~]$ sudo virsh edit Debian-11

Add the two blocks of XML for the shared memory backing and the shared directory. Make sure that the shared memory backing XML component is in the <domain> section and the shared directory XML component is in the <devices> section. After that, save the configuration file and exit.

<domain type='kvm'>
  <name>Debian-11</name>
  <uuid>1e675147-aec8-414e-8f30-97b417ba1f41</uuid>
  <metadata>
    <libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
      <libosinfo:os id="http://debian.org/debian/11"/>
    </libosinfo:libosinfo>
  </metadata>
  <memory unit='KiB'>4194304</memory>
  <currentMemory unit='KiB'>4194304</currentMemory>
  <memoryBacking>
    <source type="memfd"/>
    <access mode="shared"/>
  </memoryBacking>
  <vcpu placement='static'>2</vcpu>
...
...
...
  <devices>
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none' discard='unmap'/>
      <source file='/var/lib/libvirt/images/Debian-11.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </disk>
    ...
    ...
    ...
    <filesystem type="mount" accessmode="passthrough">
      <driver type="virtiofs"/>
      <source dir="/home/madhu"/>
      <target dir="my_host_home"/>
    </filesystem>
  </devices>
</domain>

Now, start the Debian-11 guest virtual machine.

[[email protected] ~]$ sudo virsh start Debian-11 
Domain 'Debian-11' started

Mount the Shared Directory Within the Guest Operating System

After setting the host's shared directory with the Virtual Machine Manager or the virsh command-line tool, you must now mount it within the guest operating system.

So, within the guest, run the terminal emulator and create a directory where you want to mount the shared directory. I'll create a directory called 'Host-Home' in the Debian guest's home directory. You can create a directory anywhere and call it whatever you like.

[email protected]:~$ pwd
/home/madhu

[email protected]:~$ mkdir -v Host-Home
mkdir: created directory 'Host-Home'

To temporarily mount the shared directory, use the following command.

[email protected]:~$ sudo mount -t virtiofs my_host_home /home/madhu/Host-Home

The 'my_host_home' is the arbitrary string you used before in the 'target dir="my_host_home"' element to identify the shared directory.

If you would like the shared directory to be permanently mounted within the guest, open the /etc/fstab file and add the following line.

[email protected]:~$ sudo vim /etc/fstab
...
...
my_host_home  /home/madhu/Host-Home  virtiofs  defaults  0  0

Finally, mount all filesystems mentioned in /etc/fstab.

[email protected]:~$ sudo mount -a

That's all; your selected shared directory has now been mounted within the guest virtual machine.

Watch on YouTube

Tags:
guest
0 Comments
Inline Feedbacks
View all comments
Ads Blocker Image Powered by Code Help Pro

Ad-Blocker Detected !!!

Please consider supporting me by disabling your Ad-Blocker. It really helps me to maintain this website so I can provide even better content.

To view the content, disable Ad-Blocker and refresh the page.