DevOps Box: Vagrant with KVM

Configure Vagrant with KVM (Ubuntu)

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.

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
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
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
cat <<-VAGRANTFILE > Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2004"
end
VAGRANTFILE
vagrant up --provider libvirt
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

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.

vagrant reload
ssh vagrant@192.168.123.29 \
-i .vagrant/machines/default/libvirt/private_key
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.

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, o11y, k8s, progressive deployment (ci/cd), cloud native infra, infra as code