Vagrant is a personal tool to automatically provision virtual machine environments. It comes for free along with VirtualBox plugin, although there are commercial plugins for other virtualization engines such as VMWare. It is a very handy tool, especially for developers: it enables them to quickly mock-up or destroy even complex virtual machine environments by executing just one command. Within this post we'll see how easily it can be installed, and how easy is operating with it.
A tool for quickly mock-up environments
Vagrant is "a tool for building and managing virtual machine environments in a single workflow": this means that you can define a whole environment made of several virtual machines within a single manifest file called vagrantfile - the syntax of the manifest follows Ruby coding.
Vagrant processes this manifest file and executes the directives stated into the file by connecting to hypervisors using a component called provider: Virtualbox and KVM are free providers, but there are also commercial ones like VMWare provider.
Vagrant can be exploited by:
- system engineers to mock-up virtual environments to test installation of new software or to perform proof of concepts
- developers to easily create virtual environments with all the components necessary to perform the integration tests of the software they are developing, without interfering with anybody since the environment is isolated and local to his/hers workstation
If necessary, the vagrantfile can be shared and versioned using the company SCM and even integrated in the toolchain.
Installing VirtualBox and Vagrant
To keep on the free of charge side, and to have an easy to setup solution suitable to every platform, we install Vagrant with the Oracle Virtualbox provider:
- go to https://www.virtualbox.org/wiki/Downloads and download and install Oracle's Virtualbox for the platform you are using
- go to https://www.vagrantup.com/downloads.html and download and install HashiCorp's Vagrant for the platform you are using
The only thing that you must really take care of manually doing is to add VirtualBox's path to the PATH system wide environment variable.
For example, on Windows it must be set as shown by the box on the right: by doing so you are able to issue commands such as VBoxManage without having to specify the full path of the folder containing it.
Vagrant requires access to the Internet to download Virtual Machine images – these are called Vagrant boxes in Vagrant's terms. In addition to that, the operating system instalkle into the Virtual Machines requires Internet access to download software packages to be installed (such as RPM from online yum/dnf repositories).
If you are working without a direct connection to the Internet you should ask the administration team of your corporate proxy to grant requests to https://vagrantcloud.com/ and to the online repositories of the operating system of the Virtual Machines you want to install.
After that you should also have to install the Vagrant proxy configuration plugin – issue the following command - in this example the corporate proxy URL is http://proxy.carcano.local:8080
export http_proxy=http://proxy.carcano.local:8080 export https_proxy=http://proxy.carcano.local:8080 vagrant plugin install vagrant-proxyconf
Once installed, each time you want to run vagrant, you should export the following variables right after opening the command prompt (the shell) before running vagrant up:
- VAGRANT_HTTP_PROXY – for example http://proxy.carcano.local:8080
- VAGRANT_HTTPS_PROXY – for example https://proxy.carcano.local:8080
- VAGRANT_FTP_PROXY – for example http://proxy.carcano.local:8080
- VAGRANT_NO_PROXY - localhost,127.0.0.1,.carcano.local
Upgrading Vagrant requires only to download the new version and install it overwriting the old installation. If you have installed any plugin, right after upgrading Vagrant you should also upgrade them as well. You can upgrade them as a whole as follows:
vagrant plugin update
Downloading Vagrant Boxes
Once both VirtualBox and Vagrant are installed, and proxy has been configured if necessary, you can test your installation by downloading a vagrant box.
You can get a list of public available VagrantBoxes on Hashicorps's VagrantCloud at https://app.vagrantup.com/boxes/search?provider=virtualbox
For example, we can easily install the official centos/8 VagrantBox to our local repository as follows:
vagrant box add --provider virtualbox centos/8
This downloads the CentOS 8 official VagrantBox: it will take some time (of course it mostly depends from the speed of your connection to the Internet), so this can be the right moment to relax with a coffee or a cup of tea.
Updates of the boxes are periodically released. Updating a box is really easy: for example, to update the centos/8 box simply issue
vagrant box update --box centos/8
Although this should be straightforward, it is worth to say that the upgrade is focused only to the box itself: the VMs you already provisioned using that box do not get updated - You should use the updating commands of the guest of itself to keep things up to date.
You can of course copy Vagrant boxes from another workstation (for example if you do not have a fast Internet connection, or you are paying for the amount of data you download): boxes are stored beneath the following path:
- C:\Users\%USERNAME%\.vagrant.d\boxes on Windows
- ~/.vagrant.d/boxes on Mac and Linux
Creating Vagrant Boxes
We can of course create our own Vagrant Boxes: the procedure is quite simple.
First and foremost, let's light-up a Vagrant VM: we can even use an existing Vagrant Box.
For example, the following command generates a Vagrantfile that can be used to start a CentOS 8 VM:
vagrant init centos/8
the output is as follows:
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
now let's provision and start the VM as follows:
and eventually login to the VM using the following command:
we are now connected to the CentOS 8 VM.
Let's update the RPM packages of the CentOS 8 Linux installed into the VM:
sudo dnf update -y
As soon as the update process completes, let's disconnect from the VM (hit CTRL+D, or type "exit" as usual) and halt the VM as follows:
let's get a list the configured VMs, so to guess the name or UUID of the VM Vagrant has just deployed:
VBoxManage.exe list vms
on my computer the output is as follows:
we can now attach the ISO containing the guest additions DVD - the example command below is for a Windows virtualization platform - please adjust the path to the ISO image to reflect the actual path using the syntax specific to your virtualization host.
VBoxManage.exe storageattach "a_default_1623083202261_27402" --storagectl IDE --port 0 --device 1 --type dvddrive --medium "C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso"
we are eventually ready to launch the VM again and begin the installation procedure of VirtualBox guest additions. Let's start the VM as follows:
wait until the startup completes: since the VM has already been provisioned, the startup is far quicker than the first time. Eventually login to the VM as follows:
we are now connected to the CentOS 8 VM - let's enable the EPEL8 repository:
sudo dnf install epel-release
we must now install all of the packages required to compile the VirtualBox Guest additions Kernel modules:
sudo dnf install -y gcc make perl kernel-devel kernel-headers bzip2 dkms
Let's mount the CD-ROM onto the /mnt directory as follows:
sudo mount /dev/cdrom /mnt
We are now ready to compile and install the kernel module. Issue the following commands:
cd /mnt sudo ./VBoxLinuxAdditions.run
example output is as follows:
Verifying archive integrity... All good. Uncompressing VirtualBox 6.1.22 Guest Additions for Linux........ VirtualBox Guest Additions installer Copying additional installer modules ... Installing additional modules ... VirtualBox Guest Additions: Starting. VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules. This may take a while. VirtualBox Guest Additions: To build modules for other installed kernels, run VirtualBox Guest Additions: /sbin/rcvboxadd quicksetup VirtualBox Guest Additions: or VirtualBox Guest Additions: /sbin/rcvboxadd quicksetup all VirtualBox Guest Additions: Building the modules for kernel 4.18.0-305.3.1.el8.x86_64.
wait until it completes. Now we must cleanup the VM as follows:
sudo rm -rf /var/cache/dnf /etc/sshd/*key* sudo truncate -s0 ~/.bash_history /var/log/messages /var/log/secure /var/log/cron /var/log/dnf.* truncate -s0 ~/.bash_history
we have completed the procedure. Let's shutdown the VM:
sudo shutdown -h now
Now we must repackage the VM into a new VagrantBox: in this example we package it within the "centos8_with_guestadditions.box" file:
vagrant package --output centos8_with_guestadditions.box
This completes the procedure to create the box.
We can now add this box to the list of installed boxes as follows:
vagrant box add centos-ga/8 C:\Users\vagrant\Desktop\vagrantlab\centos8_with_guestadditions.box
Vagrant let you provision Virtual Machines in a very convenient and easy way: these are the very basic Vagrant commands you need to know to operate it:
install a given vagrant plugin. For example
vagrant plugin install vagrant-scp
update an already installed vagrant plugin. For example:
vagrant plugin update vagrant-scp
removes an already installed vagrant plugin. For example:
vagrant plugin uninstall vagrant-scp
download a box – remember that boxes are specific for a given provider, so you should specify it too. For example:
vagrant box add --provider virtualbox centos/8
delete a box – For example:
vagrant box remove centos/6
update an already downloaded box. For example:
vagrant box update --box centos/8
If no VM name is specified as argument the action is performed for every VM defined in the vagrantfile. For example, to limit the command to jump-ci-upa002 VM only:
vagrant up jump-ci-upa002
shut down VM. If no VM name is specified as argument the action is performed for every VM defined in the vagrantfile. For example to limit the shut down to jump-ci-upa002 VM only:
vagrant halt jump-ci-upa002
Suspend VM. If no VM name is specified as argument the action is performed for every VM defined in the vagrantfile. For example to limit the suspend to jump-ci-upa002 VM only:
vagrant suspend jump-ci-upa002
resume VM. If no VM name is specified as argument the action is performed for every VM defined in the vagrantfile. For example to limit the resume to jump-ci-upa002 VM only:
vagrant resume jump-ci-upa002
Destroy (delete) VM. If no VM name is specified as argument the action is performed for every VM defined in the vagrantfile. For example to limit the destroy to jump-ci-upa002 VM only:
vagrant destroy jump-ci-upa002
list available snapshots for a given VM. For example
vagrant snapshot list jump-ci-up3a001
create a snapshot for a given VM. For example
vagrant snapshot push jump-ci-up3a001
Revert a VM to the specified snapshot. For example:
vagrant snapshot restore jump-ci-up3a001 push_1607852325_7783
Delete the specified snapshot for the given VM:
snapshot delete jump-ci-up3a001 push_1607852325_7783
Vagrant is certainly a great tool that every professional should be skilled on, since it is dramatically useful to quickly mock-up things. This post is just an operating cheat-sheet: to really exploit the power of Vagrant you should be able to develop vagrantfiles, but this will be the topic of another post.