Creating a Ubuntu 12.04 Vagrant Base Box for Virtualbox + Chef Solo Bootstraping

The following are instructions to create a Vagrant base box based on the official Ubuntu releases (12.04 as of this writing), and how to bootstrap chef-solo to be used as a provider in Vagrant.

![Vagrant Logo](/content/images/2014/Apr/vagrant_logo.png) **+** Virtualbox Logo **+** Chef Logo

  • Download latest Ubuntu 12.04 from Vagrantbox.es
  • Import box
    • vagrant box add ubuntu_12.04_latest precise-server-cloudimg-vagrant-amd64-disk1.box
  • Start Vagrant using imported box
    • NS_ERROR_ABORT fix (within ~/.vagrant_d/ dir for box):
    • openssl sha1 *.vmdk *.ovf > box.mf
    • vagrant init ubuntu_12.04_latest
    • vagrant up
  • Create DVD device for Virtualbox instance
    • Stop VM
    • Virtualbox GUI --> Machine --> Settings --> Devices --> New DVD --> Load iso for GuestAdditions
  • Start VM
    • vagrant up
  • Make mnt directory, mount DVD
    • sudo mkdir /mnt/dvd/; sudo mount /dev/dvd /mnt/dvd
  • Run Guest Additions installer
    • sudo /mnt/dvd/VBoxLinuxAdditions.run
  • Update & Clean up machine
    • sudo apt-get update; sudo apt-get dist-upgrade; sudo apt-get autoremove; sudo apt-get autoclean;
    • unset HISTFILE
    • rm -fr /home/vagrant/.bash_history
    • sudo rm -fr `sudo find /var/log/ -type f`
    • # Zero out the free space to save space in the final image:
    • sudo dd if=/dev/zero of=/EMPTY bs=1M
    • sudo rm -f /EMPTY
    • # Removing leftover leases and persistent rules
    • echo "cleaning up dhcp leases"
    • sudo rm /var/lib/dhcp/*
    • sudo rm /var/lib/dhcp3/*
    • # Make sure Udev doesn't block our network
    • echo "cleaning up udev rules"
    • sudo rm -fr /etc/udev/rules.d/70-persistent-net.rules
    • sudo mkdir /etc/udev/rules.d/70-persistent-net.rules
    • sudo rm -rf /dev/.udev/
    • sudo rm -fr /lib/udev/rules.d/75-persistent-net-generator.rules
    • sudo shutdown -h now
  • Repackage box
    • vagrant package
    • vagrant box add package.box
  • Bootstrap chef-solo
    • Create a chef-solo bootstrapping configuration based on the chef-full.erb bootstrap file found in:
    • ~/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.4.4/lib/chef/knife/bootstrap
    • Remove unecessary commands from file:
    • Validation
    • Encrypted data bag
    • Ohai
    • Chef start - IMPORTANT
    • Remember to close single quote at the end of the file
    • Save it as ubuntu12.04-solo.erb
    • knife bootstrap localhost -p 2222 -x vagrant -i /Users/togume/.vagrant.d/insecure_private_key --sudo --template-file /Users/togume/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.4.4/lib/chef/knife/bootstrap/ubuntu12.04-solo.erb
  • Update & Clean up machine
    • sudo apt-get update; sudo apt-get dist-upgrade; sudo apt-get autoremove; sudo apt-get autoclean;
    • unset HISTFILE
    • rm -fr /home/vagrant/.bash_history
    • sudo rm -fr `sudo find /var/log/ -type f`
    • # Zero out the free space to save space in the final image:
    • sudo dd if=/dev/zero of=/EMPTY bs=1M
    • sudo rm -f /EMPTY
    • # Removing leftover leases and persistent rules
    • echo "cleaning up dhcp leases"
    • sudo rm /var/lib/dhcp/*
    • sudo rm /var/lib/dhcp3/*
    • # Make sure Udev doesn't block our network
    • echo "cleaning up udev rules"
    • sudo rm -fr /etc/udev/rules.d/70-persistent-net.rules
    • sudo mkdir /etc/udev/rules.d/70-persistent-net.rules
    • sudo rm -rf /dev/.udev/
    • sudo rm -fr /lib/udev/rules.d/75-persistent-net-generator.rules
    • sudo shutdown -h now
  • Repackage box

Note: The DHCP cleanup becomes important when the Vagrant configuration includes private network configuration. This causes the network to not come up as desired after a box is re-packaged, and re vagrantup-ed. Thanks to +Bastian Kuberek for his Gist (https://gist.github.com/bkuberek/5266195).

comments powered by Disqus