without using libvirt
install kvm
apt-get install kvm qemu
now create a disk image
qemu-img create -f raw /vm/windows.img 100G
(note: creating a raw image has severale advantages: if your filesystem supports sparse files, it only uses the actually used space of your virtual disk on your physical disk.. all journalling filesystems that use inodes support that, so ext4 for example works fine. second you can easily mount it using mount -o loop <imgfile> <mountpoint> at any time. however, it does not support snapshots, use qcow2 if you need snapsots or if your filesystem does not support sparse files)
in my case i had a windows 7 cd that i installed from.. if your server dos not have a cd rom you can also us an iso image instead.
since I want to use virtio for fast hdd emulation, we also need the virtio drivers
cd /vm wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso
run qemu and install windows. access the display via VNC
qemu-system-x86_64 -enable-kvm -k de-ch -name windows -vnc :1 -cdrom /dev/cdrom -drive file=/vm/virtio-win.iso,media=cdrom -boot d -drive file=/vm/windows.img,if=virtio,format=raw,index=0 -m 4096
now connect with your favourite vnc client to the ip of your server and install windows. you need to load the derivers from the second cdrom that is emulated. make sure you choose the correct virtio storage driver for your windows version. it may load the wrong one if you just select the root directory and let the installer search for it, resulting in an unstable windows vm
once the installation is done, shut down your windows vm. it is now time to make it autostart upon boot of your linux machine.. in order to do that i used Thomas Martin’s kvm-simple-init of which i keep a local copy (dated 2016-01-23)
this script will also send a shutdown command to all vms once the server shuts down, so no need to manually do that in order to gracefully shutdown your vm’s
by default qemu uses nat-ed network, so that the vm is in its own network and the vm host acts as a nat router. if you want your vm to be part of your network like any physical machine you have connected to your swich you can use a bridged connection..
you need to install bridge-utils to be able to create a bridge network device
apt-get install bridge-utils
now change your /etc/network/scripts
file and basically replace eth0 with br0 and then add three lines for the bridge config. here the end result on mine:
auto br0 iface br0 inet static address 192.168.168.1 netmask 255.255.255.0 network 192.168.168.0 broadcast 192.168.168.255 gateway 192.168.168.254 bridge_ports eth0 bridge_stp off bridge_maxwait 5
Notice: you won’t need any block for eth0 after you defined the bridge interface.
now either reboot your machine or try to get your eth0 down using ifconfig
and then get br0 up using ifup
however be careful when you are doing this all remoetely!! in that case make really really sure you got your config right or even better, setup a backdoor, make sure your remote management module is accessible, whatever.. and then reboot your server.. and hope
now in your qemu command line you ened to add the parameters
-net nic,model=virtio-net-pci -net tap
the default qemu-ifup script in /etc/ will do the rest for you.
Notice you will need virtio drivers for the nic to work. you can leave the model=
option away on the first boot in order to download the virtio iso and install the drivers before switching over to virtio. however, using the e1000 (default if no model is given) is not recommended for windows VM’s as this model has a tendency to crash and lose connection under heavy use. I’ve experienced this first hand on a CCTV server that had to monitor a couple of cameras. once the nic crashes, one has to go to network settings in windows and disable, then enable the card and it will be back online. I followed the suggestions in a Proxmox forum thread and switched to virtio.
here is an example configuration for a windows 7 machine using the above mentioned init script.. if you use another init script or run kvm manually you can simply use all the cli options listed below as an example of what you might want to pass on to qemu.. also take a look at the man page it is very informative and helpful!
# Should be unique among all VMs MONITOR_PORT=5801 # Not mandatory, but useful to keep it in a distinct variable VNC_DISPLAY=1 # KVM parameters KVM_OPTS="\ -enable-kvm \ -k de-ch \ -name windows \ -drive file=/vm/windows.img,if=virtio,format=raw,index=0 \ -m 4096 \ -net nic,model=virtio-net-pci \ -net tap \ -vnc :$VNC_DISPLAY \ -monitor tcp:127.0.0.1:$MONITOR_PORT,server,nowait \ -monitor vc \ -daemonize \
I had a windows 7 license laying around so i used that but wanted to upgrade to win 10. unfortunately the update crashed with error
0xC1900101 - 0x20017 The installation failed in the SAFE_OS phase with an error during BOOT operation
this was easily fixed by adding a
-cpu core2duo
to my options in the above config file. keep in mind you need to stop your VM and start it again for this change to be activated.. I first removed this setting again after the update and windows 10 ran fine, but when it installed an automatic update and i accidently killed the vm during the installation it wouldn’t boot anymore and it repeatedly showed a BSOD saying there was a SYSTEM_THREAD_EXCEPTION_NOT_HANDLED
. at first i tried fixing my windows installation using a wind10 dvd but that was not successfull as i couldn’t even boot from the dvd.. when i ran out of options i re-enabled the core2duo cpu and voilà, it bootet straight away, finished its updates and we’re all good
that brings me to another note regarding windows vm’s:
when the host system is shut down, akk kvm vm’s will be sent a acpi power down message to gracefully shut down. the problem is, if the vm does not support this feature or crashes during shutdown, the host system will in turn hang on its shutdown and you might loose control over it when you connect from remotely.. so Thomas set a timout for the shutdown command to complete and after this timeout, his init script will simply switch the vm off. this is very good in order to keep the host system under control but it can badly harm especially Windows VM’s as windows likes to install updates during the shutdown of a machine and therefore can take literally hours to shut down.
there are two solutions to this problem.. an easy one and a more complex one:
-
Easy: disable automatic updates and do your updates manually so you can reboot your VM and give it all the time it wants to complete the updates
-
Complex: use the qemu monitor command
screendump <filename>
then run some command line ocr across the screen dump and see what it is actually doing.. using this output, prolong the timeout in the init script..
for once i decided to go the easy way.. the problem about the complex solution is, that this only works once you know what to look for in the screenshot.. and who knows what microsofts “i am updating, don’t you turn me off” message might look like in the future.. in times of error messages like “something went wrong” my expectations aren’t too high regarding a consisten message in that stage.. further more, my host system is improtant to me, it does lots of thing sand my windows vm is only used to do one single nice-to-have job (OCR on PDF Files) which i can easily live without for a moment.. so the host system has absolute priority over the windows VM and therefore i don’t want to wait hours for the windows machine to complete its updates and not have any of my other other services (files, mails etc.) available at that time..
windows 10 gives you a hard time when it comes to disabling updates.. it really insists on doing some of its updates automatically..
in windows 10 pro you can set some goup policies and disable automatic updates but in windows 10 home you need to work around this issue by setting your network connection to be a “metered connection” so windows won’t download updates over it.
Pro — set group policies
since i am using a windows 7 pro on my vm (i upgraded from a 7 ultimate i had laying around which turned into 10 pro upon the upgrade) i will disable auto updates using the official way:
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Sign up
I love Ubuntu, especially when I wanted to work with python programming, But I also love windows 10 because of its GUI and the vast variety of software support.
In the last blog, I covered how you can install windows 10 on Centos/Redhat KVM. But in this tutorial I going to cover the Debian derivative Ubuntu.
And the steps mentioned here are exactly the same for the other Debian derivative Linux mint.
Since I get a lot of questions with regards to Ubuntu specifically I decided to write separate blogs for Ubuntu and Redhat, so if you are using Centos/Redhat/Fedora you may click here.
Prerequisite.
Ubuntu machine with good configuration.
Download the Windows ISO image by following the guide here.
Windows virtio drivers iso download latest.
Download Spice guest tools.
1. How to install KVM on Ubuntu?
The first step is to prepare the environment by installing the KVM utilities on the Ubuntu host.
On my PC I am using Ubuntu version 20.04 so enter the below commands to install KVM on an ubuntu machine. The Same command can be used with as well.
sudo apt update sudo apt install qemu-kvm bridge-utils virt-manager libosinfo-bin -y
You may now go ahead and reboot the host, if you dont reboot the machine then you will end getting a below error.
2. Open the virtualization manager and create the VM.
Open terminal and type virt-manager
In the virtual machine manager window click on create a new virtual machine.
A new window now will pop up. In the New VM wizard select Local install media and click on forward.
You will now have to browse for the windows 10 iso that you have downloaded and Uncheck the option which says automatically detect from the installation media.
Start searching for the window10 under choose operating system, and check the box which says include end of life operating system. Once you check the box you will be able to see the Microsoft Windows 10 operating system in the list.
Once you select the Microsoft Windows 10 click on forward.
3. Configure the virtual machine resources.
Next, you will be asked to define the memory and the CPU, I am giving 4GB of memory and 2 CPU’s and click on forward.
Depends on your configuration you may choose yours.
When you configure the virtual hard disk, by default Ubuntu KVM would pick up 40GB, since I don’t require that much space I just added only 20GB and clicked forward.
You may choose 40GB or more.
Finally, you will have to Name the windows KVM VM and check the option Customize configuration before install.
I also selected the network as Virtual network ‘default’ NAT. By selecting this network you guest VM will start using the Ubuntu host internet connection.
Click on Finish at this moment now.
4. Configure the virtual Hardware’s.
Since you selected customize option you will now be presented with the KVM customization screen.
Do not click on Begin installation yet, before that we have to modify some hardware changes.
Click on the SATA Disk1 , and on the right side expand the advanced options and click on the Disk bus drop-down list.
Select VirtIO as the disk bus, and then click on Apply.
You now have to add VNC Viewer as the graphics, so click on Add hardware from the bottom left.
In the add New virtual hardware windows, select Graphics and in the Type select VNC Server and click on Finish.
Click on Network interface card (NIC), change the device model to VirtIO, and click on Apply.
5. Add the Windows 10 virt drivers and configure the boot loader.
Remember that we have downloaded VirtIO Windows drivers for Ubuntu KVM, let’s attach that now. So click on Add hardware.
Select the storage and click on manage and attach the VirtIO iso file that we have downloaded, and also change the Disk type to CDROM device and click on Finish.
The second CDROM now has VirtIO drivers and the first one has the windows 10 ISO image.
Change the boot order for windows
Let’s go ahead and change the Boot order of the device, first being the CDROM1 and the VirtIO disk and the CDROM2.
Note : The important part is for the KVM to load the windows 10 image from the list.
Check the option Enable boot menu.
Once you rearranged the boot loader click on Apply.
6. Configure the qemu CPU the right way.
Click on CPU and change the current allocations to 4
Check the options which say manually set CPU topology.
In the topology assign 1 socket, 2cores and 2threads.
7. Begin the windows 10 VM installation.
We just finished adding the required virtual hardware, you may now click on Begin installation.
If everything went well, you should be able to see the windows logo, and after few seconds Windows prompt where you have to choose the language and the time and date and the keyboard click on Next.
You will have to click on install now on the next prompt.
If you have the product license key, you may enter it now. Else click on I don’t have a product key, I choose the same as I didn’t have the product key during the installation.
Select the version that you would like to install, I am choosing the version Windows 10 Pro.
Accept the license agreement and click on Next.
You now have two options to install the windows, Either upgrade or Custom, Choose custom here.
8. Install the Redhat SCSI controller driver.
Remember we have added the storage of 20GB in the beginning, but we are not able to see them so click on Load driver here.
You will get a load driver prompt click on OK.
Once you click on OK you will be able to see all the drivers like below.
Select the windows 10 SCSI driver and click on Next.
Note: If the driver doesn’t show up then you may follow the below steps.
- Click on Browse and expand the VirtIO driver iso file CDROM.
- Go down and expand the folder viostor.
- Expand Win10
- And select amd64 and click on OK.
- You should now be able to see the drive listed now.
After you selected the drive and once you click on Next after a few seconds you should be able to see the virtual hard disk that you have attached earlier, select that and click on Next.
The windows installation now begins and starts to copy the files now.
After a couple of restarts, you should get the windows setup screen where you need to have the basic configuration to be done.
During the process, you could either connect the Microsoft account or create a local account.
And you should be presented with the windows home screen like below.
9. How to install virtio drivers windows 10.
After the windows 10 installation is completed, you now have to install the VirtIO drivers to windows 10. Post which the networking, display graphics, copy-paste functionality, and others should work.
Type This PC and right click and click on Manage.
In the device manager, you should be able to see there are some drivers that are not installed under Other devices.
Just right click on each driver and click on update driver. Let’s see how we can do it for the Ethernet controller.
Click on Browse my computer for drivers
In the browse section select the VirtIO iso CDROM that we have attached.
You should be able to see the drive letter as E, check the box which says include subfolders, and click on Next.
You should be able to see the pop up to install the Redhat ehternet Adapter driver to the machine click on Install.
Similarly do it for other dirvers as well.
Note: I couldn’t quite get the ‘Unknown device’ to get it working under other drivers, so it should be fine.
There is one more driver that you need to install, which is Display adapters, By default it has picked up Microsoft Basic Display Adapter.
Just like you did the driver update for other devices, do the same thing for the display adapter as well.
10. Install SPICE Guest tools.
There is a SPICE Guest agent that you need to install now. For that you need to transfer the file from Ubuntu host machine to the windows.
Steps to transfer files from Ubuntu host to Windows 10 guest vm.
In windows create folder called Driver.
Now right click and click on properties.
Click on the Sharing tab, and click on Advanced sharing.
Check the option Share this folder and click on Permissions.
Check all the permissions now, you may cancel this later once you transfer the files.
Now the folder is shared, go to Files in Ubuntu and click on Other locations.
In the connect to server address bar, type smb://windows host IP and hit enther.
You will be asked to provide the windows 10 credentials, enter it now.
Paste the Spice guest tools here
Now go back to the windows 10 and you should be able to see the Spcie guest tools software there, double click on it install the software, and you should be good to go.
11. Detach the CDROM1 and 2.
Awesome! you just installed windows 10 on your Ubuntu KVM. If you wanted to know how you can manage this VM when you wanted to access it, you can check out the article here.
At this point you should be good to go however, next time when you shutdown the Guest VM, you need to make sure that you remove the CDROM drive 1 and two that you had attached during the installation.
In source path, you may click on the x icon to detach.
Below is the video, if you prefer to watch over video.
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/
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.173-2/virtio-win-0.1.173.iso
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!
Intro
Windows in KVM is one of the best hypervisors that built right into Linux.
Virtualization is one of the most amazing tools used by many to test drive any operating system they wish to install.
Virtualbox, VMware, and even Microsoft’s Hyper-V (Windows) are a great selection of software for anyone to start using a virtual machine.
One of the most popular hypervisors in Linux that is natively supported out of the box is KVM (Kernel-based Virtual Machine).
I previously used this virtual machine from my video on demonstrating how to install Arch Linux with LVM.
KVM can manage an enormous network of virtual labs running Linux without a sweat, but what about Windows?
Getting the perfect setup
After spending a fair amount of time searching for a well-written guide in order to proceed on this task, I decided to only focus on running a new single Windows instance.
My previous goal used to be attempting to run macOS in order to see how well it would perform, but I realized it takes more work than I preferred to put in, which was something I wouldn’t enjoy.
I had to copy and paste various commands to add each driver code, which led me to abandon the effort.
After seeing how simple the steps to install Windows would be, I rolled with it instead.
Picking Windows 7 was an automatic choice since 8 and 10 are bloated by default, and it would be a waste of time trying to optimize the OS.
Applications that are installed automatically such as Candy Crush are difficult to remove on Windows.
Windows 8 is less of a burden, but I found more support online for 7 so picked it instead.
Nonetheless, it detected Windows 7 and ran flawlessly after applying the recommended patches.
What’s great about running KVM
After getting the Windows OS driver optimized under QEMU for it to render properly, it felt like brand new.
Driver settings such as networking, file system storage, and sound worked out of the box in the Windows guest.
The Windows guest was plug and play because of the virtIO drivers offered by Red Hat.
Usually, I would have to fiddle with it in order for the Windows guest to work, but the virtIO drivers made the performance all fine and dandy.
For the driver performance, the Windows guest felt more responsive than before on VirtualBox, and it seemed like a setup I might prefer over time.
If you have a Linux PC that’s a bit slow in virtualization, then KVM is your best bet when trying out Windows.
Since it’s a type-1 hypervisor, it has direct access to the hardware, which gives it more horsepower for it to use up.
Without further ado, I’ll demonstrate how easy it is to begin installing Windows on it.
I’ll be using Ubuntu 18.04.
Resources
Commands
Install the KVM and QEMU network drivers
sudo apt install qemu qemu-kvm libvirt-bin bridge-utils virt-manager build-essential
find your user
whoami
Disable password requirement with the user when launching virt-manager
sudo gpasswd -a (your_username) kvm
Enable the network and reboot the computer
sudo systemctl enable libvirtd virtlogd
reboot
Conclusion
Hopefully, this should get you up to speed and start exploring what KVM and QEMU on Linux have to offer.
Once all of the performance optimizations are applied, you should have a performant virtual machine with each driver for the guest well-established!
It does take more time for it to add each driver code as compared to VirtualBox, but KVM’s features might convince you otherwise.
If you would like to continue where I gave up, try installing macOS under Linux and let me know how it goes.