Using libvirt/KVM/QEMU to create VMs

Part of the Kubernetes the hard way on bare metal/VM. This is designed for beginners.

Drew Viles
8 min readDec 14, 2021
Libvirt logo

Introduction

This guide is part of the Kubernetes the hard way on bare metal/VMs series. On its own this may be useful to you however since it’s tailored for the series, it may not be completely suited to your needs.

libvirt/KVM/QEMU is a popular collection of packages that allow you to run virtual computers (Virtual Machines — VM — guests) on a real one (Host PC). This means you can turn one PC or server into many. Many companies and open source projects provide VM services from VMWare to Virtualbox.

This guide presumes you’re using a physical machine with Debian based linux since it uses apt. It will work on other versions too but will need adjustments.

Preparation — Bridge utils

For this particular setup you’ll be using a bridged network adapter which will allow your VMs to be seen as real machines on the network.

Install bridge-utils:

sudo apt install bridge-utils -y

Once installed you need to edit your /etc/network/interfaces file so it looks something akin the following: (replace eno1 with your NIC id)

Note that this is a very basic bridge configuration!

sudo vim /etc/network/interfacesauto lo eno1 br0
iface lo inet loopback
iface eno1 inet manualiface br0 inet dhcp #set this to static if required
bridge_ports eno1

You can reload the network interfaces/services if you like but the easiest way to apply everything is to just reboot.

Once the machine comes back up you should see that br0 has an ip address when you run ip a show br0 | grep -Po 'inet \K[\d.]+'

192.168.0.2

Installing libvirtd & virt-manager

Now you’ll be installing libvirtd — this is required to run VMs. Virt-manager is used to create and manage the VMs via a GUI. Virt-manager is useful because not only can you create them locally, you can connect to remote servers running libvirtd and create them on there too.

You can create VMs via the command line using virsh however this is a little more advanced than I’d like to get into in this tutorial.

sudo apt install libvirt-daemon virt-manager -y

Once it’s installed, enable and start the service:

sudo systemctl enable libvirtd
sudo systemctl start libvirtd

Now you can start creating VMs. Start virt-manager up and move onto the next step.

Create VMs — Virt-manager

You’ll be presented with a screen like the one shown below. You may need to connect to the QEMU/KVM daemon that is listed. Right click on it and click Connect. All that will change is that it will no longer say Not connected.

Connect to the local daemon or a remote machine

I recommend you setup the location your ISOs will be loaded from. To do this right click on QEMU/KVM then click Details. In the new window that pops up, click the Storage tab.

List of storage locations on the machine you’re connected to

Click on the + icon in the bottom left of the window to add a new storage location. Enter a name and the location of your ISOs and click finish.

Add any other storage such as ‘other’ disk storage locations and you should see them listed as shown below. If you don’t have any ISOs yet then you can acquire these from various places such as from Microsoft for Windows, Apple for MacOS* and many places for Linux such as Debian, Ubuntu, CentOS, Fedora, Elementary OS and many, many more.

*Running MacOS in VMs is sketchy at best and requires some work converting the disk into Apple supported formats — HFS+.

New storage locations

Close the details window to go back.

Create a VM

Right click on QEMU//KVM then click on New.
Select Local Install Media and click Forward.
Then click Use ISO Image -> Browse and select your ISO from the storage location you created, uncheck auto detect OS based on install media and then select your OS type and version. If your version isn’t listed, select the closest match. For example I’m installing Ubuntu 20.04 however the highest available is this example is Ubuntu 17.10.
After that you’ll be prompted to set the CPU and Memory allocation; do this then click Forward.

Initial configuration

Now you’ll be prompted to create a new disk.

Click Select or create custom storage then Manage.
Select the location you want to store disk drives in, usually default or a custom path you added when adding the ISO storage location, then click the + icon near the top of the window next to Volumes.
Next create a name for the disk and set the format to qcow2, set the Max capacity and click Finish.

Create the new disk

Once you’ve finished that, the disk will show up in the Volumes list. Select it, click on Choose Volume and then click Forward.

Select the new disk

Note: You can set the size to whatever you want really and it isn’t technically limited to the size of your disk. The dynamic allocation only uses what the guest OS needs and is what allows you to set a 2TB disk size for the VM on, for example, a 500GB physical disk. In reality you’d only be able to use up to the size of the disk you have, 500GB in this example, but it means you can move the VM to a larger disk later and that will allow your 2TB virtual disk to continue expanding. Obviously in a real-world scenario you would not do this as over allocating resources can lead to a system lock up but it’s definitely possible for the sake of testing. This also applies for CPU and memory allocation, just don’t over do it.

Finally name the VM, check the Customise configuration before install box, ensure you’re network selection is on br0 and click Finish.

Name the VM & set Customize configuration before install

Customise the VM

In Overview you can name and describe the VM. You can also adjust the BIOS and chipset if you wish but generally speaking this is left as is.

Overview

Then click CPUs and check the Copy host CPU configuration box.

CPU config

Boot Options allows you to adjust whether the VM starts when the host boots and allows you to change the boot order.

Boot options

In the Disk options expand Advanced options and Performance options. Ensure the following is set.
Disk bus: VirtIO
Storage format: qcow2
Cache mode: none
IO mode: native

Disk config

Finally ensure that in Network the source is br0 and the Device model is virtio. Others can be used but for the sake of this tutorial, we’re using this.

Network config

Now click begin and you’re done!

Installing the OS

You’ll now see this screen and you can get started on installing the OS.

OS Installation starts

I won’t be taking you through the process of installing the OS here as this is a whole tutorial of its own. See the following guide for steps on how to install Ubuntu. (You’ll need additional disks for this Kubernetes series — see Extra Credit below)

Adding disks

In this section you will look at how we can add additional disks. The same can be done for other virtual hardware.

IMAGE REQUIRED

If it’s not already open as shown in the image above, open the VM by double clicking on it. Then click on the info icon (I) next to the play button in the top bar. You should see the window you used to adjust configurations before clicking Begin installation.

At the bottom left of this window is a Add Hardware button, click it. You’ll see the window below.

Add new hardware

I won’t go into too much detail here but you can see how each section is similar to the setting up or configuring of a VM in the info window. For example, to add new disks just click Storage -> Select or create custom storage -> Manage and follow through the steps you did to create the original disk for the VM.

It’s really quite straightforward in virt-manager as adding new hardware generally works in the same way as configuring existing hardware. If you’re not sure what you need but you have an existing disk to compare it to, just click finish and then you’ll be back on the configuration screen

Conclusion

You have been through the basics of installing and using libvirt, KVM/QEMU and virt-manager today.

What you’ve learned:

  • How to setup bridge networking
  • How to install libvirtd & virt-manager.
  • How to create a new VM via virt-manager
  • How to adjust some settings of the new VM.

Go make yourself a cup of tea and relax, you’re done!

Next: Back to tutorial

--

--