Vagrant + libvirt on CentOS 7

Back to Listing

Hanover, MD, 21 August 2020


It is not obvious how to get Vagrant working with libvirt on CentOS 7. Here’s a guide:

tl;dr

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install libvirt-daemon-kvm libvirt-client vagrant gcc-c++ make libstdc++-devel libvirt-devel
sudo systemctl enable --now libvirtd
vagrant plugin install vagrant-libvirt

But why?

The built-in virtualization on CentOS (plus RHEL and Fedora) is based on KVM and uses libvirt to manage virtual machines.

Vagrant is a great tool for testing and prototyping systems. By default, it uses VirtualBox, but if you don’t want to install VirtualBox for some reason (for example: KVM performs better, you’re already using KVM on a system, or maybe you’re doing nested VMs), there is a libvirt plugin for Vagrant that will let you use it as a front-end for libvirt.

Install Vagrant and libvirt

Hashicorp recently announced repositories of their tools for Linux systems. Let’s start by enabling that.

[user@demo ~]$ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
Loaded plugins: fastestmirror
adding repo from: https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
grabbing file https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo to /etc/yum.repos.d/hashicorp.repo
repo saved to /etc/yum.repos.d/hashicorp.repo

Assuming the system doesn’t already have libvirt set up, we need to start by installing a couple of packages for libvirt. In addition, we need to install Vagrant and some development tools and headers to build the vagrant-libvirt plugin.

[user@demo ~]$ sudo yum -y install libvirt-daemon-kvm libvirt-client vagrant gcc-c++ make libstdc++-devel libvirt-devel

Once that is done, enable libvirtd to turn the virtualization system on.

[user@demo ~]$ sudo systemctl enable --now libvirtd

Enable non-privileged management

Users in the libvirt group are allowed to manage VMs without any additional authentication, so you’ll want to add your user account to that group.

[user@demo ~]$ sudo usermod -a -G libvirt $( id -un )

After that, you’ll need to either log back in as the user or use newgrp libvirt to activate the group membership.

Install the vagrant-libvirt plugin

[user@demo ~]$ vagrant plugin install vagrant-libvirt
Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Fetching excon-0.76.0.gem
Fetching formatador-0.2.5.gem
Fetching fog-core-2.2.0.gem
Fetching fog-json-1.2.0.gem
Fetching mini_portile2-2.4.0.gem
Fetching nokogiri-1.10.10.gem
Building native extensions. This could take a while...
Fetching fog-xml-0.1.3.gem
Fetching ruby-libvirt-0.7.1.gem
Building native extensions. This could take a while...
Fetching fog-libvirt-0.7.0.gem
Fetching vagrant-libvirt-0.1.2.gem
Installed the plugin 'vagrant-libvirt (0.1.2)'!

Build a VM

Vagrant uses the working directory name as the root of the name of any VMs it creates. Make a directory and cd into it.

[user@demo ~]$ mkdir test
[user@demo ~]$ cd test

Next, create a Vagrantfile that describes the VM you want to build. In this example, we’re creating a CentOS 8 VM using the official CentOS Vagrant box.

[user@demo test]$ vagrant init centos/8
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

As the comments above say, you can edit Vagrantfile to change settings, add additional VMs, etc., but for now let’s test with the defaults.

[user@demo test]$ vagrant up
Bringing machine 'default' up with 'libvirt' provider...

Note: If you already have VirtualBox installed, you may need to specify the libvirt provider with --provider libvirt.

You’ll see a lot of messages go by as the VM starts up. Once it is done, you should be able to login to the VM and poke around.

[user@demo test]$ vagrant ssh
[vagrant@localhost ~]$ cat /etc/redhat-release 
CentOS Linux release 8.0.1905 (Core) 

When you’re finished with the VM, use vagrant destroy to remove it.

[user@demo test]$ vagrant destroy -f
==> default: Removing domain...

Click here to learn more about how Onyx Point, Inc's professional services and development teams can help you.

Steven is a consultant and trainer for Onyx Point, focusing on Puppet, compliance automation, and all things DevOps.

At Onyx Point, our engineers focus on Security, System Administration, Automation, Dataflow, and DevOps consulting for government and commercial clients. We offer professional services for Puppet, RedHat, SIMP, NiFi, GitLab, and the other solutions in place that keep your systems running securely and efficiently. We offer Open Source Software support and Engineering and Consulting services through GSA IT Schedule 70. As Open Source contributors and advocates, we encourage the use of FOSS products in Government as part of an overarching IT Efficiencies plan to reduce ongoing IT expenditures attributed to software licensing. Our support and contributions to Open Source, are just one of our many guiding principles

  • Customer First.
  • Security in All We Do.
  • Pursue Innovation with Integrity.
  • Communicate Openly and Respectfully.
  • Offer Your Talents, and Appreciate the Talents of Others

vagrant, libvirt, kvm, CentOS, RHEL, technical

Share this story

We work with these Technologies + Partners

puppet
gitlab
simp
beaker
redhat
AFCEA
GitHub
FOSSFeb