QEMU, KVM and the other ones
Learn about virtualising the Linux way, with libvirt and Virtual Machine Manager, and jokes about Inception
Virtualbox and Vmware, with their friendly GUIS, are great tools for spinning up VMS on whatever OS you happen to be running at the time. They also both have command line interfaces, and Vmware Pro has all kinds of enterprise management features. So if you want to deal with more VMS than you can comfortably fit on your screen at once, these tools are more than fit for purpose.
Linux has its own virtualisation solution, KVM (the Kernel Virtual Machine) and it’s hugely popular, both at home and at scale. As the name suggests, KVM resides in the kernel, and transforms it into a hypervisor. But to make use of it, we need a userspace program to provision our abstract hardware and disk images and such. The most common way to do this is through
QEMU, which is an emulator, but also a virtualiser. You can run QEMU from the command line if you really want, but once you realise how much you need to type to get everything just right you’ll find yourself clamouring for something a little friendly. Enter Libvirt, which provides a high-level API for creating and managing VMS. It’s not in itself a friendly GUI, but such a thing does exist in the form of the imaginatively Virtual
Machine Manager.
Having four tiers in a virtualisation stack might seem like the kind of unnecessary overcomplication that Linux is so often accused of, but they all do different things and can interoperate with other tools. Libvirt, for example, can happily manage Virtualbox machines as well as QEMU/KVM ones. It can also set up virtual networks and bridges and things so your guests can talk to each other or the physical network as you wish.
We can install all the required bits and pieces for wrangling KVM VMS on desktop Ubuntu with a simple
$ sudo apt install virt-manager
Or if you want to run a headless Ubuntu Server VM host (for remote connections, see box, facing page) use the following bit of command-line prose:
$ sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst
To communicate with the libvirt daemon, and manipulate QEMU your user must be added to the appropriate groups (libvirt on most distros). Apt will have done this for you on desktop, but you’ll need to log out for these changes to take effect. There’s a bit of libvirt nomenclature, parlance and ritual with which it pays to be familiar before you start creating your very own horde of VMS.
Keeping things separate
When you run a VM (known as a domain) through libvirt, that’s all done through a separate user account. So it doesn’t matter if something goes wrong and your desktop crashes – once you’ve restored it you’ll find your guests exactly as you left them. We’ll look in a moment at connecting to VMS remotely, where connections may be fragile, and this resilience comes into its own. But first let’s get one running locally. Start Virtual Machine Manager (hereafter Virt
Manager), you’ll find it in the Applications Grid, and click the New VM button in the top left. If you see an error about libvirt not running you may need to restart first.
Choose Local Install Media and point it to the ISO file of your choosing. If you’re planning on using Geckolinux or Manjaro from the LXFDVD make sure and copy the appropriate ISO files to your local storage first, otherwise the whole process will be noisy, slow and frustrating. Virt Manager is good at guessing Linux distributions from the install medium, but occasionally misses. The dialog does insist you choose something, so just choose whichever’s closest to the OS you’re using, or failing that choose one of the generic options.
Next we allocate how much memory and physical CPU cores to assign to the machine. Everyone’s been through the ordeal of having to use a computer that’s too slow or short of memory. And having to use a VM that’s chewed up all its RAM and is constantly swapping to an emulated disk is even less fun. Most Linux distros will be fine with 2GB of RAM and two CPU cores, but more is better if you can spare it. Don’t overallocate resources though – you need to save some for the host machine, especially if it’s running your desktop.
A question of storage
Libvirt’s default storage location is /var/lib/libvirt/ images, but you can tell it to put its virtual storage anywhere. This is a good thing because you don’t want to go accidentally filling up your real root partition with virtual cruft. If your root partition is on a fast SSD though, and you have space then you’d be well advised to store your first QEMU volume in the default location, in which case select Create a disk image. Otherwise choose the custom storage option, add your directory as a Pool from the bottom left, select it and click the + in the Volumes section. If you’re using Btrfs make sure any directories you plan on storing VMS in are marked with the NOCOW attribute, for example:
$ sudo chattr +C /path/to/vms
Copy on write isn’t suitable for large, dynamic files. You can opt to use RAW files instead of the default QCOW format and it might be marginally faster. But QCOW files are more resilient to events like power failure, and are more efficient with regards to snapshotting. Either way, you can choose to allocate all the space now, or have the files dynamically expand as virtual space is used. Virtual distros grow just like real ones, and don’t even get us started on virtual Windows, so consider how much you’ll need, rather that the initial install size. 20GB works well for desktop distros (and Ubuntu won’t install on anything less than 15GB). Once that’s allocated, select your volume and move on to the final step: give the VM a name and choose a network.
The default NAT network enables your guests to communicate with the outside world, so that’ll do for now. At this point it’s easy to miss the tiny checkbox where it says “Customize configuration before install”, and for a first VM it doesn’t matter if you do. But if you want to make a UEFI booting virtual machine (which is required for GPU passthrough, which we’ll discuss later) this is the only opportunity you get to set this up. All the other options can be changed after install, and in some cases on the fly too. But cast your eye over them nonetheless (everything you need to get started is on by default) and then click Begin Installation. The machine should leap into life and you can install your distro of choice as if it were the real thing. We had Ubuntu installed updated and rebooted in exactly the time it took to make and drink a cup of tea.