Intro

This post will cover the process of installing Vagrant with the libvirt provider on CentOS 7.

For reference the following software will be used in this post.

  • CentOS - 7
  • Vagrant - 2.2.4
  • Vagrant-libvirt - 0.0.45

System Prep

Before we begin, lets ensure the host is updated.

cmd
sudo yum update -y && sudo yum upgrade -y

On this host I will set the SELinux policy to permissive.

cmd
sudo setenforce permissive
sudo sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config

Installation

Install Vagrant

cmd
sudo yum install -y https://releases.hashicorp.com/vagrant/2.2.4/vagrant_2.2.4_x86_64.rpm

Install the vagrant-libvirt dependencies

cmd
sudo yum install -y qemu libvirt libvirt-devel ruby-devel gcc qemu-kvm rsync

Install the vagrant-libvirt plugin.

cmd
vagrant plugin install vagrant-libvirt

Configuration

Start and enable the libvirtd service.

cmd
sudo systemctl start libvirtd.service
sudo systemctl enable libvirtd.service

Add your user to the libvirt group.

cmd
sudo usermod -aG libvirt $USER
Important
Re-login to have the group applied to your user.

Set an environment variable for the default libvirt uri as qemu:///system . Without this virsh commands point to the users qemu:///session uri which does not have privileges to add/view system wide networks.

cmd
sudo tee --append /etc/environment > /dev/null << "EOF"
LIBVIRT_DEFAULT_URI=qemu:///system
EOF

Source the /etc/environment file to load the config into your environment.

cmd
source /etc/environment

Create a vagrant-libvirt network using an xml file named vagrant-libvirt.xml with the below contents.

file
<network connections='1'>
  <name>vagrant-libvirt</name>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr1' stp='on' delay='0'/>
  <ip address='192.168.121.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.121.2' end='192.168.121.254'/>
    </dhcp>
  </ip>
</network>

Define and start the network.

cmd
virsh net-define vagrant-libvirt.xml
virsh net-start vagrant-libvirt
virsh net-autostart vagrant-libvirt

Testing

Confirm you can build a Vagrant box.

cmd
mkdir -p ~/vagrant/centos-test && cd ~/vagrant/centos-test
vagrant init centos/7 && vagrant up

# output

Bringing machine 'default' up with 'libvirt' provider...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Creating image (snapshot of base box volume).
==> default: Creating domain with the following settings...
==> default:  -- Name:              centos-test_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/centos-test_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: Configuring and enabling network interfaces...
    default: SSH address: 192.168.121.38:22
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Rsyncing folder: /home/bradmin/vagrant/centos-test/ => /vagrant

# SUCCESS !!!!

Outro

If you followed along, Vagrant and the vagrant-libvirt plugin are now installed in your CentOS 7 host and you can successfully build Vagrant boxes.