02 January 2014

In the last few weeks, I have been using packer and vagrant a lot. This post is dedicated to packer, specifically to build oracle linux using packer.

I suggest check the official packer documentation

Have a look at the packer google groups

Packer is a powerful tool that take arguments from the command line, variables, and use them on top of a template file.

All the files used here are available on github at the url https://github.com/kikitux/packer-vagrant-oracle

Inside this github project, you will find the files used for this packer project

The outcome of build, will be a box file (file.box) that can be imported on Vagrant, or optionally can leave the ova/ovf file to import directly the created VM back into virtual box.

We will focus on Virtual Box + Oracle Linux.

A tipical workflow to build a machine using Packer will be:

Example Workflow

A packer file, have a very simple, yet powerful structure:

    {

    "variables": {},

    "builders": [{}],

    "provisioners": [{}],

    "post-processors": [{}]

    }

Variables

In the variables group, we can define user defined variables, that will allow create different machines without having to modify the whole template. This variables will be override with values given to packer from the command line, so this give us a very convenient method to create different machines from the command line.

In the template files I am using, I have defined the following variables:

So a typical run could be:

    packer build -var 'hostname=myserver' -var 'outputfile=myserver.box' packer-vagrant-oracle65.json

At the end of this execution, we will have a vagrant box file named myserver.box with the hostname of myserver. All automatically done by packer.

Builders

Packer support several builders:

For Virtualbox as a builder, we can pass as many arguments we want that will be executed aftet the virtual machine is created, so any task that you can manually perform using VBoxManage, can be also pass to packer in the Builders section.

For these particular files we are using VBoxManage to:

  • Set video memory

  • Set VM memory

  • Set VM cpus

  • Set OS disk size

  • Set OS disk interface as SATA

  • Create and attach a 2nd, 3rd and 4th hard drive

  • Pass to the VM boot arguments that will be typed on the screen

Provisioners

Packer support several provisioners:

In these packer files, we are using plain shell scripts.

These shell scripts, can be inline, that is the command is typed on the packer file, or a file, that will be uploaded and then executed at the guest.

Example:

        "inline": [
          "mkdir -p /media/dvd",
          "mount -o loop VBoxGuestAdditions*.iso /media/dvd",
          "sh /media/dvd/VBoxLinuxAdditions.run --nox11",
          "umount /media/dvd",
        ]

        "script":       "script.sh"

Post-Processors

Packer support at this moment, 2 post-processors:

In our files, we will be using the vagrant specific tasks.

If you have scripts to take care of application dependencies, then installation of the application, you can build in one shot, a very customized machine, without any manual intervention, read to be used in virtualbox, or imported and used by Vagrant.

Build

An just the OS build, on my computer take about 10 minutes. The output can be used on Virtualbox as an appliance, or by vagrant, so create a new vm using the outof packer, will take less than 10 minutes, typically is the time is needed to copy the files and turn on the new VM.

A more complex build:

  • Create a VM
  • Create 4 HDD
  • Setup VM for memory and cpus
  • Install OS with a kickstart file
  • Setup hostname
  • Format 2nd hdd and mount as /u01
  • Install the pre-requirements for Oracle Clusterware/grid and database

This will create a VM, with a custom hostname with all the binaries needed to just go and install crs/grid and database. Very cool.

We are creating a new machine from scratch, a fully updated OS, plus all the requirements for Oracle Clusterware/grid and database, which done manually takes around 1 hours more of dedicated time.

Here, you just execute packer, and enjoy a cup of coffee.

This is all included in the page of packer on github project

Just check the stagefiles and packer-vagrant-oracle65-btrfs-4disk.json

If you haven't seen packer in action, here's a video of the build of the first VM.

Alvaro.

  • packer 1
  • oracle 6
  • linux 7