Home DevOps How To Use Vagrant With Libvirt KVM Provider
How To Use Use Vagrant With Libvirt KVM Provider On Linux

How To Use Vagrant With Libvirt KVM Provider

By sk
1755 Views

Vagrant is not a stand-alone virtualization provider. It relies on other virtualization providers such as Virtualbox, 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. By default, Vagrant uses Oracle VirtualBox as provider. It will always start a VM with Virtualbox unless you explicitly provide a specific provider. In this brief guide, I will explain how to use Vagrant with Libvirt KVM provider on Linux.

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 vagrant-libvirt plugin

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 vagrant-libvirt plugin.

On Ubuntu:

$ 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

Now, install 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

Use Vagrant With Libvirt KVM Provider

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.

Discover Vagrant boxes supported by libvirt
Discover Vagrant boxes supported by libvirt

For the purpose of this guide, I am going to use a CentOS 7 box.

Go to your Vagrant project directory and initialize the Vagrant environment:

$ vagrant init centos/7

Next, run the following command to start the virtual machine:

$ vagrant up --provider=libvirt

Here, --provider=libvirt option explicitly tells Vagrant to use libvirt KVM to run the virtual machine. Meaning - KVM acts as default provider here.

Sample output:

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
Use Vagrant With Libvirt KVM Provider On Linux
Use Vagrant With Libvirt KVM Provider On Linux

As you can see in first line of the above output, Vagrant uses "libvirt" provider to launch the CentOS 7 VM.

Alternatively you can tell Vagrant to permanently use libvirt as default provider by adding the following environment variable.

export VAGRANT_DEFAULT_PROVIDER=libvirt

Verify if VM is running in Libvirt KVM

You can verify if the CentOS 7 VM is really running in Libvirt KVM provider from Virsh command line interface.

$ virsh list

Sample output:

 Id   Name                 State
------------------------------------
 2    myvagrants_default   running
Verify if VM is running in libvirt kvm using virsh
Verify if VM is running in libvirt kvm using virsh

You can also verify it from a KVM management GUI applications like Virt-manager.

Verify if VM is running in libvirt kvm using virt-manager
Verify if VM is running in libvirt kvm using virt-manager

Hope this helps.

Resource:

You May Also Like

2 comments

Chiwy September 8, 2020 - 2:16 pm

Nice article, but…
My virtual machine is running fine, but I don´t have Internet connection in it, do you know how can I fix this?

Reply
sk September 8, 2020 - 2:54 pm

By default, Vagrant VMs should be able to connect to internet via NAT. You don’t need to do any extra configuration. Try to enable bridged networking to connect to outside network. https://ostechnix.com/how-to-configure-networking-in-vagrant/

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More