To KVM or not to KVM
Posted: Fri Dec 20, 2019 1:56 pm
Virtual machines are really cool.
But VirtualBox isnt so much...
Firstly it is proprietary and secondly it is really horribly slow.
But let us not fret because we do have Qemu/KVM available in Linux.
I love KVM because it is blazing fast(almost @bare-metal speeds) but needs a little bit of effort to get it going.
Here's how:
Open a terminal and install the following:
Then add your user to the libvirt group:
Edit the /etc/libvirt/qemu.conf file and append the following:
...to enable uefi boot.
Make sure the appropriate module is loaded @boot:
If you need nat networking please start the default network:
You will need to create a bridge interface to use with
vms if you need them to share your host network.
It is easy to create one via the network-manager cli or nmcli thus:
WARNING:Do the above only if you have an ethernet(wired) interface,but NOT if you only have wifi, as wifi is not an ideal candidate for bridging
Start and enable libvirt service
Then reboot and the bridge should be up and running.
You can assign the bridge interface to guests in Virtual Manager UI by:
Pressing the "NIC xxxx" tab of the VM-->
"Specify shared device name"-->
and choosing "br0"
instead of the "default NAT" interface.
Thats it!!!
Now you can use Virtual Manager and have fun with VM-ing around.
Here is a link to Virtual Manager page https://virt-manager.org
ADDENDUM: THINLY-PROVISIONED DISKS
One drawback of kvm is that it assigns all disk space at once to the VM disks that it creates whether your VM will be using it or not.
Thus it is heavy on disk space consumption.
Fortunately there is a workaround.
Create a directory where you wish to store your KVM disks ---for example ~/kvm-disks:
Open a terminal and install the following:
Then enter the following:
Likewise you might wish to create 10G 20G 60G etc disk templates to have them ready to use when creating VMs.
Take care to assign the correct sizes and names to the above commands accordingly.
Then before creating a new VM just go to ~/kvm-disks and COPY and PASTE the desired size disk, renaming it to the VM name you wish to create.(Take care not to delete the ".qcow2" extension) .
And in Virtual Manager do NOT create a NEW disk but IMPORT the one you already created.
Then after you finished creating disk templates according to your needs you might wish to remove the "libguestfs-tools".Its up to you.
Thats it!
ADDENDUM: GPU-PASSTHROUGH
Sometimes it is useful to "pass" a video graphics card for example nvidia from the host to the guest.This is particularly useful if one needs to run graphics-demanding apps and games on the guest.It is easy to do so in KVM.
This is HOW:
NOTE:Although I have tested and used this in my own MX and it seems to be functioning normally I BEAR NO RESPONSIBILITY in case of unstable or unresponsive system after applying these steps.PLEASE PROCEED AT YOUR OWN RISK!!!
YOU NEED TO HAVE AN EXTRA GRAPHICS CARD YOU CAN AFFORD TO PASS TO THE GUEST.
As an extra layer of caution we enable the REISUB functionality:
In this way if our machine freezes or becomes unstable while applying these steps we simply PRESS and HOLD Right-ALT+PRT SC and type "reisub".This will unfreeze and reboot the system.
First EDIT your "/etc/default/grub" file and in the line GRUB_CMDLINE_LINUX="" append:
Then run "sudo update-grub" to apply the changes.
Now run
lspci -nn | grep -i nvidia
to get your nvidia card's id,something like the following:
In my case the nvidia card id will be "10de:1398"
Now create a file
/etc/modprobe.d/vfio.conf
with the lines:
Make sure to load the appropriate kernel module @boot:
Now REBOOT your machine.
If everything went smoothly you will have now a functioning IOMMU.
Verify this:
IF YOU DONT SEE ANY ERRORS please continue with Virtual Manager
IMPORTANT THIS ONLY WORKS WITH VMs SET TO BOOT WITH UEFI.
Open your Virtual Manager and press the "Add Harware"--> "PCI Host Device"
Choose your graphics card.APPLY the changes and start your VM.
HOPEFULLY your guest will boot with no problem.
Once in the guest open a terminal and verify that everything worked as expected:
You should now be able to see your host NVIDIA card appearing in your guest.
NOTE:Not only can you pass your graphics card this way but also USB devices,disks and maybe even your wireless network card.
Thats it !!!
Congrats

