Vagrant is a framework to quickly spin up and manage virtual machines and is very popular within development communities. While Vagrant uses Oracle VirtualBox as the hypervisor by default, we can install a libvirt plugin to use Vagrant with libvirt, benefiting from the sVirt security offered by SELinux.
Deploying Vagrant and the libvirt plugin
The Vagrant application can be installed from a single RPM file. Find the latest version at https://www.vagrantup.com/downloads.html and install it. For instance, for CentOS systems, you can use
# yum install https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.rpm
To install the libvirt plugin, we first need to make sure that the dependencies are installed as well. The documentation, online at https://github.com/vagrant-libvirt/vagrant-libvirt, gives a good overview of which packages need to be installed. Do not forget this step, as dependency failures during the plugin installation are not always obvious.
Once the dependencies are installed, use
vagrant itself to download and install the plugin:
# vagrant plugin install vagrant-libvirt
Installing a libvirt-compatible box
Vagrant uses boxes: images prepared for quick installation using Vagrant. Not all Vagrant boxes are compatible with the libvirt provider. Luckily, the Vagrant Cloud website at https://app.vagrantup.com/boxes/search?provider=libvirt allows you to quickly find compatible boxes.
Suppose we want to use a Fedora image called
fedora/32-cloud-base, then we can configure it as follows:
- Create a new directory, which we will define the box configuration in, and enter this location:
# mkdir vagrant # cd vagrant
- Initialize the Vagrant box, using the
# vagrant init fedora/32-cloud-base
This will create an empty
Vagrantfile that can be used to further configure the box.
- Edit the
Vagrantfile, and add the following code:
config.vm.provider :libvirt do |libvirt| libvirt.storage_pool_name = "images" libvirt.driver = "qemu" # or kvm end
This will configure the libvirt provider to use the
images directory as the default storage pool, and use the QEMU driver within libvirt.
- Still inside the
Vagrantfile, add the following code to give the box a proper name:
config.vm.define :test do |test| test.vm.box = "fedora/32-cloud-base" end
The name chosen here is
test, and will result in a virtual guest named
- To launch the test guest, run the
vagrant upcommand like so:
# vagrant up --provider=libvirt
Depending on the speed of the system, this step can take a while to complete.
Configuring Vagrant boxes
# virsh list --all Id Name State ----------------------------- 1 vagrant_test running
# virsh edit vagrant_test
As long as the Vagrant box is not destroyed, the settings in libvirt will persist.