Managing and provisioning VMs

Touch and Go

© Lead Image © adchariya chanpipat,

© Lead Image © adchariya chanpipat,

Article from Issue 235/2020

With Vagrant, you can automate the creation and management of consistent virtual machines that work across platforms.

Automation is the key ingredient for efficiency in any system administration strategy. If your job involves spinning virtual machines (VMs) regularly, you should familiarize yourself with Vagrant [1], which helps you make consistent virtual environments available to your users with a few keystrokes. Vagrant provides a simple and easy to use command-line interface (CLI) that helps automate the creation, editing, running, and deletion of VMs. Moreover, it supports all major virtual platforms, such as VirtualBox and VMware, and plays nicely with all the well-known software configuration tools, such as Chef, Puppet, Ansible, Fabric, and more.

Since VM hypervisors like VirtualBox and VMware have their own CLIs that can be used to automate provisioning, why should you choose Vagrant? Vagrant offers consistency and interoperability. With Vagrant you can define your virtual environment in code and then use it to provision VMs on top of any hypervisor running on any operating system. Additionally, anytime you make changes to the virtual environment, instead of sharing the complete VM that can be worth several gigabytes, you can share a simple text file that can then be used to provision VMs with the changes.

A Rolling Start

While Vagrant can provision VMs with various virtualization software, by default it uses the free and open source VirtualBox.

To get started, the first thing you need to do is install Vagrant and VirtualBox. The Vagrant project recommends downloading the packages for Vagrant [2] and VirtualBox [3] directly from their respective websites, since package managers will probably have outdated versions.

Next, you'll need to create a Vagrant configuration file that defines all the VM's characteristics from a template. You can search for templates based on various operating systems and predefined purposes on the project's website [4]. For example, fire up a terminal and enter:

$ mkdir ~/demo
$ cd ~/demo
$ vagrant init centos/7

to create a VM based on the CentOS 7.6.181 release inside the ~/demo directory.

Under the ~/demo directory, this command creates a file called Vagrantfile, which is the main configuration file that defines all the VM's attributes. If you want to make changes to a VM, you'll need to edit this file. You'll need to be well-versed with the Vagrantfile's anatomy in order to modify or create one as per your needs.

For now, you can bring up the VM using the default settings, by typing:

$ vagrant up

This command will create the actual VM reading the configuration specified in the Vagrantfile. This tells Vagrant to create a new VirtualBox machine based on the base image specified in the Vagrantfile. It'll do so by copying the virtual hard disk files from the remote server (Figure 1).

Figure 1: The first time you bring up a VM, it'll take a lot longer, because it downloads the image from the Internet.

Once it's done, you'll have a fully featured CentOS 7 VM running headless in the background. If you get errors regarding missing guest additions, refer to the "Guest Additions Plugin" box to leverage Vagrant's extensive plugin infrastructure to solve the issue permanently.

Guest Additions Plugin

The VirtualBox guest additions is an essential component that is required to take full advantage of Vagrant. However, rolling them into every box and then making sure the boxes are running the latest version of the guest additions is a time consuming task and an unnecessary distraction. A Vagrant plugin can take care of installing and updating the guest additions automatically.

To install the plugin, head to the terminal and run the following Vagrant command:

$ vagrant plugin install vagrant-vbguest

Once Vagrant has been equipped with the plugin, every time you launch a box, Vagrant will check whether the VM is equipped with the guest additions. If the latest version is installed, it'll continue booting the machine. If an update or installation is required, Vagrant will then automatically download all dependent packages and then install the guest additions from the VirtualBox ISO (Figure 2).

Figure 2: The vbguest plugin will automatically download the latest guest additions if it isn't available in the VM.

Once the VM is up and running, no extra window will pop up on the screen; you'll be returned to the shell prompt, because Vagrant VMs run headless by default. Instead you can access the VM by typing:

$ vagrant ssh
[vagrant@localhost ~]$

As you can see, the command uses SSH to connect to the VM. The command will automatically authenticate the SSH sessions and drop you at the SSH shell in the machine. You can now interact with the VM like any other CentOS installation (Figure 3). You can also share files between the host and the VM (see the "Shared Filesystem" box). When you're done working inside the VM, type:

