Recently, i created a very cheap Linux - Debian gaming-streaming* VFIO PC where i can play some lovely games from my tablet in home or from any internet PC with Moonlight & Sunshine streams (future guides but i will add their links as references at the bottom of this guide so you know what i am talking about) and to support my home network with a Streaming PC to handle my family's streams with total cost less than 400 euro (future post too) and i wanted to have a shared folder from the Linux Hosts to Windows 10 gaming guests*.
I am chosing the Linux Host - Windows Guest implementation over the Windows Host - Windows Guest because i think the OS diversity increases the security of implementation and it minimizes the risk of infection of host from its guests and vice versa. Also since i decided to move from Windows to Linux i wanted to learn the Linux Raid with mdadm to use it to my future projects i have in mind.
During this guide the Linux Distro and Version is Debian 11, the Host's Qemu is running without any modifications like running as root or any changes inside the config files of /etc/libvirt/ folder as many other guides suggest, you don't need to have same username on Guest machine that access the shared folder because Libvirt-qemu is the owner of KVM and shared folder and it automatically changes the owners and groups on folder to every user on Host or Guest that have create/write/read permissions of the shared folder and its files!
Enough with words let's start the KVM guide and let me tell you now that if you copy paste any command inside the quoted text below works because i used this guide too for the last KVM Guest i created when it was draft!
1. Add the user you are running Virt Manager to libvirt-qemu group by typing as root from # or sudo from $ on a user's terminal.
sudo adduser $USER libvirt-qemu
check if the user is now on libvirt-qemu group with this commnand:
2. Create the folder you want to share and change the ownership to libvirt-qemu as root from # or sudo from $. Personal preference is the /mnt directory since for this reason created on Linux.
sudo mkdir /mnt/Win10SharedFiles
sudo chown -R libvirt-qemu:$USER /mnt/Win10SharedFiles
If you don't want to have problems with write permissions because our linux user has not any rights on /mnt in general, use this command to give him write rights on the folder you just created.
sudo chmod -R +775 /mnt/Win10SharedFiles
If you copied files and folders on this folder re-type this command to apply the new permissions to these files and folders too.You can re-type this command anytime you want even after the end of this guide but with the Guest Machine in shutdown state!
sudo chmod -R +775 /mnt/Win10SharedFiles/*
You can check that everything is good with this command: ls -la
3. Now we are creating the filesystem share from inside Virt Manager (the window of the above image) with the Windows 10 Guest in shutdown state this way.
+ Add Hardware > Filesystem and we choose
Windows Option
Type: mount
Mode: passthrough
Source path: /mnt/Win10SharedFiles
Target path: Win10SharedFiles
INFO: The Virt Manager displays passthrough as hypervisor default and when you press aplly it displays passthrough.
If you choose all the 4 above options press apply to create your new filesystem share and if you want the Guest Machine to has Read only access inside the Folder you should tick the Export filesystem as readonly mount (i haven't try it yet).
Now edit the previous entry in XML of Virt Manager and add the virtiofs driver by adding <driver type='virtiofs' queue='1024'/> after the 1st line. The xml should be like this after the last addition:
<filesystem type='mount' accessmode='passthrough'>
<driver type='virtiofs' queue='1024'/>
<source dir='/mnt/Win10SharedFiles'/>
<target dir='Win10SharedFiles'/>
<address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
</filesystem>
Don't worry if the numeric values are different at your VM, the important of this step is to add the <driver type='virtiofs' queue='1024'/> line and only!
Also you should add manually under the <domain line of our Windows Guest xml these 4 lines. Press Overview of your windows Guest machine and press XML and add after the </metadata> exactly where is the memory options and after the </currentMemory> insert these 4 lines on the same coloumn with it.
<memoryBacking>
<source type='memfd'/>
<access mode='shared'/>
</memoryBacking>
INFO: To allow virt-manager edits the XML you should have enable it by ticking the option enable xml editing in Virt Manager preferences (the main window of Virt Manager, not the window of windows guest)
4. After that step add an option to apparmor to the local file that is unaffected from the updates to make this folder writable from libvirt-qemu.
sudo nano /etc/apparmor.d/local/abstractions/libvirt-qemu
and add this line. Probable this is a blank file but if there are things inside it from other programs add this line below of them.
/mnt/Win10SharedFiles/(,**) rw,
This step is also 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. Also there are already installed apparmor settings with a apparrmor profile and you can check them with aa-status as root.
and better after a restart of the Host to be sure that the changes on the Host appplied correct...
5. Start the Windows Guest and be prepared to install 2 drivers and 1 service inside the Guest that will auto-mount your Linux's Host shared folder as Z: Network Drive by following the
WinSFP guide from its github page (and this is the
WinSFP gitlab page). During the next paragraphs i will notice only the important steps since its a bit techie article and i know that streamers will have problems to succefully install the 2 drivers and the risk for failure is big if they follow the easy way... the right click GUI way!
a) Download and install the
SFP driver from github (when i installed it was the
version v1.12.22339 you see below as WinFsp 2022.2 that is digital signed and this version this guide follows). Only the Core you need (as the image below shows) and nothing else. You can find the description and the newer versions if you press the link on version above but if you read the newer versions releases text you will see that many changes happened on this driver and maybe this guide is not the correct for the newer versions!
b) Now, if you already have enabled the VFIO kernel module for your Win 10 guest gaming machine it's time to re-attach the virtio-win iso from Red Hat as cdrom image... We will copy everything from the folder viofs/w10/amd64 that you can find inside the files of the virtio-win iso to C:Utils\VirtioFS folder of the guest (i am using the name Utils as directory on C: to add inside it any app or service that i want to run without installer because it is easier for me their backup) and if you don't have the
VFIO windows drivers available on your PC you can find them here:
https://github.com/virtio-win/virtio-win-pkg-scripts
(i am using the
virtio-win-0.1.225.iso version for this guide and on my home but the latest version the time i published the guide changed and now is
virtio-win-0.1.229.iso and maybe in the future has changed if you press the link on the previous paragraph. it's up to you what you will use because i don't think there is reason to not use the latest. if it fails and you have used the last try the version 0.1.225 i am using )
c) If you did the previous steps, copy everything from viofs/w10/amd64 folder of the virtio ISO image that is mounted as Cdrom files to your new C:Utils\VirtioFS directory like there are on the image below.
CAUTION: If you try to install the driver and service with right click on inf file, by using the PowerShell as administrator (like the image below) or cmd without Administrator they will all fail even the inf reported that installed in simple and not a administrator cmd.
THE RIGHT WAY IS THE OLD SCHOOL CMD AS ADMINISTRATOR WAY FOLKS** ...
d) Right click on a cmd.exe shortcut and run as Administrator! change your working directory to the folder with our files with this command:
and type these 3 commands on the Administrator terminal that have path the C:Utils\VirtioFS directory :
sc create VirtioFsSvc binpath= "C:\Utils\VirtioFS\virtiofs.exe" start= auto depend= "VirtioFsDrv" displayname= "Virtio FS Service"
%SystemRoot%\System32\pnputil.exe -i -a *.inf
Update 26/1/2024: if the sc start fails to start means that the binpath is wrong and if you want to delete the wrong service use the next command to re-create the service with the sc command as Administrator on cmd.
The space after = on each parameter on the 1st command is important otherwise the command will fail. Better copy paste everything as it is here!
After the 3rd command as you see from the above image Win 10 OS reports that the service started, and if not check your antivirus/antimalware program for reports or any group policy (3 times so far never failed) or do a restart of the Win Guest machine and the Shared Folder from Host will be there on explorer as ... Z:\ like the network mounts and if you check the permissions from both guest and host you will see that are correct like the image below. Also the copy speed is really impressive since it is not a really network path but emulates it!
That's it, happy KVM Folder and File sharing ... on Linux Hosts with its Windows 10 Guests!
p.s. if you confused with names and where you should create them i will write their explanations and their locations now to have them as guide:
/mnt/Win10SharedFiles = the shared folder on Host.
Win10SharedFiles = the shareName name you will use as shared name on Virt Manager's Target Path and you will see it as z:sharename inside the Guest machine.
better use everything as it is here to avoid make mistake at least to the 1st KVM.