But VirtualBox isnt so much...
Firstly it is proprietary and secondly it is really horribly slow.
But let us not fret because we do have Qemu/KVM available in Linux.
I love KVM because it is blazing fast(almost @bare-metal speeds) but needs a little bit of effort to get it going.
Here's how:
Open a terminal and install the following:
Code: Select all
apt install qemu-kvm libvirt-daemon libvirt-daemon-system virtinst libosinfo-bin bridge-utils virt-manager qemu-system gir1.2-spiceclientgtk-3.0 ovmf ebtables dnsmasq
Code: Select all
usermod -aG libvirt <user>
Code: Select all
nvram = [
"/usr/share/OVMF/OVMF_CODE.fd:/usr/share/OVMF/OVMF_VARS.fd"
]
Make sure the appropriate module is loaded @boot:
Code: Select all
echo vhost_net >> /etc/modules
Code: Select all
virsh net-start default ###and
virsh net-autostart default ###if you need it enabled @boot
vms if you need them to share your host network.
It is easy to create one via the network-manager cli or nmcli thus:
Code: Select all
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.d/99-sysctl.conf ###Enable -ip forwarding needed for bridging
sysctl -p /etc/sysctl.d/99-sysctl.conf ###apply it immediately
nmcli con add type bridge con-name br0 ifname br0 ip4 <your-desired-ip/your-network-netmask> gw4 <your-router-ip>
nmcli con mod br0 ipv4.dns <your-dns-server-ip> ipv4.method manual
nmcli con add type bridge-slave con-name br0-slave1 ifname <your-primary-eth-interface(eth0 or eth1)> master br0
nmcli con del "Wired connection 1"###and if needed 2 to delegate the network only to the bridge
Start and enable libvirt service
Code: Select all
systemctl enable --now libvirtd ###OR if in sysvinit
update-rc.d libvirtd enable && service libvirtd start
You can assign the bridge interface to guests in Virtual Manager UI by:
Pressing the "NIC xxxx" tab of the VM-->
"Specify shared device name"-->
and choosing "br0"
instead of the "default NAT" interface.
Thats it!!!
Now you can use Virtual Manager and have fun with VM-ing around.
Here is a link to Virtual Manager page https://virt-manager.org
ADDENDUM: THINLY-PROVISIONED DISKS
One drawback of kvm is that it assigns all disk space at once to the VM disks that it creates whether your VM will be using it or not.
Thus it is heavy on disk space consumption.
Fortunately there is a workaround.
Create a directory where you wish to store your KVM disks ---for example ~/kvm-disks:
Code: Select all
mkdir ~/kvm-disks && cd ~/kvm-disks
Code: Select all
sudo apt install libguestfs-tools
Code: Select all
sudo qemu-img create -f qcow2 10g-template.qcow2 10G ###Create a 10G disk template
sudo virt-sparsify -q 10g-template.qcow2 10g-template.thin.qcow2 ###Create the thinly-provisioned disk..Please wait for the command to complete--it might take some time.
sudo rm 10g-template.qcow2 ###Delete the original disk, keeping only the thinly-provisioned disk.
Take care to assign the correct sizes and names to the above commands accordingly.
Then before creating a new VM just go to ~/kvm-disks and COPY and PASTE the desired size disk, renaming it to the VM name you wish to create.(Take care not to delete the ".qcow2" extension) .
And in Virtual Manager do NOT create a NEW disk but IMPORT the one you already created.
Then after you finished creating disk templates according to your needs you might wish to remove the "libguestfs-tools".Its up to you.
Thats it!
ADDENDUM: GPU-PASSTHROUGH
Sometimes it is useful to "pass" a video graphics card for example nvidia from the host to the guest.This is particularly useful if one needs to run graphics-demanding apps and games on the guest.It is easy to do so in KVM.
This is HOW:
NOTE:Although I have tested and used this in my own MX and it seems to be functioning normally I BEAR NO RESPONSIBILITY in case of unstable or unresponsive system after applying these steps.PLEASE PROCEED AT YOUR OWN RISK!!!
YOU NEED TO HAVE AN EXTRA GRAPHICS CARD YOU CAN AFFORD TO PASS TO THE GUEST.
As an extra layer of caution we enable the REISUB functionality:
Code: Select all
echo "kernel.sysrq = 1" >> /etc/sysctl.d/99-sysctl.conf
sysctl -p /etc/sysctl.d/99-sysctl.conf
First EDIT your "/etc/default/grub" file and in the line GRUB_CMDLINE_LINUX="" append:
Code: Select all
GRUB_CMDLINE_LINUX="..... intel_iommu=on" ###For Intel processors OR
GRUB_CMDLINE_LINUX="..... amd_iommu=on" ###For AMD processors
Now run
lspci -nn | grep -i nvidia
to get your nvidia card's id,something like the following:
Code: Select all
root@mx:~# lspci -nn | grep -i nvidia
01:00.0 3D controller [0302]: NVIDIA Corporation GM107M [GeForce 845M] [10de:1398] (rev a2)
Now create a file
/etc/modprobe.d/vfio.conf
with the lines:
Code: Select all
options vfio-pci ids=10de:1398 ###SUBSTITUTE your own id.In case your nvidia card has an audio capability you might get an additional id and in that case seperate them with comma.
Code: Select all
echo 'vfio-pci' > /etc/modules-load.d/vfio-pci.conf#####IN CASE OF SYSTEMD
echo 'vfio-pci' >> /etc/modules#####IN CASE OF SYSVINIT
If everything went smoothly you will have now a functioning IOMMU.
Verify this:
Code: Select all
dmesg | grep -E "DMAR|IOMMU"
dmesg | grep -i vfio
IMPORTANT THIS ONLY WORKS WITH VMs SET TO BOOT WITH UEFI.
Open your Virtual Manager and press the "Add Harware"--> "PCI Host Device"
Choose your graphics card.APPLY the changes and start your VM.
HOPEFULLY your guest will boot with no problem.
Once in the guest open a terminal and verify that everything worked as expected:
Code: Select all
lspci | grep -i nvidia
NOTE:Not only can you pass your graphics card this way but also USB devices,disks and maybe even your wireless network card.
Thats it !!!
Congrats


