What is KVM
"Kernel-based Virtual Machine (KVM) is an open source virtualization technology built into Linux®. Specifically, KVM lets you turn Linux into a hypervisor that allows a host machine to run multiple, isolated virtual environments called guests or virtual machines (VMs)." - Red Hat, What is KVM?
Install required packages
To run a KVM virtual machine you need to have a Intel VT/AMD-V supported machine and you need to be running any sort of Linux distribution which provides virt-manager, libvirt and qemu packages.
For this instance I'm currently running a Artix GNU/Linux distribution(same as Arch Linux), but all the steps should be significantly the same for other distributions excluding the package names
Required Packages
libvirt
;virt-manager
;qemu-desktop
;edk2-ovmf
(Optional, but required if you're trying to virtualize Windows 11);swtpm
(Optional, but required if you're trying to virtualize Windows 11).
Prepairing Virt-Manager
Setting up a user session
After installing these packages according the OS you're running, you must now open virt-manager and create a new connection to a user qemu/kvm session. You do this by going into the file menu and by clicking on the Add Connection...
option.
Now in the Hypervisor
, select QEMU/KVM user session
.
Enabling XML Editing (Optional, but required if installing Windows 11)
Since each VM is configured in XML in th backend, XML editing will allow you to have more control over your VM.
To do this, all you have to do is, go to the Edit menu, and select Preferences
, now just tick on the option that says Enable XML editing
.
Setting up the Virtual Machine
In this section I'll be going over the steps to install Windows 11 on KVM. Some of the steps will be marked with a optional marker, which means, that the referred stage is specific to Windows 11 only.
Creating the Virtual Machine
Firstly you must click the "➕" icon and select the Local install media (ISO image or CDROM)
option.
Install media
Now go ahead and choose your installation ISO, if the OS is not automatically detected, select it manually or choose something similar to what you are trying to virtualize.
Tip: When browsing for the iso, there's no need to create a pool, you can just click on Browse local
and click on the desired ISO file
Ram and CPU
After selecting the disk, you'll be prompted with the memory and CPU settings, in this stage my only advice is to use at least 2GiB of ram.
Storage
Next up you'll be prompted with virtual storage, I recommend at least 25GiB if you're running Windows, if you're running any Linux distro I'd give it at least 15GiB.
Customize Installation (Optional, but required if installing Windows 11)
For a Windows 11 installation you need to have a UEFI, secure boot capable machine with the TPM(Trusted Platform Module) minimum version 2.0. Ahead I'll show how to take care of each instance.
TPM Module
After selecting the desired storage, go ahead and tick on the option that says Customize configuration before install
.
Here you must click on Add Hardware
and select the TPM category
Under TPM, the type must be Emulated
. In advanced options, the model must be TIS
and the version 2.0
.
Secure Boot
Still in the Customize configuration before install
window, go over to the Overview
category and set the firmware to something like UEFI x86_64: /usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd
After changing the firmware there are some hardware modules that won't working, so we must remove them to evade conflicts. To do this, still in the Overview
category, go over to the XML
tab and remove the lines below:
<controller type="pci" model="pcie-root"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
Finishing Installation
Now that you have everything setup to run the VM, just hit Finish
or Begin Installation
.
Extra: Shared Linux VM folder
Create a shared folder
In order to use a shared folder between the host machine and the guest VM, you must first create a folder in your filesystem and `chmod 777` it.
mkdir ~/KVM_Share
Adding the shared folder has a filesystem
Now open virt-manager, got to your virtual machine and under the virtual hardware tab, click on `Add Hardware`, select filesystem and in the driver option, select the virtio-9p option.
In the source path you are going to put the complete path to the host machine shared folder and in the target path you should type where the filesystem should exist inside the guest VM.
For this instance we are going to set the source path to ~/KVM_Share
and the target path to /shared
.
Mounting the shared folder
Boot your guest VM and create a folder anywhere in your filesystem where you will mount the shared folder to.
mkdir ~/hostfiles
Now, if you type: sudo mount -t 9p -o trans=virtio /shared ~/hostfiles
, the shared folder will be mounted.
Auto mount on startup
Now if you want the guest VM to auto mount the shared folder everytime you boot it, you just have to add the following line to your guest VM /etc/fstab
file:
/shared /home/user/hostfiles 9p trans=virtio,version=9p2000.L,rw 0 0
WARNING!!! The mounted folder path must be an absolute path!