By default, Vagrant uses Oracle VirtualBox as provider. If more than one providers are installed (E.g. Virtualbox and Libvirt) in your system, it will always start a VM with Virtualbox unless you explicitly provide a specific provider. In this brief guide, let us see how to use Vagrant with Libvirt KVM provider on Linux.
Vagrant is not a stand-alone virtualization provider. It relies on other virtualization providers such as Virtualbox, Libvirt/KVM, Docker, VMWare to create and run virtual machines.
One of the great features of Vagrant is that users are never tied to a specific virtualization platform. The users can create workflows that work with multiple virtualization providers.
Currently, Vagrant supports more than 30 providers. You can view the complete list of supported providers here.
1. Install KVM on Linux
First, you need to install KVM on your Linux system. We already have documented KVM installation steps for DEB-based systems and RPM-based systems. Just follow the links to install KVM on your preferred Linux distribution.
- Install And Configure KVM In CentOS 8 Server
- Install And Configure KVM In Ubuntu 20.04 Headless Server
- Install And Configure KVM In OpenSUSE Tumbleweed
2. Install vagrant-libvirt plugin in Linux
In order to run Vagrant virtual machines on KVM, you need to install the
vagrant-libvirt plugin. This plugin adds the Libvirt provider to Vagrant and allows Vagrant to control and provision machines via Libvirt.
Install the necessary dependencies for
$ sudo apt install qemu libvirt-daemon-system libvirt-clients libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev ruby-libvirt ebtables dnsmasq-base
On CentOS, Fedora:
$ sudo dnf install gcc libvirt libvirt-devel libxml2-devel make ruby-devel
vagrant-libvirt plugin using command:
$ vagrant plugin install vagrant-libvirt
You also need to install
vagrant-mutate plugin which converts vagrant boxes to work with different providers.
$ vagrant plugin install vagrant-mutate
3. Use Vagrant With Libvirt KVM Provider
3.1. Make sure the Vagrant box that you want to use supports the libvirt provider. To discover vagrant boxes supported by libvirt, just select the "libvirt" option in Vagrant Cloud repository.
For the purpose of this guide, I am going to use a CentOS 7 box.
3.2. Go to your Vagrant project directory and initialize the Vagrant environment:
$ vagrant init centos/7
3.3. Next, run the following command to start the virtual machine:
$ vagrant up --provider=libvirt
--provider=libvirt option explicitly tells Vagrant to use libvirt KVM to run the virtual machine. Meaning - KVM acts as default provider here.
Bringing machine 'default' up with 'libvirt' provider... ==> default: Box 'centos/7' could not be found. Attempting to find and install... default: Box Provider: libvirt default: Box Version: >= 0 ==> default: Loading metadata for box 'centos/7' default: URL: https://vagrantcloud.com/centos/7 ==> default: Adding box 'centos/7' (v2004.01) for provider: libvirt default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/libvirt.box Download redirected to host: cloud.centos.org default: Calculating and comparing box checksum... ==> default: Successfully added box 'centos/7' (v2004.01) for 'libvirt'! ==> default: Uploading base box image as volume into Libvirt storage... ==> default: Creating image (snapshot of base box volume). ==> default: Creating domain with the following settings... ==> default: -- Name: myvagrants_default ==> default: -- Domain type: kvm ==> default: -- Cpus: 1 ==> default: -- Feature: acpi ==> default: -- Feature: apic ==> default: -- Feature: pae ==> default: -- Memory: 512M ==> default: -- Management MAC: ==> default: -- Loader: ==> default: -- Nvram: ==> default: -- Base box: centos/7 ==> default: -- Storage pool: default ==> default: -- Image: /var/lib/libvirt/images/myvagrants_default.img (41G) ==> default: -- Volume Cache: default ==> default: -- Kernel: ==> default: -- Initrd: ==> default: -- Graphics Type: vnc ==> default: -- Graphics Port: -1 ==> default: -- Graphics IP: 127.0.0.1 ==> default: -- Graphics Password: Not defined ==> default: -- Video Type: cirrus ==> default: -- Video VRAM: 9216 ==> default: -- Sound Type: ==> default: -- Keymap: en-us ==> default: -- TPM Path: ==> default: -- INPUT: type=mouse, bus=ps2 ==> default: Creating shared folders metadata... ==> default: Starting domain. ==> default: Waiting for domain to get an IP address... ==> default: Waiting for SSH to become available... default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Rsyncing folder: /home/sk/myvagrants/ => /vagrant
As you can see in first line of the above output, Vagrant uses "libvirt" provider to launch the CentOS 7 VM.
3.4. Alternatively you can tell Vagrant to permanently use libvirt as default provider by adding the following environment variable.
4. Verify if VM is running in Libvirt KVM
4.1. You can verify if the CentOS 7 VM is really running in Libvirt KVM provider from Virsh command line interface.
$ virsh list
Id Name State ------------------------------------ 2 myvagrants_default running
vagrant status command:
$ vagrant status
4.2. You can also verify it from a KVM management GUI applications like Virt-manager.
If another Vagrant box is already using a different provider (e.g. virtualbox), you will see the following error message when you try to start the new VM:
$ vagrant up --provider libvirt An active machine was found with a different provider. Vagrant currently allows each machine to be brought up with only a single provider at a time. A future version will remove this limitation. Until then, please destroy the existing machine to up with a new provider. Machine name: default Active provider: virtualbox Requested provider: libvirt
As stated in the above output, Vagrant can able to run a VM with only a single provider at a time. This limitation might be solved in the future version.
Before running another VM with different provider, first delete the existing active machine using command:
$ vagrant destroy
Type "y" and hit ENTER to delete the default active machine:
default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Destroying VM and associated drives...
Now start the VM with new provider i.e. KVM in this case:
$ vagrant up --provider libvirt
In this guide, we learned how to use Vagrant with libvirt KVM provider in Linux. We also looked at how to verify if the virtual machine is running in Libvirt KVM.