Update your Vagrant boxes

Update your Vagrant boxes

VagrantVagrant is a great tool. It’s easy to get started with and coupled with provisioning software, you can create VMs very reliably and efficiently. Consequently, you create VMs when you need them and destroy them once you’re done. You don’t snapshot, archive, export, etc … I use it constantly to build headless linux VMs to test, try, discover, work in “throw away” environments.

Yet, every here and then, you need to update. You can create a VM from a base box from HashiCorp’s box hosting in the cloud, Atlas, provision it and then upgrade. If you allow kernel upgrades, you’ll need to re-install the guest additions for your specific provider (I was doing it manually until a colleague showed me this vagrant guest plugin – thanks Florent). This all works fine, but it’s not very efficient when you’re used to create a VM, test and destroy regularly. A more efficient approach is to update the box.

It’s very comparable to creating a box with a few steps less.

I have to upgrade a box I use for my docker hosts. My box is based on chef/centos-7.0. Yep, it’s a year old: you can imagine how long an update would take. So I update it whenever a new kernel version is released. Why? “If it ain’t broke, don’t fix it!” Mmhm, first of all: nope! Second: Continuous Improvement anyone? Third: I use a Mac, Virtualbox and rely on folder synch to expose my local FS into the VM, which allows me to use local data within docker containers. Plus, using the latest kernel is a good practice when working with docker.

  1. Create a new folder, cd into it and create a new VM based on the outdated box, in my case: vagrant init bob/centos7 && vagrant up && vagrant ssh
  2. Then, simply update: sudo yum -y update
  3. The update is done, we switched to something else in the mean time, let’s check if we have a new kernel.
  4. We do: kernel-3.10.0-229.11.1.el7.x86_64 is new. Well, let’s reboot (automatically using the newest kernel unless you changed the default behaviour) and have the vagrant_guest plugin work for us.
  5. Almost nothing to do and almost done. We’re going to do some clean up before we package the VM into a box.
  6. Remove gcc (installed by the vagrant_guest plugin to compile the guest additions). This is obviously not mandatory, and you may want to install/uninstall packages that are useful/useless to YOU at this stage.

    Clean all cached files from any enabled repository.
  7. Zero out free space:
  8. Clear history
  9. OK, our work in the VM is done.
    Halt the VM with ‘vagrant halt’.
  10. Note: If you use Virtualbox and a VDI disk image, you can compact the disk image with VBoxManage modifyhd --compact PATH_TO_DISK.vdi. You can get the virtualbox name of the VM with
  11. The chef/centos-7.0 (my original base box) uses a VMDK disk image. We could convert to VDI, compact, convert back to VMDK, not sure … But, we’ll leave it. We’ll package the VM into a box.
  12. The box is done. What is left for us to do is to add it to our vagrant boxes. Versioning boxes works only if you use HashiCorp’s box hosting in the cloud. If you store your boxes elsewhere (on a NAS for instance), versioning is not an option. We can add the box with another name or remove the outdated box and add the new one. If you have long lived VMs you rely on, you might prefer the former.
  13. List your boxes and chose an appropriate name for your update or remove/replace the outdated box with the new one. We’ll add the box with a new name to make sure we do not corrupt an existing environment using the outdated box. For the purpose of this post, we’ll name it bob/centos7-003
  14. List your boxes to get state
  15. Add the new box
  16. I have a VM still using bob/centos7, no VMs using bob/centos7-001 and bob/centos7-002. All my new centos 7 VMs will use bob/centos7-003 from now on.
  17. That’s it, time to archive the up to date box, destroy the VM that helped us creating it and delete the folder we worked in. To spin up a new centos 7 VM based on the new updated box
  18. No updates needed for a little while, no guest additions to be installed (automatically or worse, manually), no time wasted.

Save yourself time to work on what matters, update your boxes, not your “throw away” VMs.

Olivier Robert
No Comments

Post a Comment