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
[ostechnix@kvmhost ~]$ 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.
[ostechnix@kvmhost ~]$ chmod 777 ~/KVM_Share
Next set the correct SELinux type for the KVM shared folder:
$ sudo semanage fcontext -a -t svirt_image_t "/home/sk/KVM_Share(/.*)?"
This command sets the default file context for anything under and including the shared folder i.e.
/home/sk/KVM_Share in our case.
After you running the above command, it appends a line to the
/etc/selinux/targeted/contexts/files/file_contexts.local file which makes the change persistent.
The appended line may look like this:
$ cat /etc/selinux/targeted/contexts/files/file_contexts.local This file is auto-generated by libsemanage Do not edit directly. /usr/lib/chromium-browser system_u:object_r:bin_t:s0 /usr/lib/chromium-browser/chromium-browser.sh system_u:object_r:bin_t:s0 /usr/lib/chrome-sandbox system_u:object_r:chrome_sandbox_exec_t:s0 /home/sk/KVM_Share(/.*)? system_u:object_r:svirt_image_t:s0
Next, run the following command to tell SELinux to restore file contexts on everything underneath the shared directory to what we have as being the default (i.e what semanage changes).
$ sudo restorecon -vR /home/sk/KVM_Share
If you don't set the correct SELinux type for the KVM shared folder, 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
Now it is time to configure shared folder in Virt-manager.
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.
In the next window, click "Show virtual hardware details" button on the menu bar and click "Add Hardware" button on the left pane.
Click "Filesystem" tab 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.
One last thing you should do is select the proper access mode for accessing the shared folder.
There are three possible values. They are
- 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
chownare 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:
[sk@kvmguest ~]$ mkdir ~/hostfiles
Finally, mount the shared folder in the guest machine using command:
[sk@kvmguest ~]$ 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.