KVM machines for Vagrant on Archlinux
Sep 19, 2018
- Categories
- DevOps & SRE
- Tags
- Arch Linux
- KVM
- Linux
- Virtualization
- VM
- Vagrant
Never miss our publications about Open Source, big data and distributed systems, low frequency of one email every two months.
Vagrant supports different providers to manage virtualization. In a Linux environment, you can dramatically improve VM performance by using the libvirt provider and the KVM hypervisor.
This tutorial explains how to configure an Archlinux environment to be able to use libvirt as a Vagrant provider. The installation will be validated with a basic Vagrantfile. If you are using another Linux distribution than Archlinux, you can probably follow the same procedure.
libvirt is a library used to manage virtual machines. It can work with many hypervisors including VirtualBox, VMWare Workstation, QEMU… It bridges the kernel, which is responsible for resource allocation (CPU, RAM, network…), with the virtual machine hypervisor and provides an API to control VM states (start, stop…). One of Vagrant strengths when using libvirt is that it starts the machines in parallel, thus speeding up the starting process.
QEMU is an emulation and virtualization tool. It can work alone or use advanced optimization brought by KVM. KVM (Kernel-based Virtual Machine) is a Linux kernel module that leverages hardware virtualization, i.e. special CPU features made specifically for virtualization (for example Intel VT).
KVM vs Virtualbox
To get an idea of how much faster is Vagrant with libvirt and KVM, let’s compare its performances with VirtualBox at boot time. We created a cluster of 5 VMs using both QEMU/KVM and VirtualBox (link to the Vagrantfile). Here are the results:
Provider | VirtualBox | QEMU/KVM |
---|---|---|
vagrant up time | 3 min | 42 sec |
The VMs are also faster at runtime thanks to KVM.
Prerequisites
Vagrant has to be installed and working on your environment. The installation guide is available on the official documentation. It is recommanded to not use a package manager to install Vagrant (the package is not up to date).
Installation of required packages
libvirt needs several packages to work with Vagrant. Use your package manager (e.g. pacman) to install the following packages:
sudo pacman -S qemu ebtables dnsmasq bridge-utils virt-manager libvirt
ebtables
is used to setup firewall rules for Ethernet frames.dnsmasq
is a DNS and DHCP server needed by Vagrant when using libvirt.bridge-utils
is used to create and manage bridged connections (bridges enable several machines to be linked in the same global network).virt-manager
will allow us to manage the VMs through a graphical interface.
Starting the libvirt service
sudo systemctl start libvirtd
sudo systemctl enable libvirtd
Vagrant plugins installation
vagrant plugin install vagrant-mutate
vagrant plugin install vagrant-libvirt
The plugin vagrant-mutate
enables Vagrant to convert boxes from one hypervisor to another (eg. from a Virtualbox box to a QEMU box) and vagrant-libvirt
enables Vagrant to use libvirt manager.
Creating a storage pool
A storage pool defines the quantity of storage that will be used for the VMs volumes. libvirt supports multiple storage pool types: directory pool, file system pool, NFS pool, etc. (more on libvirt’s website). Creating custom storage pools for your VMs allows you to isolate VMs and to use different storage types.
To test the installation, we will create a directory pool (it uses a directory of your file system) to host a test VM. You can follow the same procedure to create other storage pools.
Launch virt-manager
and perform the following actions:
- Add the connection QEMU/KVM:
- “Files”
- “Add a connection”:
- Hypervisor: “QEMU/KVM”
- “Automatic connection” enabled
- “Connect”
- Create the disk pool:
- Double-click on “QEMU/KVM”
- “Storage” tab
- ”+ (Add a pool)” on the bottom-left
- Step 1:
- Name: “test-libvirt”
- Type: “dir: File system repository”
- Step 2: Default
- “End”
- Step 1:
- ”+ (Add a pool)” on the bottom-left
Installing the Vagrant box libvirt
In our example, the test VM will run the CentOS7 system. First, you have to import the Vagrant box centos/7
:
vagrant box add centos/7 --provider libvirt
Launching the test VM
Copy the Vagrantfile and launch the VM:
git clone https://gist.github.com/9d2e81703701b8d2dbb3d8647d6b73e8.git libvirt-test
cd libvirt-test
vagrant up
Try to connect via SSH to the VM:
vagrant ssh test_machine
If you do not succeed, it means that the installation failed… If this happens do not hesitate to comment on this post by describing your problem!
Conclusion
We have configured an alternative to the commonly used Virtualbox. As you can see, it is a bit complex, with several components having to work properly (QEMU, KVM, libvirt, vagrant plugins…). However, using KVM through QEMU should really boost your virtual machines performances. We also have quicker boot time thanks to the libvirt plugin doing parallel lauching. With that said, happy virtualization!
Known issues
Error:
Call to virDomainCreateWithFlags failed: internal error: Failed to load module '/usr/lib/libvirt/storage-file/libvirt_storage_file_gluster.so': libgfapi.so.0: Can not open the shared object file: No file or folder of this type
Solution: Install the package qemu-block-gluster and reboot your computer.
Error:
On vagrant-libvirt
plugin install:
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:
ERROR: Failed to build gem native extension.
current directory: ~/.vagrant.d/gems/2.5.1/gems/nokogiri-1.8.3/ext/nokogiri
/usr/bin/ruby -r ./siteconf20180702-5091-8tys0j.rb extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using system libraries.
pkg-config could not be used to find libxml-2.0
Please install either pkg-config or the pkg-config gem per
gem install pkg-config -v "~> 1.1"
pkg-config could not be used to find libxslt
Please install either pkg-config or the pkg-config gem per
gem install pkg-config -v "~> 1.1"
pkg-config could not be used to find libexslt
Please install either pkg-config or the pkg-config gem per
gem install pkg-config -v "~> 1.1"
ERROR: cannot discover where libxml2 is located on your system. please make sure pkg-config is installed.
* extconf.rb failed *
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/bin/$(RUBY_BASE_NAME)
--help
--clean
--use-system-libraries=true
--with-zlib-dir
--without-zlib-dir
--with-zlib-include
--without-zlib-include=${zlib-dir}/include
--with-zlib-lib
--without-zlib-lib=${zlib-dir}/lib
--with-xml2-dir
--without-xml2-dir
--with-xml2-include
--without-xml2-include=${xml2-dir}/include
--with-xml2-lib
--without-xml2-lib=${xml2-dir}/lib
--with-libxml-2.0-config
--without-libxml-2.0-config
--with-pkg-config
--without-pkg-config
--with-xslt-dir
--without-xslt-dir
--with-xslt-include
--without-xslt-include=${xslt-dir}/include
--with-xslt-lib
--without-xslt-lib=${xslt-dir}/lib
--with-libxslt-config
--without-libxslt-config
--with-exslt-dir
--without-exslt-dir
--with-exslt-include
--without-exslt-include=${exslt-dir}/include
--with-exslt-lib
--without-exslt-lib=${exslt-dir}/lib
--with-libexslt-config
--without-libexslt-config
To see why this extension failed to compile, please check the mkmf.log which can be found here:
~/.vagrant.d/gems/2.5.1/extensions/x86_64-linux/2.5.0/nokogiri-1.8.3/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in ~/.vagrant.d/gems/2.5.1/gems/nokogiri-1.8.3 for inspection.
Results logged to ~/.vagrant.d/gems/2.5.1/extensions/x86_64-linux/2.5.0/nokogiri-1.8.3/gem_make.out
Solution: Install the plugin pkg-config:
vagrant plugin install pkg-config