Rolling your own vagrant box

[a·bout]

No doubt, vagrant is a great timesaver when it comes to the integation workflow. Setting up a JeOS for development purpose was a timeconsuming process before I discovered vagrant. Each box and its setup is now defined through its configuration file and its Chef recipes. In doubt of a 'unclean' or 'broken' OS, I simply check out my cookbook and my vagrant config and vagrant up.

[ob·jec·tives]

Since I did this very often, I felt it very anoying to apt-get uppdate and upgrade all over again to end with a up-to-date system.
I wanted a VM which didn't need a extensif update procedure.

  • Just enough Operating System
  • updated base packages
  • minimal footprint
  • fully functional

[pre·req·ui·sites]

  • a working vagrant installation
  • a clean ubuntu vagrant box, precise64.box

[step·wise]

First we want to fire up a new vagrant box based on the precise64 box from vagrantbox.es and connect (ssh) to it.

    # create initial Vagrantfile
    vagrant init precise64
    # fire up the vagrant box
    vagrant up
    # connect to the box
    vagrant ssh

For this example I want to dist-upgrade my Ubuntu box to get the newest kernel, modules and packages.
While upgrading you will be asked on which device to install grub on. Choose /dev/sda, VBOX_HARDDISK
After upgrding has finished you need to reboot to ensure booting with the newest kernel.

    # update package list
    sudo apt-get update
    # upgrade all installed packages
    sudo apt-get dist-upgrade -y
    #reboot the box
    exit

Reconnect to the box for part two.

    vagrant halt && vagrant up

    #connect to the box
    vagrant ssh

Now we need to update the Virtualbox Guest Additions. Therefore we need some additional packages to compile the Guest Additions

    # install missing packages
    sudo apt-get install linux-headers-$(uname -r) build-essential dkms make

    # update ruby gems
    sudo  gem update --no-rdoc --no-ri

    # get latest Guest Additions and mount it.
    # The version of the ISO may vary over time ;)
    # You will find the latest version number
    # here: http://download.virtualbox.org/virtualbox/LATEST.TXT
    wget http://download.virtualbox.org/virtualbox/4.3.4/VBoxGuestAdditions_4.3.4.iso

    # mount the image (ISO)
    sudo mount -o loop VBoxGuestAdditions_4.3.4.iso /mnt

    # start the installer
    sudo sh /mnt/VBoxLinuxAdditions.run --nox11
    # un-mount the image (ISO) is not needed because we reboot now
    # sudo umount /mnt
    sudo reboot

Reconnect to the box for final cleanup.

    #connect to the box
    vagrant ssh

To keep vagrant start up time low, size of the box is crucial. Let'sremove every thing which is not needed and fill up the empty filesystem space with zeros for efficient compressing.

    # remove image (ISO)
    rm VBoxGuestAdditions_4.2.12.iso
    # remove package installed for building the Guest Additions
    sudo apt-get purge linux-headers-$(uname -r) build-essential dkms make
    # remove unneded package depandencies and clean package sources
    sudo apt-get autoremove && sudo apt-get autoclean
    # fill up empty space with zero bytes
    sudo dd if=/dev/zero of=/EMPTY bs=1M
    sudo rm -f /EMPTY
    # stop the box
    sudo halt

rebuild the vagrant box

    vagrant package --output precise64upd.box
    vagrant box add precise64upd precise64upd.box
    rm precise64upd.box
    vagrant destroy