There are many tutorials out there about how to create KVM, but not many go in to details on how to do it if your guest OS is Windows and also scares info on the problems you may encounter. This is why I decided to put in brief the steps how to do it and describe the problems I had to deal with.
This guide assumes you already have a working Debian 9 installation /the steps should in general work for Debian 10 as well/. Commands has to be executed as root.
Before you start is a good idea to check some hardware capabilities of the host computer, especially if it is not relatively new.
First you need to check if your CPU supports virtualisation, to do this run the command:
egrep -c ‘(vmx|svm)’ /proc/cpuinfo
If the output of the command is 1 or 2 you are good, 0 means no go for you.
Then you need to check if the hardware virtualisation is enabled and if not you need to change the settings in your BIOS. Run this command and read the output:
dmesg | grep “disabled by bios”
If the output is “kvm: disabled by bios” you need to alter the BIOS settings, otherwise you are fine.
When ready you need to install quite a lot of packages, so in your terminal type the command:
apt-get install –no-install-recommends qemu-kvm libvirt-daemon-system libvirt-clients libvirt-daemon-system libvirt-dev libguestfs-tools genisoimage virtinst libosinfo-bin virt-viewer virt-manager acpid
Be patient, it will take some time. The reason of –no-install-recommends switch is to avoid installation on packages related to GUI, which I personally avoid on server installations.
When the installation is finished you will have to change your network interfaces so the virtual machine becomes part of your LAN. For this you will have to create a bridge interface, make sure you have the bridge-utils package installed. If it happens to have a OpenVPN server already running on the Debian server as I did, make sure you change your VPN interface to TAP and add it to the bridge, otherwise you won’t be able to browse your network.
This is how my /etc/network/interfaces file looked like after the configuration change:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
# allow-hotplug enp2s0
# iface enp2s0 inet static
# address 192.168.1.2
# netmask 255.255.255.0
# gateway 192.168.1.1
######################################
# bridge part, coment above int conf #
######################################
auto enp2s0
iface enp2s0 inet manual
auto tap0
iface tap0 inet manual
auto br0
iface br0 inet static
address 192.168.1.2
broadcast 192.168.1.255
netmask 255.255.255.0
gateway 192.168.1.1
bridge_ports enp2s0 tap0
bridge_stp off
bridge_waitport 0
bridge_fd 0
dns-nameservers 8.8.8.8
dns-nameservers 8.8.4.4
dns-nameservers 192.168.1.2
Restart the networking service (or reboot) and test your connectivity is working as intended.
Next step is to let regular user to manage the VM:
adduser your_user libvirt
adduser your_user libvirt-qemu
Then reload group membership:
newgrp libvirt
newgrp libvirt-qemu
Verify your group membership with id command:
id
Check virtual machine network and state
virsh net-list –all
virsh list –all
Bridge networking has to be configured for the VM as well, so do the following:
Create a file using a text editor
nano /root/bridged.xml
Append the following config in the file, make sure bridge interface name is the same as in your interfaces configuration:
<network>
<name>br0</name>
<forward mode=”bridge”/>
<bridge name=”br0″/>
</network>
Save and close the file. Then run the following commands to apply this configuration to the VM:
virsh net-define –file /root/bridged.xml
virsh net-autostart br0
virsh net-start br0
Veryfy bridged network
virsh net-list –all
In order Windows 10 to recognise your virtual hard disk during its installation you will need to get the vertio drivers. Create “virtio” (or some other name) folder in /var/lib/libvirt/boot/ and download the drivers (checking for the latest version is probably a good idea):
cd /var/lib/libvirt/boot/virtio/
Next create the VM’s virtual disk, adjust to your needs:
qemu-img create -f qcow2 /var/lib/libvirt/images/windows_10_x64.qcow2 80G
With the disk set it is time to create the VM:
virt-install \
–virt-type=kvm \
–hvm \
–name=windows10 \
–ram=2048 \
–cpu=host \
–vcpus=2 \
–os-type=windows \
–os-variant=win10 \
–disk path=/var/lib/libvirt/images/windows_10_x64.qcow2,format=qcow2,bus=virtio \
–disk /var/lib/libvirt/boot/Win10_1909_EnglishInternational_x64.iso,device=cdrom,bus=ide \
–disk /var/lib/libvirt/boot/virtio/virtio-win-0.1.173.iso,device=cdrom,bus=ide \
–network=bridge=br0,model=virtio \
–graphics vnc
If stumble upon error stating that the access to the KVM kernel module is denied do the following:
Open the file qemu.conf and edit the following:
nano /etc/libvirt/qemu.conf
#user = root -> user = root
#group = “root” – > group = “kvm”
After a successful VM creation you will need a VNC client to connect to the VM and start the installation of Windows. In the installation process you will have to navigate to the folder containing the virtio disk driver, mind in my case it was drive E:, but may not be the same for you. The path for me was: E:\viostor\w10\amd64. After the installation you can run RDC on Windows and set users to connect to your VM directly without third party software.
To have your VM starting automatically when the host starts or is rebooted run the commands:
First make sure libvirtd service is stared on boot:
systemctl enable libvirtd
Then run:
virsh autostart Your_VM_Name_Here
If you try to shutdown your VM from host terminal with the virsh shutdown VM_Name_Here without success this might be due to not working acpi event handler. To fix this edit the content of powerbtn file (create the file if does not exist):
nano /etc/acpi/events/powerbtn
Then type these lines (delete anything in the file if the file exists)
event=button/power
action=/sbin/poweroff
and restart the acpid service
service acpid restart
And this is the end of it in general, next is to post the script to shutdown, backup and then start the KVM when time is available.
Have fun!