This Guide is the first of the many articles that will cover the tech part of my movement from Windows to Linux for everything including... gaming!
Recently, i have been experimenting with Linux guests on a Linux KVM Host that i will use as streaming PC because of Software Raid 1 and i faced a problem when i tried to share a folder from the KVM Host to KVM Guest on a Debian 11 KVM. Of course there is an easy way to move files from KVM Guests with usb disks or flash drives (with Virt Manager is just an Add Hardware > Usb Host Device) but after a while this mount procedure on Linux KVM Guest becomes annoying...
So, i started searching for a permanent way to share a folder from the Host Machine to its Linux Guest and i found a better way than all the promoted ways i found on the internet and without changing the permissions of the libvrt service by taking the root permissions like many suggest on their guides or by changing the shared folder permissions inside the home folder to 777 that means ... free for all inside their home folder.
With the guide that follows your Qemu & the Libvirt are both running without any modifications or any changes inside their config files of the /etc/libvirt/ folder. Also you don't need to have the same username on both Host and Guest machine that access the shared folder as many guides suggest too. Because Libvirt-qemu is the owner of the KVM machine it controls and automatically changes the owners and groups on the shared folders to every user on Host or Guest that have create/write/read permissions of the shared folder and its files! And finally i will use the Virt Manager because we can do everything with it even to edit the configuration files of our KVM Guests Machines and it is a simple, clean and perfect piece of GUI software.
|
Virt Manager Menu is just perfect!
|
Enough with words lets start the guide.
On Linux KVM Host Machine:
1. Add the normal user you are running Virt Manager to libvirt-qemu group if not already with this command as root from # or sudo from $ on a user's terminal.
sudo adduser <username> libvirt-qemu
check if your user added to the libvirt group with this command: groups $(whoami).
2. Create the folder you want to share as root from # or sudo from $ with mkdir command and chance the ownership to libvirt-qemu. Personal preference is the /mnt folder and not the home of the user as many suggest because for this reason created the special /mnt folder on Linux and on this folder we are manually mounting our usb devices or even entire disks.
sudo mkdir /full/shared/folder/path
sudo chown -R libvirt-qemu:<username> /full/shared/folder/path
With the last command you changed the Libvirt-qemu as the owner and all users who belong to Libvirt-qemu group will inherit and have the write ability on host folder (including our user that we added on the Libvirt-qemu group before). Adjust or Fix (if you copied a folder) the write permissions with the next command .
sudo chmod -R +775 /full/shared/folder/path
You can check that everything is good with this command on the same path with your shared folder: ls -la.
3. Now, you can creat the filesystem share for your Linux guest from inside Virt Manager with VM in shutdown state this way.
Open the Virt Manager and when you have selected your closed KVM Guest you choose to Add a Hardware. The [Add a Hardware] button is on the bottom left corner of Virt Manager of the above image. You can add the folder as Filesystem by pressing Browse next to Source Path and this button will open a smaller window. On this smaller window you should choose the Browse Local (very important step this) to select the folder you created before to share with the guest.
+ Add Hardware > Filesystem and you choose
Type: mount
Mode: mapped
Source path: /full/shared/folder/path
Target path: shareName
The source path should be full path while the Targed path only the name you choose to be the shared name inside the Guest. After that you press apply to create the filesystem share. if you want the Guest Machine to have Read only files for the guest inside the Folder we tick the Export filesystem as readonly mount.
Very important right now is to edit the Filesystem entry you just created. The easy way is to press the XML tab next to Details but this is available only if you have ticked the option on Virt Manager Preferences > General > Enable XML editing. Otherwise use the virsh edit command.
Inside the xml you add inside the 1st line 2 options fmode="0770" and dmode="0770". and the
<filesystem type="mount" accessmode="mapped">
to become
<filesystem type="mount" accessmode="mapped" fmode="0770" dmode="0770">
and you press apply to take the changes.
The above settings in Details of your Filesystem in xml will look like this now if you press the XML tab in Virt Manager.
<filesystem type="mount" accessmode="mapped" fmode="0770" dmode="0770">
<source dir="/full/shared/folder/path"/>
<target dir="shareName"/>
<alias name="fs0"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x0c" funtion="0x0"/>
</filesystem>
alias name and slot values maybe are different on your settings except /full/shared/folder/path and shareName.
4. After that step you should add an option to apparmor (if your distro using it, if not escape the 4 step) to the local file that is unaffected from the updates to make the shared folder readable and writable from libvirt-qemu.
sudo nano /etc/apparmor.d/local/abstractions/libvirt-qemu
and you add this line. probable this is a blank file but if there are things inside it from other programs we add this line below of them.
/full/shared/folder/path/(,**) rw,
this is very important because this command allows the local user inside guest machine to take inheritance permission from the libvirt-qemu user to write the files & and folders inside the shared folder of the host if you want to allow him to write.
On Linux KVM Guest Machine now,
and better after a restart of the Host to be sure that the changes on the Host applied correct...
5. Now, startup the VM and inside the guest machine you should create a folder that you will use to mount the shared folder from Host! lets say that you will use the /mnt folder again. Try to add to this folder a very unique name to not have future conflicts with names. as root from # or sudo from $
sudo mkdir /mnt/GuestSharedFolder
6. and you can check that you can mount the shared folder with this command as root from # or sudo from $
sudo mount -t 9p -o trans=virtio,version=9p2000.L,rw shareName /mnt/GuestSharedFolder
(if it failed to mount maybe the reason is that is not loaded the 9p driver and you can manually load it with:
command as root from # or sudo from $)
if you can mount the share folder from Host now it's the time to check the write permissions from both Guest and Host and if everything is ok we finished... but if you want to make it permanent you need to follow 1 more step...
7. First you will permanent load the 9p modules in your guest VM or the fstab will crash and it will go to maintainance mode. Type as root from # or sudo from $ inside guest VM
sudo nano /etc/initramfs-tools/modules
and you are adding inside this file these 3 lines
9p
9pnet
9pnet_virtio
now create new initramfs image with this command as root from # or sudo from $
and finally add the line that it will auto mount our shared folder inside our Guest Machine with this command as root from # or sudo from $ again
and at the end of this file write
shareName /mnt/GuestSharedFolder 9p trans=virtio,version=9p2000.L,rw 0 0
That's it, happy KVM Folders sharing from Linux Hosts with their Linux KVM Guests!
p.s. If you confused with names and where you should create them i will write their explanations and their locations now to use them as guide:
/full/shared/folder/path = the shared folder of the Host.
shareName = the name you will use as shared name on Virt Manager on Target Path and inside the Guest machine as shareName.
/mnt/GuestSharedFolder = the mount point on Guest machine of the shared folder from Host.
I tried to not use many images and only one and describe everything else with words because it is already a big guide and the images are taking space and i didn't want to confuse you while you are copy pasting the commands.
References about sharing on KVM from Linux Hosts to their Linux Guests.