Setup A Shared Folder Between KVM Host And Guest

After creating a new Virtual machine, you may want to share files and folders between the KVM host and the KVM virtual machine. In this brief guide, we will see how to setup a shared folder between KVM host and guest virtual machine using Virt-manager in Linux.

Before configuring a shared folder, make sure you have installed Virt-manager on your KVM host.

If you already have installed complete KVM Virtualization Package group, you don’t need to install Virt-manager separately. Virt-manager is part of the KVM virtualization package group. Otherwise, you may need to install it as shown in the above link.

Create a new Virtual machine from Virt-manager interface. It is very straight-forward and easy! Don’t start the VM yet. We need to create shared folder in our KVM host.

Create shared folders in KVM host

Create a new shared folder in your KVM host system that you want to share to the guest machines.

For the purpose of this guide, I created a shared folder named KVM_Share in my $HOME directory.

[[email protected] ~]$ mkdir ~/KVM_Share

Next, set up required permissions for the shared folder. I wanted to edit and modify the contents of this shared folder from my guest virtual machines, so I gave full permissions to the shared folder.

[[email protected] ~]$ chmod 777 ~/KVM_Share

Next set SELinux to permissive mode. I couldn’t figure out how to set proper SELinux policy for KVM shared folders yet, so I temporarily set the SELinux mode from targeted to permissive with the following command:

[[email protected] ~]$ sudo setenforce 0

This change is valid for the current runtime session only. To make the changes permanent, edit /etc/selinux/config file:

[[email protected] ~]$ sudo vi /etc/selinux/config

Set SELinux mode to permissive:

[...]
SELINUX=permissive
[...]

Save and close the file. Reboot your system to take effect the changes. Now it is time to configure shared folder in Virt-manager.

If you don’t set SELinux in permissive mode, you will encounter with the following error when starting the VM.

Error starting domain: internal error: qemu unexpectedly closed the monitor: 2021-05-06T09:30:21.239353Z qemu-system-x86_64: -device virtio-9p-pci,id=fs0,fsdev=fsdev-fs0,mount_tag=/hostshare,bus=pci.5,addr=0x0: cannot initialize fsdev 'fsdev-fs0': failed to open '/home/sk/KVM_Share': Permission denied
 Traceback (most recent call last): File "/usr/share/virt-manager/virtManager/asyncjob.py", line 65, in cb_wrapper callback(asyncjob, *args, *kwargs) File "/usr/share/virt-manager/virtManager/asyncjob.py", line 101, in tmpcb callback(args, **kwargs) File "/usr/share/virt-manager/virtManager/object/libvirtobject.py", line 57, in newfn ret = fn(self, *args, **kwargs) File "/usr/share/virt-manager/virtManager/object/domain.py", line 1329, in startup self._backend.create() File "/usr/lib64/python3.9/site-packages/libvirt.py", line 1353, in create raise libvirtError('virDomainCreate() failed')
 libvirt.libvirtError: internal error: qemu unexpectedly closed the monitor: 2021-05-06T09:30:21.239353Z qemu-system-x86_64: -device virtio-9p-pci,id=fs0,fsdev=fsdev-fs0,mount_tag=/hostshare,bus=pci.5,addr=0x0: cannot initialize fsdev 'fsdev-fs0': failed to open '/home/sk/KVM_Share': Permission denied

So for the time being, set SELinux is permissive moder and continue the subsequent steps.

Setup a shared folder between KVM host and guest

Open Virt-manager and make sure the guest system is turned off. Select the guest machine and click Open button on the menu bar to show the virtual machine console and details.

Show the virtual machine console and details in Virt-manager
Show the virtual machine console and details in Virt-manager

In the next window, click “Show virtual hardware details” button on the menu bar and click “Add Hardware” button on the left pane.

Click "Add Hardware" button in Virt-manager
Click “Add Hardware” button in Virt-manager

Click “Filesystem” on the left pane and the enter the shared folder location in Source path and enter mount tag under the Target path. Here, I am sharing "~/KVM_Share" directory and my mount tag is /hostshare. You can specify the name of mount_tag as you wish. Please note that mount_tag is not a path. The mount_tag is just a name used to identify the host’s share. Click Finish and Apply to update the changes.

Setup a shared folder between KVM host and guest
Setup a shared folder between KVM host and guest

One last thing you should do is select the proper access mode for accessing the shared folder.

Select the security mode for accessing the shared folder in Virt-manager
Select the security mode for accessing the shared folder in Virt-manager

There are three possible values. They are passthrough, mapped and squash.

  • passthrough – Specifies that the source (i.e. shared folder) is accessed with the User’s permission settings that are set from inside the guest virtual machine. i.e. Files are stored using the same credentials as they are created on the guest. This is the default access mode if one is not specified.
  • mapped – Specifies that the source is accessed with the permission settings of the hypervisor. If you want to have read and write access from the guest machine, you must choose this mode.
  • squash – Similar to ‘passthrough’, the exception is that failure of privileged operations like chown are ignored. This makes a passthrough-like mode usable for people who run the hypervisor as non-root.

After selecting your preferred access mode, click Apply button. We have now configured the shared folder for KVM host and guest system from Virt-manager.

Now, power on the guest virtual machine and create a mount point in your guest machine to mount the shared folder of KVM host system:

[[email protected] ~]$ mkdir ~/hostfiles

Finally, mount the shared folder in the guest machine using command:

[[email protected] ~]$ sudo mount -t 9p -o trans=virtio /hostshare hostfiles/

The shared folder ~/KVM_Share is mounted on the hostfiles directory in your virtual machine. From now on, you can access the contents of the shared folder from your guest virtual machine. You can also edit and delete the contents of the shared folder on both KVM host and guest systems.

To automatically mount the shared folder every time at boot, add the following line to /etc/fstab file in your guest system:

/hostshare /hostfiles 9p trans=virtio,version=9p2000.L,rw 0 0

Hope this helps.

Resource: