System ProgrammingLearn How Vagrant Boxes Are Packaged And Shared

Learn How Vagrant Boxes Are Packaged And Shared

Vagrant Boxes

In Vagrant, you can create a box by repackaging an environment you have created or you can create a box using minimum components needed for the box to function. Boxes that just have minimum components are referred to as base boxes. An example of a base box is precise64 which consists of a minimum ubuntu install instead of a repackaged environment. Base boxes provide a foundation for building environments. Creating base boxes is a resource intensive process, therefore new users are advised to find a base box they can build on.

A base box only contains minimum software. For example, a Linux box may include a package manager, SSH and a SSH user, and a configuration manager such as Chef. Other providers such as Hyper-V, Vmware, and Docker may need additional software. Guidelines for developing provider specific boxes are available, but we will not discuss them in this tutorial.

When building a base box, you need to consider the following factors listed below:

• Ensure there is adequate disk space. This will differ for each provider. For example, on a Virtualbox, you can create a dynamic drive and assign maximum size.
• Allocate an appropriate amount of memory. Avoid very high default values because the user can always override them in the Vagrantfile.
• Any unnecessary hardware such USB controllers need to be disabled as the user can always add them through the Vagrantfile

Vagrant is a highly customizable environment. For boxes that will be distributed publicly include default values needed for the box to work. For boxes that will be used privately, do not include defaults because they are known security risks. Default values that can be included are listed below:

• Vagrant needs a vagrant user for SSH connection. The default user is set up using an insecure key pair but commonly a password vagrant is assigned to default user to enable password login. The insecure key pair is implemented by placing the public key in ~/.ssh/authorized_keys directory of vagrant user. The ~/.ssh directory requires 0700 permissions while while the authorized keys need 0600 permissions.
• For boxes that will be distributed publicly a root password vagrant can be included which eases customization of the machine
• To enable network configuration, software installation and other requirements, it is important to ensure the default SSH user has a passwordless sudo configuration. Some operating systems lack sudo by default so ensure it is installed. Once sudo has been installed include this entry vagrant ALL=(ALL) NOPASSWD: ALL in your configuration file to enable passwordless sudo. If there is any entry that has requiretty, remove it
• To ensure your SSH is fast in your SSH server configuration, set UseDNS entry to no

Instead of building your base boxes manually, the recommended approach is using Packer for build reproducibility and Atlas for build automation. In this article, we will limit ourselves to demonstrating how Packer and Atlas can be used on Ubuntu.

To install Packer, navigate to this link https://www.packer.io/downloads.html download, unzip it into your preferred folder, enter the folder and add the folder to your path. To verify the installation was complete, the packer command should return available commands as shown in the output below.
install packer
To specify your build, you use template files written in JSON. Broadly build involves using a chosen ISO to create an operating system installation. The build specifics are ISO mounting, booting and OS UI initialization.

Consider the sample template provided below. At the beginning of the template, we have specified user variables. By using variables we improve template portability by keeping out sensitive and platform specific data. The key/value pair is the variable name and a default variable.

{
    "variables": {
        "ssh_name": "sammy",
        "ssh_pass": "sammy",
        "hostname": "sammy"
    },

    "builders": [{
        "type": "virtualbox-iso",
        "guest_os_type": "Ubuntu_64",

        "vboxmanage": [
            ["modifyvm", "{{.Name}}", "--vram", "32"]
        ],

        "disk_size" : 10000,

        "iso_url": "http://releases.ubuntu.com/precise/ubuntu-12.04.5-server-amd64.iso",
        "iso_checksum": "769474248a3897f4865817446f9a4a53",
        "iso_checksum_type": "md5",

        "http_directory" : "http_directory",
        "http_port_min" : 9001,
        "http_port_max" : 9001,

        "ssh_username": "{{user `ssh_name`}}",
        "ssh_password": "{{user `ssh_pass`}}",
        "ssh_wait_timeout": "20m",

        "shutdown_command": "echo {{user `ssh_pass`}} | sudo -S shutdown -P now",

        "boot_command" : [
            "<esc><esc><enter><wait>",
            "/install/vmlinuz noapic ",
            "preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg ",
            "debian-installer=en_US auto locale=en_US kbd-chooser/method=us ",
            "hostname={{user `hostname`}} ",
            "fb=false debconf/frontend=noninteractive ",
            "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA ",
            "keyboard-configuration/variant=USA console-setup/ask_detect=false ",
            "initrd=/install/initrd.gz -- <enter>"
        ]
    }]
}

Before building an image from a template, you can check the validity of syntax and configuration values by passing the template name as shown below.

packer validate packer_example.json

validate
After your template is validated, you build it using the command shown below.

packer build packer_example.json

build packer
After the build process is complete, you can launch your image. Packer does not perform any image management.

Instead of creating a base box an easier approach is selecting an existing box, customizing it and repackaging. You begin by selecting a box that meets your needs. For this article, we will use precise64 from Hashicorp. Initialize, start and SSH into your box using the commands below:

vagrant init hashicorp/precise64
vagrant up
vagrant ssh

init box
After entering your server, you can install all the software you need. In this article, we will demonstrate a minimal set up of a LAMP stack. The actual set up of LAMP is more involving. The commands below will install required software.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install vim
sudo apt-get install apache2
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
sudo mysql_install_db
sudo /usr/bin/mysql_secure_installation
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
sudo apt-get install php5-cgi php5-cli php5-curl php5-common php5-gd php5-mysql
sudo service apache2 restart

Use the command below to ensure your box has minimum size by cleaning up the disk space.

sudo apt-get clean

The last step is clearing your CLI and exiting the VM using the commands shown below.

cat /dev/null > ~/.bash_history && history -c && exit

To repackage the environment we created, we passed the name we would like our box assigned as shown below.

vagrant package --output lamp.box

In this article, we introduced a special type of box known as a base box. We discussed considerations when creating a base box. We discussed some of the default values that can be included in a base box. We demonstrated how Packer can be used to build base boxes. Finally, we demonstrated how to package an existing environment into a box.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -