KVM, QEMU, and libvirt
This post is taking a look at another suite of software to create and manage Virtual Machines. For a background on computer virtualization, check out a previous post.
Linux distributions support virtualization through the Linux kernel which is why the software is called KVM. This stands for “Kernel-based virtual machine”. It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko.
In order to leverage the kernel module, we will use another software called QEMU which is an open source machine emulator. QEMU emulates “hardware” such as a processor, memory, network interface, and storage. While qemu has a command line interface, it is mostly used by engineers who are developing the platform. Libvirt provides an abstraction from specific versions and hypervisors and encapsulates some workarounds and best practices.
These three softwares come together to form a relatively user-friendly way to create and manage virtual machines on Linux systems.
Installation
These softwares are packaged in most Linux distributions for an easy Installation. Below is a simple ansible playbook to install the software packages on Ubuntu and Red Hat distributions. If you are not familiar with ansible, check out a previous post to get started.
|
|
Playbook execution
|
|
Enter the user account you want to use to create virtual machines as an extra variable and adding the -K
will ask for a sudo password as this playbook installs new packages and requires superuser access.
Install with package manager
An alternative way to install qemu/kvm is to use a distribution package manager. For example on Ubuntu, install the packages and add your user to the libvirt group.
|
|
|
|
libvirt command line usage
Ensure that the libvirt service is running if you encounter any error messages:
|
|
CLI create vm
Use virt-install
to create a vm from the shell:
|
|
virsh commands
There are several utilities available to manage virtual machines and libvirt. The virsh utility can be used from the command line:
To list running virtual machines:
|
|
To start a virtual machine:
|
|
To restart a virtual machine:
|
|
GUI create vm
If you have a desktop environment installed, the virt-manager
utility provides a gui to create, manage, and connect to virtual machines.
Ubuntu virt-manager
If it was not installed with other libvirt tools, install:
|
|
Ensure your user is in the libvirt
group or run virt-manager
as sudo.
For more details and how to use this tool on other distributions, check out the official site.