In this guide, we’ll look at how to use Vagrant with Libvirt on Linux. Vagrant is an open-source software written in Ruby to help you build and maintain a portable virtual software development environments, e.g. for VirtualBox, Hyper-V, Docker containers, VMware, Libvirt, and AWS. Vagrant makes it easy to create, stop and destroy Virtual Machines.
The most Vagrant use case on Local machines is the use of VirtualBox provider. If you are a KVM and QEMU user, this article is for you. KVM has been known to have a better performance and small resource utilization as compared to Virtualbox.
Vagrant with Libvirt on Linux Prerequisites:
- Installed Vagrant
- Installed Libvirt and QEMU-KVM
- Installing libvirt plugin for Vagrant
For installation of KVM on Linux, we have some articles already baked for you.
At least mostly used, Linux Distributions have been covered. For installation of Vagrant, please consult your Vagrant official documentation.
Installing Vagrant plugin for Vagrant
Once you have Vagrant and KVM installed, you should be ready to install a libvirt plugin so that you can start managing KVM Virtual machines using Vagrant.
$ vagrant plugin install vagrant-libvirt Installing the 'vagrant-libvirt' plugin. This can take a few minutes... Building native extensions. This could take a while... Building native extensions. This could take a while... Installed the plugin 'vagrant-libvirt (0.0.43)'!
Once the installation is complete, you can confirm that the plugin has been installed using the following command:
$ vagrant plugin list vagrant-libvirt (0.0.43)
Downloading Vagrant boxes
A Vagrant box for Libvirt is a tar archive with 3 files in it.
- A base VagrantFile
- Th metadata.json file
- QCOW2 image
If you are interested in building your own Vagrant boxes, have a look at using Packer and Packer build templates for Vagrant on chef/bento github repo. It will help you to easily get started. In this example, we’re going to use a ready template. Let add CentOS 7 and CentOS 6 boxes.
$ vagrant box add centos/7 --provider=libvirt ==> box: Loading metadata for box 'centos/7' box: URL: https://vagrantcloud.com/centos/7 ==> box: Adding box 'centos/7' (v1803.01) for provider: libvirt $ vagrant box add centos/6 --provider=libvirt
Check the list of boxes presents locally.
$ vagrant box list centos/6 (libvirt, 1803.01) centos/7 (libvirt, 1803.01) generic/ubuntu1604 (libvirt, 1.5.0)
Create VM Vagrantfile
Vagrant needs a configuration file to get the details and settings for a VM to be created. Let’s create a single VM Vagrantfile.
$ mkdir ~/vagrant-vms $ cd ~/vagrant-vms
Create a Vagrantfile with content similar to below:
To bring the VM up, run:
Run virsh list to see if you’ll get a list of VMs.
As defined in Vagrantfile, a new bridge will be created with a .1 IP Address from subnet defined, default mask is /24.
You can as well see the bridge using the brctl command:
To ssh to the VM, use vagrant ssh command.
To output .ssh/config valid syntax for connecting to this environment via ssh, run ssh-config command. You’ll need to place provided output under ~/.ssh/config directory to ssh.
Then use ssh command to log in with name configured above:
$ ssh rabbitmq-server-01 Last login: Sat May 12 09:20:05 2018 from 192.168.121.1 [vagrant@rabbitmq-server-01 ~]$
To shut down the VM, run:
$ vagrant halt ==> rabbitmq-centos6: Halting domain... $ virsh list --all ---------------------------------------------------- - centos_mysql-server-01 shut off - rabbitmq-server_rabbitmq-centos6 shut off - ubuntu-terraform shut off
To set VM to its initial state by cleaning all data, use vagrant destroy:
Build your own Vagrant box
You need packer installed for this to work.
$ wget https://releases.hashicorp.com/packer/1.2.3/packer_1.2.3_linux_amd64.zip $ unzip packer_1.2.3_linux_amd64.zip Archive: packer_1.2.3_linux_amd64.zip inflating: packer $ sudo cp packer /usr/local/bin $ type packer packer is /usr/local/bin/packer
Then clone bento Github repo.
$ cd ~/ $ git clone https://github.com/chef/bento $ cd bento $ cd centos $ packer build -only qemu -var "headless=true" centos-7.5-x86_64.json ==> qemu: Gracefully halting virtual machine... ==> qemu: Converting hard drive... ==> qemu: Running post-processor: vagrant ==> qemu (vagrant): Creating Vagrant box for 'libvirt' provider qemu (vagrant): Copying from artifact: ../builds/packer-centos-7.5-x86_64-qemu/centos-7.5-x86_64 qemu (vagrant): Compressing: Vagrantfile qemu (vagrant): Compressing: box.img qemu (vagrant): Compressing: metadata.json Build 'qemu' finished. ==> Builds finished. The artifacts of successful builds are: --> qemu: 'libvirt' provider box: ../builds/centos-7.5.libvirt.box
If the build is successful, ready to import box files will be in the builds directory at the root of the repository.
$ vagrant box add builds/centos-7.5.libvirt.box --name "centos-7.5" ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'centos-7.5' (v0) for provider: box: Unpacking necessary files from: file:///home/jmutai/hacks/vagrant/labs/packer/bento/builds/centos-7.5.libvirt.box ==> box: Successfully added box 'centos-7.5' (v0) for 'libvirt'!
Verify the box is installed
$ vagrant box list centos-7.5 (libvirt, 0) centos/6 (libvirt, 1803.01) centos/7 (libvirt, 1803.01)