02 January 2021

A New Year, A New Lab -- libvirt and kvm

For years I have done the bulk of my personal projects with either virtualbox or VMWare Professional (all of the SANS courses use VMWare). Recently, a member of my LUG mentioned they'd dropped virtualbox some time ago in favour of using kvm with libvirt and virt-manager...since I'd never used kvm (or libvirt/virt-manager), I thought that was the perfect opportunity to redo my home environment.

The New Setup


When I first started OSG, I had just bought a MacBook Pro. As of macOS 11, that laptop is no longer supported - and I can't use a system that doesn't get OS updates. My options were to either buy a Windows 10 Pro licence or install Linux on it. Ubuntu 20.04 LTS (Desktop version) it was!

I'd also grown a bit tired of having to shuffle VMs between laptops and realised there are some REALLY good ways to solve that particular problem. Thanks to Amazon and too much time on my hands, I now have two 10th generation i5 Intel NUCs. Because more memory is better, each one has 64GB of RAM and a 500GB NVMe. Both are running Ubuntu 20.04 LTS as well, and I've opted to use their wired network interfaces rather than the wireless.

I did choose to install openssh-server, and install system updates, just after each system booted but otherwise all I've done is install Simple Screen Recorder, Chromium browser and the Scottish Gaelic language pack on the laptop.

In THIS post I'm going to focus on getting a new VM up and running on the laptop; I'll have a post in a few days about setting up the NUCs.

Step One - Install ISO


Since I'm setting up new VMs and replacing everything, I need the latest Ubuntu LTS server ISO:


For the last year I've been on a CentOS kick, so I have dozens of CentOS 7 and 8 servers, but with the uncertainty that has been introduced in that project...alas, that's for some other time, preferably over several pints of Belhaven Best.

Because I'm going to use the "Virt Manager" GUI on the laptop to create and manage KVM VMs, I'm only downloading the ISO on the laptop.

Step Two - libvirt and Virtual Machine Manager


With the ISO for the VMs downloaded, it's time to move to the fun part. On the laptop I'm going to install libvirt-daemon, libvirt-clients (which provides command line tools for working with libvirt) and virt-manager. KVM is built into the Linux kernel, libvirt just offers a way to interact with it (and with other tech, it's not a one-trick pony!). Since this is an Ubuntu system, I'll do it with "apt":

sudo apt install libvirt-daemon libvirt-clients virt-manager

the command "sudo apt install libvirt-daemon libvirt-clients virt-manager" and the output of apt displaying all of the additional packages to be installed

Depending on Internet speeds, now is a good time to go put the kettle on...

Step Three - the first VM


Once everything finishes installing, I'm going to move my Ubuntu Server ISO from my Downloads directory to the libvirt images directory, just because it's a standardised location for disk images (and because I don't really need it hanging out in my Downloads directory). I moved mine with:

sudo mv ~/Downloads/ubuntu-20.04.1-live-server-amd64.iso /var/lib/libvirt/images/

When it all finishes you can run "Virtual Machine Manager" by going to "Show Applications" and searching for it, or (as I am going to do) from the command line via:

virt-manager &

From here on, I'll just refer to that as VMM. When VMM starts up, there isn't much there - there are no VMs to monitor, so your display should look a lot like the screenshot below.


Don't worry, we're about to take care of that empty VM list =)

If you click "File", there's an option for "New Virtual Machine". This will begin the new VM wizard (yes, you can do all of this from the command line with virt-install - but that's another post!). I chose the following options:

VMM Step 1:
  - Local install media (ISO image or CDROM)

VMM Step 2:
  - Browse
  - select "ubuntu-20.04.1-live-server-amd64.iso"
  - click "Choose Volume"

VMM Step 3:
  - Accept the defaults (4GB RAM, 2 cores)

VMM Step 4:
  - Accept the defaults (25 GB disk)

VMM Step 5:
  - Changed name to u20-0

Note that to progress from step to step, you click "Forward" rather than "Next" or "Continue".

After a popup saying "creating domain" and the VM being made, things get to be a bit more exciting! Notice that a new VM shows up in the VMM window, with a status of "Running", and the same sort of display you'd get with VirtualBox or VMWare shows the install screen for the VM:

the "virtual machine manager" main window showing the new "u20-0" VM and it's status of "running"

the Ubuntu 20.04 text-based installer running in the new VM

From this point on, setting up the VM is just like any other Linux installation. I like to keep my installs REALLY simple so I usually accept all of the defaults for my test VMs. For this one, the only options I changed were:

o don't use LVM
o user is "demo"
o server name is "u20-0"
o install openssh-server

Once the install is finished, I select "reboot" and wait for things to reboot...


Notice the second icon, the "i" below "Virtual Machine". That is an info tab that provides all the information about the VM we could want to know - data about the hypervisor being used, CPU/Memory/Disk/Network usage graphs, CPU/Memory allocations, all the info you'd expect to be able to find. That same data can be retrieved via the command line with tools like "virsh" (as I said earlier, that's another post =)).

At this point I have a new Ubuntu Server 20.04.1 LTS system up and running, just like I would have in VirtualBox or VMWare. By default it uses a NAT network and I'm *fine* with that on the laptop. On the NUCs I suspect I'll have a mix of NAT and bridged, with a mix of network ranges based on what the VMs will do.

Wrapping Up


On the whole, I don't find this to be any more difficult or onerous than using either VirtualBox or VMWare Workstation/Fusion - but let's be fair, these products are geared at totally different user groups. There is nothing stopping anyone from using KVM to run Windows and a few dev VMs on their Linux laptop but where it really shines is on a cluster of 64-core, 4TB RAM servers running a few hundred (or thousand) virtual machines. Considering AWS made the decision to move from Xen to KVM, maybe "a few thousand virtual machines" is missing a couple of zeroes on the end...

A New Year, A New Lab -- libvirt and kvm

For years I have done the bulk of my personal projects with either virtualbox or VMWare Professional (all of the SANS courses use VMWare). R...