DevOps Box: Vagrant with KVM

Configure Vagrant with KVM (Ubuntu)

Joaquín Menchaca (智裕)
4 min readOct 19, 2020

--

When developing for cloud native solutions, I occasionally need to setup clean isolated environments using virtualization for testing or modeling solutions. One of the tools for my development system that is used to manage virtual machines is the popular Vagrant tool from Hashicorp.

Vagrant by default supports Virtualbox, but what if I want to use the native Linux virtualization solution with KVM? Well, you can with the vagrant-libvirt plug-in.

This article will cover how to install and setup these components on Ubuntu 20.04.1 LTS (Focal Fossa).

Installation Steps

Prerequisite Tools

These are instructions use GNU bash that comes standard on most distros. Additionally, these commands need to be installed to for this running the scripts in this tutorial.

sudo apt install curl xml2

Installing KVM and libvirt

You can get libvirt and KVM by installing these tools:

sudo apt-get install -y \
bridge-utils \
cpu-checker \
libguestfs-tools \
libvirt-clients \
libvirt-daemon-system
libvirt-dev \
qemu-kvm \
virt-manager

Installing Vagrant

You can query the latest Vagrant version and then download and install the package with this small script:

Installing vagrant-libvirt Plugin

By default, Ubuntu 20.04.1 comments out deb-src required by build-dep. Thus we’ll need to uncomment these entries to enable this process.

sudo cp /etc/apt/sources.list /etc/apt/sources.list~
sudo sed -Ei 's/^# deb-src /deb-src /' /etc/apt/sources.list
sudo apt-get update

Afterward, we can install required packages for vagrant-libvirt.

sudo apt-get build-dep vagrant ruby-libvirt
sudo apt-get install \
dnsmasq-base \
lebtables \
libvirt-dev \
libxml2-dev \
libxslt-dev \
qemu \
ruby-dev \
zlib1g-dev

Now we can install the plug-in:

vagrant plugin install vagrant-libvirt

Testing Vagrant Box

First let’s download a Vagrant box that supports libvirt.

vagrant box add generic/ubuntu2004 --provider libvirt

Create a small configuration file to use use this new Vagrant box:

cat <<-VAGRANTFILE > Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2004"
end
VAGRANTFILE

And now bring up the system (< 20 seconds):

vagrant up --provider libvirt

You can no log onto the virtual machine guest with:

vagrant ssh

System Info

Let’s install some stuff to see specs of the virtual system. Some of this will vary because it is dependent on the host hardware, but other devices can be emulated, such as network hardware.

sudo apt install -y inxi neofetch
inxi --system --machine --cpu --network --disk --info
neofetch

For inxi, we can get this information:

And for neofetch we get this

Vagrant Features

Here are some options to add local file sharing as well as virtual network, which is useful if you want to network several virtual machines together.

Setup Synced Folders

You can add support to grant access to the current directory, which can be mounted as /vagrant on the virtual machine guest.

Update the Vagrantfile configuration to have the following:

When you are ready you can run the command below. This will install NFS client on the guest system, and update /etc/exports on the host system (so you will be prompted for password). For this to work, you should have nfs-kernel-server installed and running to support NFS (see Setting Up NFS HowTo).

vagrant reload

Afterward, you can access the mounted directory /vagrant on the virtual guest system to write to the current working directory on the host.

Setup Private Networks

With Vagrant and libvirt provider, you can also add private networks that are fully routable from localhost (127.0.0.1):

You can now access 192.168.123.29 directly. Try this for ssh:

ssh vagrant@192.168.123.29 \
-i .vagrant/machines/default/libvirt/private_key

Clean-up

When finished with the virtual guest, you can destroy the virtual machine and associated configuration (/etc/exports entries and virtual IP addresses) with a single command:

vagrant destroy

Resources

Here are some articles I came across in my research or by complete accident.

Tool Homepages

Installation

Vagrant Libvirt Boxen

Here are places where you can get a Vagrant box image that has support for the libvirt provider:

Networking Articles

Concluding Thoughts

The great thing about this setup is that libvirt with KVM is incredibly fast with about 20 seconds to bring up a system in contrast to Virtualbox, which can be over a minute to bring up a single system. Though, this comes at a cost as there is greater complexity with KVM and libvirt, while Virtualbox is easier to use and enjoys greater support.

I especially wanted to cover these tools in this small tutorial as documentation from web searches around using the latest Vagrant with vagrant-libvirt plug-in often yield inconsistent and often outdated information.

I hope this was useful exposure to Hashicorp Vagrant tool with the libvirt provider for automation in your local development setup.

--

--

Joaquín Menchaca (智裕)

DevOps/SRE/PlatformEng — k8s, o11y, vault, terraform, ansible