KVM machines for Vagrant on Archlinux

Vagrant supports different providers to manage virtualization. In a Linux environment, you can dramatically improve VM performance by using the libvirt provider and the KVM hypervisor.

This tutorial explains how to configure an Archlinux environment to be able to use libvirt as a Vagrant provider. The installation will be validated with a basic Vagrantfile. If you are using another Linux distribution than Archlinux, you can probably follow the same procedure.

libvirt is a library used to manage virtual machines. It can work with many hypervisors including VirtualBox, VMWare Workstation, QEMU… It bridges the kernel, which is responsible for resource allocation (CPU, RAM, network…), with the virtual machine hypervisor and provides an API to control VM states (start, stop…). One of Vagrant strengths when using libvirt is that it starts the machines in parallel, thus speeding up the starting process.

QEMU is an emulation and virtualization tool. It can work alone or use advanced optimization brought by KVM. KVM (Kernel-based Virtual Machine) is a Linux kernel module that leverages hardware virtualization, i.e. special CPU features made specifically for virtualization (for example Intel VT).

KVM vs Virtualbox

To get an idea of how much faster is Vagrant with libvirt and KVM, let’s compare its performances with VirtualBox at boot time. We created a cluster of 5 VMs using both QEMU/KVM and VirtualBox  (link to the  Vagrantfile). Here are the results:

Provider VirtualBox QEMU/KVM
vagrant up time 3 min 42 sec

The VMs are also faster at runtime thanks to KVM.

Prerequisites

Vagrant has to be installed and working on your environment. The installation guide is available on the official documentation. It is recommanded to not use a package manager to install Vagrant (the package is not up to date).

Installation of required packages

libvirt needs several packages to work with Vagrant. Use your package manager (e.g. pacman) to install the following packages:

  • ebtables is used to setup firewall rules for Ethernet frames.
  • dnsmasq is a DNS and DHCP server needed by Vagrant when using libvirt.
  • bridge-utils is used to create and manage bridged connections (bridges enable several machines to be linked in the same global network).
  • virt-manager will allow us to manage the VMs through a graphical interface.

Starting the libvirt service

Vagrant plugins installation

The plugin vagrant-mutate enables Vagrant to convert boxes from one hypervisor to another (eg. from a Virtualbox box to a QEMU box) and vagrant-libvirt enables Vagrant to use libvirt manager.

Creating a storage pool

A storage pool defines the quantity of storage that will be used for the VMs volumes. libvirt supports multiple storage pool types: directory pool, file system pool, NFS pool, etc. (more on libvirt’s website). Creating custom storage pools for your VMs allows you to isolate VMs and to use different storage types.

To test the installation, we will create a directory pool (it uses a directory of your file system) to host a test VM. You can follow the same procedure to create other storage pools.

Launch virt-manager  and perform the following actions:

  1. Add the connection QEMU/KVM :
    1. “Files”
    2. “Add a connection” :
      1. Hypervisor: “QEMU/KVM”
      2. “Automatic connection” enabled
      3. “Connect”
  2. Create the disk pool:
    1. Double-click on “QEMU/KVM”
    2. “Storage” tab
      1. “+ (Add a pool)” on the bottom-left
        • Step 1:
          • Name: “test-libvirt”
          • Type: “dir: File system repository”
        • Step 2: Default
        • “End”

Installing the Vagrant box libvirt

In our example, the test VM will run the CentOS7 system. First, you have to import the Vagrant box centos/7 :

Launching the test VM

Copy the Vagrantfile and launch the VM:

Try to connect via SSH to the VM:

If you do not succeed, it means that the installation failed… If this happens do not hesitate to comment on this post by describing your problem!

Conclusion

We have configured an alternative to the commonly used Virtualbox. As you can see, it is a bit complex, with several components having to work properly (QEMU, KVM, libvirt, vagrant plugins…). However, using KVM through QEMU should really boost your virtual machines performances. We also have quicker boot time thanks to the libvirt plugin doing parallel lauching. With that said, happy virtualization!


Known issues

Error:

Solution: Install the package  qemu-block-gluster and reboot your computer.

Error:

On vagrant-libvirt plugin install:

Solution: Install the plugin pkg-config:

By |2018-09-18T15:41:52+00:00September 19th, 2018|Categories: DevOps|Tags: , , , |0 Comments

About the Author:

Data Engineer & Big Data consultant at Adaltas.

Leave A Comment