26 C
Jaipur
Saturday, October 24, 2020

How To Use Vagrant With Libvirt KVM Provider

Must read

iPhone 12 Users Can Now Download OS Updates Over 5G Data

Apple finally introduced 5G support with the launch of its latest iPhone 12 series. Now, as iPhones support high-speed 5G networks, the Cupertino tech...

Check Out Oppo Find X2 League of Legends Special Edition Right Here

After much anticipation, Chinese phone maker, Oppo unveiled the second generation of its Find X series, the Oppo Find X2 and Find X2 Pro,...

apple: Apple fixes 6 malicious apps posing as Adobe Flash installers – Latest News

The malicious apps masquerading as Adobe Flash installer are not going to go away anytime soon as Apple has once again fixed six more...

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:       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:

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

iPhone 12 Users Can Now Download OS Updates Over 5G Data

Apple finally introduced 5G support with the launch of its latest iPhone 12 series. Now, as iPhones support high-speed 5G networks, the Cupertino tech...

Check Out Oppo Find X2 League of Legends Special Edition Right Here

After much anticipation, Chinese phone maker, Oppo unveiled the second generation of its Find X series, the Oppo Find X2 and Find X2 Pro,...

apple: Apple fixes 6 malicious apps posing as Adobe Flash installers – Latest News

The malicious apps masquerading as Adobe Flash installer are not going to go away anytime soon as Apple has once again fixed six more...

smart speaker: Global smart speaker market to reach 163 million units in 2021: Report – Latest News

Led by Amazon Alexa and Google Assistant powered devices, the global smart speaker market (including smart displays) is set to reach 163 million units...