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 a KVM host and a guest virtual machine in Linux.
Before configuring a shared folder, make sure that you have installed Virt-Manager on your KVM host.
If you have already installed the entire KVM virtualization package group, you do not need to install the virtue-manager separately. The virtue-manager is part of the KVM virtualization package group. Otherwise, you may need to install it as shown in the link above.
Create a new virtual machine from the virtue-manager interface. It is very straight-forward and easy! Do not start the VM yet. We need to create a shared folder in our KVM host.
Create a shared folder in a KVM host
Create a new shared folder in your KVM host system that you want to share to guest machines.
For the purpose of this guide, I created a shared folder named
KVM_Share In my
~]$ mkdir ~/KVM_Share
Next, set the 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 permission to the shared folder.
~]$ chmod 777 ~/KVM_Share
Next set to SELinux
permissive Mode. I could not figure out how to set the proper SELinux policy for KVM shared folders, so I set SELinux mode temporarily
permissive With the following order:
~]$ sudo setenforce 0
This change is valid only for the current runtime session. To make changes permanent, edit
[[email protected] ~]$ sudo vi /etc/selinux/config
Set SELinux mode to permissible:
[...] SELINUX=permissive [...]
Save and close the file. Reboot your system for the changes to take effect. Now it is time to configure the shared folder in Virt-Manager.
If you have not set SELinux to permissible mode, you will encounter 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
Therefore, for the time being, Setinux is the permitted moder and continue the subsequent steps.
Set up a shared folder between KVM host and user
Open the Virtue Manager and make sure that the guest system is turned off. Select 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 Name” Button on the left pane.
Click “Filesystem” on the left pane and enter the shared folder location in the source path and mount tag under the target path. Here, i am sharing
"~/KVM_Share" The directory and my mount tag is
/hostshare. You can specify the name of the mount_tag as you wish. Please note that mount_tag is not a path. The mount_tag is simply a name that is used to identify part of the host. Click Finish and Apply to update the changes.
One last thing you should do is Select the appropriate access mode To access a shared folder.
There are three possible values. They are
- Withdrawal – 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. The files are stored using the same credentials as they are created on the guest. If none is specified, this is the default access mode.
- Mapped – Specifies that the source is accessed with the permission settings of the hypervisor. If you want to read and write access from the guest machine, you must select this mode.
- Squash – Similar to ‘Poscharth’, exceptions are such as failure of privileged operations
chownIs ignored. This makes a posture-like mode usable for those who run the hypervisor as non-root.
After choosing your preferred access mode, click on the Apply button. We have now configured the shared folder from shared-manager for the KVM host and guest system.
Now, to mount the power and shared folder of the KVM host system on the guest virtual machine, create a mount point in your guest machine:
[[email protected] ~]$ mkdir ~/hostfiles
Finally, mount the shared folder in the guest machine using the command:
[[email protected] ~]$ sudo mount -t 9p -o trans=virtio /hostshare hostfiles/
~/KVM_Share Is mounted
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 edit and delete the contents of the shared folder on both the KVM host and the guest system.
To automatically mount a shared folder each time on boot, add the following line to it
/etc/fstab File in your guest system:
/hostshare /hostfiles 9p trans=virtio,version=9p2000.L,rw 0 0
Hope this helps.