Science and technology

How I take advantage of Vagrant with libvirt

I will admit it: I am a fan of Linux. While I’ve used Slackware on workstations and Red Hat Enterprise Linux (RHEL) on servers for years, I like seeing how different distributions do issues. What’s extra, I actually like to check purposes and scripts I write on different distributions to make sure portability. In truth, that is one of many nice benefits of Linux, as I see it: You can obtain a distro and check your software program on it totally free. You cannot try this with a closed OS, not less than not with out both breaking an EULA or paying to play, and even then, you are usually signing as much as obtain a number of gigabytes simply to check an software that is no quite a lot of hundred megabytes. But Linux is open supply, so there’s not often an excuse to disregard not less than the three or 4 major distros, besides that organising a digital machine can take plenty of clicks and typically advanced digital networking. At least, that was once the excuse till Vagrant modified the digital machine workflow for builders.

What is Vagrant

Vagrant is an easy digital machine supervisor on your terminal. It means that you can simply pull a minimal and pre-built digital machine from the Internet, run it regionally, and SSH into it in just some steps. It’s the quickest you may ever arrange a digital machine. It’s superb for internet builders needing a check internet server, programmers who want to check an software throughout distributions, and hobbyists who take pleasure in seeing how totally different distributions work.

Vagrant itself is comparatively minimal, too. It’s not a virtualization framework itself. It solely manages your digital machines (“boxes” in Vagrant terminology). It can use VirtualBox or, by means of a plug-in, the light-weight libvirt challenge as a backend.

What is libvirt

The libvirt challenge is a toolkit designed to handle virtualization, with help for KVM, QEMU, LXC, and extra. You would possibly consider it as a form of digital machine API, permitting builders to jot down pleasant purposes that make it simple for customers to orchestrate virtualization by means of libvirt. I take advantage of libvirt because the backend for Vagrant as a result of it is helpful throughout a number of purposes, together with virt-manager and GNOME Boxes.

Installing Vagrant

You can set up Vagrant from vagrantup.com/downloads. There are builds out there for Debian-based methods, CentOS-based methods, macOS, Windows, and extra.

For CentOS, Fedora, or related, you get an RPM bundle, which you’ll set up with dnf:

$ sudo dnf set up ./vagrant_X.Y.ZZ_x86_64.rpm

On Debian, Linux Mint, Elementary, and related, you get a DEB bundle, which you’ll set up with apt:

$ sudo apt set up ./vagrant_X.Y.ZZ_x86_64.deb

Installing libvirt and help packages

On Linux, your distribution might have already got libvirt put in, however to allow integration with Vagrant you want just a few different packages, too. Install these along with your bundle supervisor.

On Fedora, CentOS, and related:

$ sudo dnf set up gcc libvirt
libvirt-devel libxml2-devel
make ruby-devel libguestfs-tools

On Debian, Linux Mint, and related:

$ sudo apt set up build-dep vagrant ruby-libvirt
qemu libvirt-daemon-system libvirt-clients ebtables
dnsmasq-base libxslt-dev libxml2-dev libvirt-dev
zlib1g-dev ruby-dev libguestfs-tools

Depending in your distribution, you might have to begin the libvirt daemon:

$ sudo systemctl begin libvirtd

Installing the Vagrant-libvirt plugin

In Vagrant, libvirt is enabled by means of a plug-in. Vagrant makes it simple to put in a plug-in, so your first Vagrant command is one you may not often run once more:

$ vagrant plugin set up vagrant-libvirt

Now that the libvirt plug-in is put in, you can begin utilizing digital machines.

Setting up your Vagrant atmosphere

To begin with Vagrant, create a listing known as ~/Vagrant. This is the place your Vagrantfiles are saved.

$ mkdir ~/Vagrant

In this listing, create a subdirectory to symbolize a distro you wish to obtain. For occasion, assume you want a CentOS check field.

Create a CentOS listing, after which change to it:

$ mkdir ~/Vagrant/centos
$ cd ~/Vagrant/centos

Now you should discover a digital machine so you may convert the listing you have simply made right into a Vagrant atmosphere.

Finding a Vagrant digital machine

Broadly talking, Vagrant containers come from three totally different locations: Hashicorp (the maintainers of Vagrant), maintainers of distributions, and folks such as you and me. Some pictures are minimal, meant to function a base for personalisation. In distinction, others attempt to resolve a particular want (for example, you would possibly discover a LAMP stack picture prepared for internet improvement). You can discover pictures by searching or looking the principle hub for containers app.vagrantup.com/boxes/search.

For this instance, seek for “centos” and discover the entry named generic/centos8. Click on the picture for directions on tips on how to use the digital machine. The directions are available two varieties: 

  • The code you want for a Vagrantfile
  • The command you should use the field from a terminal

The latter is the extra simple technique:

$ vagrant init generic/centos8

The init subcommand creates a configuration file, known as a Vagrantfile, in your present listing, which transforms that listing right into a Vagrant atmosphere. At any time, you may view a listing of recognized Vagrant environments utilizing the global-status subcommand:

$ vagrant global-status
id       identify    supplier state   listing
-------------------------------------------
49c797f  default libvirt working /residence/tux/Vagrant/centos8

Starting a digital machine with Vagrant

Once you have run the init command, you can begin your digital machine with vagrant up:

$ vagrant up

This causes Vagrant to obtain the digital machine picture if it does not exist already regionally, arrange a digital community, and configure your field.

Entering a Vagrant digital machine 

Once your digital machine is up and working, you may log in to it with vagrant ssh:


You connect with the field working in your present Vagrant atmosphere. Once logged in, you may run all of the instructions native to that host. It’s a digital machine working its personal kernel, with emulated hardware and customary Linux software program.

Leaving a Vagrant digital machine

To depart your Vagrant digital machine, sign off of the host as you usually exit a Linux laptop:

field$ exit

Alternately, you may energy the digital machine down:

field$ sudo poweroff

You also can cease the machine from working utilizing the vagrant command:

field$ vagrant halt

Destroying a Vagrant digital machine

When completed with a Vagrant digital machine, you may destroy it:

$ vagrant destroy

Alternately, you may take away a digital machine utilizing the worldwide field subcommand:

$ vagrant field take away generic/centos8

Vagrant is straightforward

Vagrant makes digital machines trivial, disposable, and quick. When you want a check atmosphere or a faux server to ping or develop on, or a clear lab laptop for experimentation or monitoring, you may get one with Vagrant. Some folks suppose digital machines aren’t related now that containers have taken over servers, however digital machines have distinctive traits that make them helpful. They run their very own kernel, have a full and distinctive stack separate from the host machine, and use emulated hardware. When a digital machine is what you want, Vagrant could also be simply one of the simplest ways to get it.

Most Popular

To Top