Building the Base VM
Building the base VM (Base box) for Vagrant¶
It is assumed that you have installed the Packer and Vagrant tools, as well as the Oracle VirtualBox virtualisation software.
The operations for creating a base box are documented online; you have to follow the basic guide and apply the provider-specific rules, namely for VirtualBox . The procedure is somewhat tedious and, above all, an error can happen quickly. This is why we will use the Packer tool, which allows to fully automate the creation of a Vagrant base box by following the process illustrated by the figure below:
Note: All the scripts and other configuration files mentioned in this description can be found in free access under the GiHub INRAE repository.
We are using Ubuntu version 18.04 as a basis.
A Packer configuration is defined with a JSON file (box-config.json) with several sections:
- "builders": is used to define the elements for creating the virtual machine from an ISO image.
- "provisioners": is used to define the software configuration from Shell scripts to provision the virtual machine.
- "post-processors": runs once the virtual machine is created and provisioned. This allows among other things to define the output format of the VM.
"builders" section
- To build this section, it is often simpler to start from already functional examples and to modify the few elements specific to its configuration. A query in a search engine (e.g. Google) with the keywords "github packer ubuntu 18.04 virtualbox" will give you enough examples. So we started with an example appropriate to our needs, which we then adapted.
- The VirtualBox box is made from an Ubuntu ISO specified by its URL (iso_urls) in order to create the VM in VirtualBox ("type": "virtualbox-iso") .
- A "preseed" file (http/preseed.cfg) allows to configure the installation (see more info on preseeding ). We have adapted the example by adding instructions concerning the root account and the network configuration.
- We limited the maximum disk size to 18GB ("disk_size": 18432) so that the final VM can be accepted by the Genouest cloud.
"provisioners" section
This section allows to run Shell scripts after the virtual machine has booted properly and then its operating system installed. So it is in this step that we can configure the VM to be compatible with Vagrant and VirtualBox. A Shell script (scripts/setup.sh) is then executed in order to:
- install the drivers for VirtualBox
- configure SSH accesses in compliance with Vagrant boxes.
"post-processors" section
- Once the base virtual machine is fully installed and configured, it is simply exported to Vagrant format.
Creating the base VM¶
Once the configuration has been established, simply run Packer as follows:
$> packer build box-config.json
All the messages produced can be consulted on the github repository. The base VM after execution can be found under the builds directory.
Registering a box on Vagrant Cloud¶
Vagrant Cloud provides an API that allows users to register their virtual machines (boxes) with Vagrant Cloud so that they can be reused by themselves or by other users. The use of the API is described online.
The registration of a virtual machine can only be done with the Vagrant format, i.e. a "box". A "box" is in fact a zipped archive file (TAR + GZIP) with the extension '.box'. Registering a "box" consists of 5 steps:
- Creating an entry for the "box": at least the name of the box (boxname) must be specified. Its description is optional.
- Creating a version: there may be several versions of the same entry; the version must be specified.
- Creation of a provider: similarly, for an entry (boxname) and a version, there may be several associated providers; the provider must be specified.
- Upload the box file.
- Validate the box. (entry + version + provider)
All of these steps can be done either via the Vagrant Cloud web interface, or through multiple API calls. In order to facilitate the automation of registrations, the Vagrant tool provides a 'cloud' functionality that allows you to register your box on Vagrant Cloud. This requires an account (called an 'organisation') to be created on Vagrant Cloud.
Example of invocation :
$> vagrant cloud auth login
...
Vagrant Cloud username: GAEV
Vagrant Cloud password: XXXXXXX
$> vagrant cloud publish GAEV/centos7-dd8Gb 1.0.0 virtualbox virtualbox-centos7_8Gb.box
You are about to create a box on Vagrant Cloud with the following options:
GAEV/centos7-dd8Gb (1.0.0) for virtualbox
Automatic Release: true
Do you wish to continue? [y/N] y
Creating a box entry...
Creating a version entry...
Creating a provider entry...
Uploading provider with file /Vagrant/boxes/virtualbox-centos7_8Gb.box
Releasing box...
Complete!
tag: GAEV/centos7-dd8Gb
username: GAEV
name: centos7-dd8Gb
private: false
downloads: 0
created_at: 2020-07-25T17:53:04.340Z
updated_at: 2020-07-25T18:01:10.665Z
current_version: 1.0.0
providers: virtualbox
The registered box can be viewed on Vagrant Cloud.