Virtualisation
Introduction
The best system for handling virtual machines under Ubuntu is a combination of 5 components:
- KVM (Kernel Virtual Machine): a 'hypervisor' kernel module that is part of the Linux kernel. This provides the basic virtual machine capability.
- libvert: a library that provides an API to KVM.
- virt-manager: a GUI application that uses libvert to manage virtual machines.
- Qemu: a hardware emulator that also provides device access for virtual machines. The hardware emulation capabilities of Qemu are not needed for virtualisation, and are not covered in these notes. Note that the Qemu documentation confusingly treats virtualisation as a form of "System emulation".
See this superuser topic for more on the relationships of these components.
I originally used virtualbox to create virtual machines, but it was ridiculously slow. There are lots of websites claiming that Qemu is slower than virtualbox, but that is probably when Qemu is doing hardware emulation rather than using KVM. Qemu/KVM is much faster than virtualbox. There are also lots of websites claiming that Qemu/KVM is much harder to use than virtualbox. These claims probably pre-date the intrduction of virt-manager, which I find easier to use than virtualbox.
Installation
There are Ubuntu packages available for the virtual machine system. I followed these instructions and installed the following packages:
apt install qemu qemu-kvm virt-manager virtinst libvirt-clients bridge-utils libvirt-daemon-system
These packages are:
- qemu: fast processor emulator, dummy package
- qemu-kvm: fast processor emulator, dummy package
- virt-manager: desktop application for managing virtual machines
- virtinst: Programs to create and clone virtual machines
- libvirt-clients: Programs for the libvirt library
- bridge-utils: Utilities for configuring the Linux Ethernet bridge
- libvirt-daemon-system: Libvirt daemon configuration files
I probably didn't need all these. A lot of installation guides only install 'qemu' and 'virt-manager', and I suspect that they would have been sufficient.
Following the same installation guide, I did:
systemctl enable --now libvirtd
systemctl start libvirtd
systemctl status libvirtd
usermod -aG kvm root
usermod -aG kvm fog
usermod -aG libvirt root
usermod -aG libvirt fog
System details
There is a virtual machine daemon called 'libvirtd', which can be controlled using the usual systemd commands.
Most of the virtual machine files are under '/var/lib/libvirt/'. Images of the virtual machines are in '/var/lib/libvirt/images'.
Creating and managing a virtual machine
Creating a virtual machine
Virtual machines can be created and managed using the command 'virt-manager', which launches a GUI frontend to libvert.
To create a new virtual machine:
- Select 'File->New Virtual Machine', or click the leftmost 'display' icon.
- Select 'Local install media (ISO image or CDROM), and click 'Forward'
- Select the ISO image file (e.g. a Windows 10 image - see below), and click 'Forward'.
- Adjust memory and CPUs if required, and click 'Forward'.
- Enable virtual storage, set size, and click 'Forward'.
- Set virtual machine name, and click 'Finish'.
This will create the virtual machine and open a window for managing it.
See below for full details of creating a virtual machine running Windows 10.
Managing a virtual machine
Virtual machines can be managed using the command 'virt-manager'. Select the required virtual machine, and then click the appropriate icon to start, stop, pause or open the VM.
To delete a VM, right click on the VM in the virt-manager window and select 'Delete'.
If you click 'Open', a separate window will be opened. This has the following icons (left to right):
- Show the graphical console
- Show virtual hardware details
- Power on the virtual machine
- Pause the virtual machine
- Shut down the virtual machine
- Manage VM snapshots
The graphical console enables you to interact with the software (e.g. Windows 10) running in the VM.
The 'hardware details' allows you to view and configure hardware aspects of the VM. Some of the more interesting options:
- CPUs: set how many CPUs the VM can use.
- Boot options: Start the VM on host boot up.
- Display VNC: Address: All interfaces - allows VNC access from other machines.
There is an 'Add Hardware' button at the bottom left of the 'hardware details' page. If you click this, it opens a new window with options for accessing devices. In particular, the 'USB Host Device' provides access to USB devices.
Network problem
The Windows 10 VM lost its network connection after working OK for a few months.I dont know what had changed, but after a couple of hours of web searching I found a solution. Apparently the default network set up by QEMU/KVM is configured to bridge to "any physical device". The solution is to set up a new network that bridges to the specific named physical interface.
Windows 10 in a virtual machine
Installation
I used the following web pages for guidance:
The following steps follow the outline above, but include additional steps and details for Windows 10.
- Two ISO files are needed for the installation. These need to remain available to the VM (don't know why, since the Windows installer copies files to the "virtual" hard disk), so I created a directory /var/lib/windows10 to contain them.
- Download the 64 bit Windows ISO file to /var/lib/windows10.
- Select 'File->New Virtual Machine', or click the leftmost 'display' icon.
- Select 'Local install media (ISO image or CDROM), and click 'Forward'
- Select the ISO image file (e.g. a Windows 10 image - see below), and click 'Forward'.
- Adjust memory and CPUs if required, and click 'Forward'.
- Enable virtual storage, set size, and click 'Forward'.
- Set virtual machine name, and click 'Finish'. This start the virtual machine and open the graphical console for the VM.
- in the graphical console, follow the instructions for installing and setting up Windows. Some specific points:
- When asked for a product key, click "I don't have a product key" at the bottom of the window.
- Select "Windows 10 Home Single Language" as the Windows version - this seems to be the minimal bloat version.
- Select "Custom" as the type of installation (I don't know why it's referred to as "Custom" and "Advanced")
- Set the keyboard layout to "United Kingdom Extended".
- Select the 'Add Hardware' button at the bottom left of the 'hardware details' page, and set up the following:
- Select "USB Host Device" and select required USB device.
- Select "Graphics", set "Type" to "VNC server" and "Address" to "All interfaces". Set port to next available ('Auto' may change dynamically).
Sharing files
I used the following web page for guidance:
Windows 11 in a virtual machine
Installation
I used the following web pages for guidance:
The following steps follow the outline above, but include additional steps and details for Windows 11.
- Two ISO files are needed for the installation. These need to remain available to the VM (don't know why, since the Windows installer copies files to the "virtual" hard disk), so I created a directory /var/lib/windows11 to contain them.
- Download virtio-win.iso:
cd /var/lib/windows11
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-virtio/virtio-win.iso
Download the 64 bit Windows ISO file to /var/lib/windows10.Install the 'swtpm' package, which is needed to support TPM:add-apt-repository ppa:smoser/swtpm
apt install swtpm swtpm-tools
/usr/share/swtpm/swtpm-create-user-config-files
chmod 777 /var/lib/swtpm-localca # There is probably a better way to solve permission issues, but I couldn't be bothered
Start the VM manager:virt-manager
Select 'Edit->Preferences' and 'Enable XML editing'Select 'File->New Virtual Machine', or click the leftmost 'display' icon.Select 'Local install media (ISO image or CDROM), and click 'Forward'Select the ISO image file from /var/lib/windows11, and click 'Forward'.Change memory to 6144MB, and click 'Forward'.Enable virtual storage, set size (e.g. 80GB), and click 'Forward'. (There wasn't enough space in /var/lib to do this, so I set up a "pool" on /dev/sdc1 and created the virtual storage there.)Set virtual machine name to 'win10', select 'Customize configuration before install', and click 'Finish'. This will take you to the advanced options window.In the 'Overview' section, make sure the chipset is set to 'Q35' and set the firmware to 'UEFI x86_64: /usr/share/OVMF/OVMF_CODE_4M.ms.fd'.Select the 'XML' tab, and replace the 'hyperv' section with:<hyperv mode="custom">
<relaxed state="on"/>
<vapic state="on"/>
<spinlocks state="on" retries="8191"/>
<vpindex state="on"/>
<runtime state="on"/>
<synic state="on"/>
<stimer state="on">
<direct state="on"/>
</stimer>
<reset state="on"/>
<vendor_id state="on" value="KVM Hv"/>
<frequencies state="on"/>
<reenlightenment state="on"/>
<tlbflush state="on"/>
<ipi state="on"/>
<evmcs state="on"/>
</hyperv>
In the 'CPUs->Details' section, ensure that 'host-passthrough' is enabled.In the 'SATA DIsk 1' section, chenge the disk bus from 'SATA' to 'virtio', set the cachemode to 'none', and set the discard mode to 'unmap'.Click the 'Add Hardware' button, then select 'CDROM device' as the 'Device type' in the window that appears and mount the 'virtio-win.iso' image file.In the 'NIC' section, change the 'Device model' to 'virtio'.DON'T remove the 'Tablet' - it's needed for sensible mouse behaviour. If you remove it, the mouse pointer is captured by the Windows 11 window (you have to press Ctrl+Alt) to release it), the proper mouse pointer is not visible unless you turn on mouse "trail", and there is another mouse pointer that isn't aligned with the mouse position.Click the 'Add Hardware' button and select Channel. Then, from the drop-down list, select 'org.qemu.guest_agent.0' and click 'Finish' to apply.Click the 'Add Hardware' button, select TPM, check that version is 2.0, and click 'Finish' to apply.n the graphical console, follow the instructions for installing and setting up Windows. Some specific points:- When asked for a product key, click "I don't have a product key" at the bottom of the window.
- Select "Windows 11 Home" as the Windows version - this seems to be the minimal bloat version.
- Select "Custom" as the type of installation
- Set the keyboard layout to "United Kingdom Extended".
Select the 'Add Hardware' button at the bottom left of the 'hardware details' page, and set up the following:- Select "USB Host Device" and select required USB device.
- Select "Graphics", set "Type" to "VNC server" and "Address" to "All interfaces". Set port to next available ('Auto' may change dynamically).