11 C
Tuesday, January 19, 2021

How To Use Vagrant With Libvirt KVM Provider

Must read

Vagrant is not a stand-alone virtualization platform. It relies on other virtualization providers such as Virtualbox, KVM, Docker, VMWare to create and run virtual machines. Vagrant supports 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.

  1. Install KVM on CentOS

    This guide is tested on CentOS 8 minimal server. However, it should work on RHEL, and other RPM-based systems.

  2. Install KVM on Ubuntu

    This guide is tested on Ubuntu 20.04 LTS headless server. The same procedure should work on Debian and other DEB-based systems.

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

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:
==> 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: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    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.


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.


Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

Latest article