[vagrant@localhost ~]$ exit
Connection to closed.

which will drop you back to your host's CLI.

Figure 3: Vagrant doesn't support SSH using a normal password; instead it uses public key authentication.

Shared Filesystem

With Vagrant, you can easily share files between the host and the VM. The files placed inside the shared filesystem are kept in sync between the physical and the virtual machines. You can then easily roll the shared filesystem into your host's backup strategy. In addition, these files will always be available to the VM whenever you create a new VM instance.

By default, the current project folder (the root folder that contains the Vagrantfile) is shared between the host and guest machines. You can verify this by first SSHing into the VM and then listing the files in the directory:

$ vagrant ssh
[vagrant@localhost ~]$ ls /vagrant

As you can see, this is the same Vagrantfile that's kept under the ~/demo directory on the host. Any file or directory you place inside this folder will be available in /vagrant inside the guest.

You can easily share another folder from the host to the VM by tweaking the Vagrantfile. Open the file and add the following line:

config.vm.synced_folder "/home/bodhi/Documents/", "/docroot"

This line asks Vagrant to share the contents of the /home/bodhi/Documents/ folder on the host inside the /docroot directory in the VM. After saving the file, simply restart the VM with:

$ vagrant reload

During boot up, Vagrant will automatically create the /docroot directory inside the VM and mirror the contents of /home/bodhi/Documents/ inside it. You can again verify this by SSHing into the VM and then listing the directory's contents:

$ vagrant ssh
[vagrant@localhost ~]$ ls /docroot

Vagrantfile Anatomy

Before you can make changes to your VMs, you must familiarize yourself with Vagrantfiles. Vagrant is configured separately for each VM, each of which has its own isolated configuration environment. At the crux of each VM is the configuration file, Vagrantfile.

The Vagrantfile is a simple text file that Vagrant reads in order to create the virtual environment. The Vagrantfile describes the various parameters that are necessary to create the VM. Vagrant reads this file to configure and provision the VMs.

Because of this singular Vagrantfile, other users can use Vagrant to automatically and easily create their virtual environment with a single command. Vagrantfiles are portable, which means you can use them to create and provision VMs on every platform that Vagrant supports, including Linux, Windows, and Mac OS X.

As shown previously, the Vagrantfile is created automatically when you issue the vagrant init command, for example:

vagrant init centos/7

By default, the file is heavily commented out and only exposes a handful of parameters. However, these are the most essential ones, and you can use this Vagrantfile to create a VM with any additional modifications.

Also note that a Vagrantfile is written in Ruby. Even if you aren't well versed in Ruby, you'll be able to configure every aspect of Vagrant without ever learning the programming language.

Boxed In

One of the very first parameters in the Vagrantfile is In our example, this parameter will be set to centos/7.

Because building a VM from scratch is a resource-intensive and time-consuming endeavor, Vagrant uses a base image and clones it to rapidly create a usable machine. In Vagrant terminology, this base image is called a box, and it is distributed in the form of .box files.

Boxes contain already-installed operating systems (CentOS in my example), so they're usually quite large, ranging from a few hundred megabytes to a few gigabytes. The boxes are downloaded when you issue vagrant up for the first time. Vagrant saves this box file for future usage, so it won't have to be downloaded again, even when you remove a Vagrant VM.

The required box needs to be specified for every VM using the parameter in the Vagrantfile. In my example, this is set to centos/7 since this was the box I requested with vagrant init earlier. Remember that multiple Vagrant environments can have the same value, which gives you the flexibility to create different VMs for different purposes with the same box image.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl – Vagrant Package

    The Vagrant package provides easy management of virtual machines with VirtualBox as the hypervisor on the command line. Provisioning tools like Puppet let customers try out products in pre-installed environments.

  • Perl – Elasticsearch

    Websites often offer readers links to articles about similar topics. Using Elasticsearch, the free search engine, is one way to find related documents instantly and automatically.

  • YunoHost

    YunoHost offers a wide range of services on a proven Debian platform that you can host yourself.

  • Docker

    Docker is an economical alternative to conventional virtualization. Because each Docker container shares the underlying operating system, it enjoys the resource isolation and allocation benefits of VMs but is much more portable and efficient.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More