Vagrant Tip: Sync VirtualBox Guest Additions

Quick tip. If you lose your Vagrant mounts after kernel upgrades in your virtualbox, you'll need to reinstall your VirtualBox Guest Additions. Same is true when you upgrade Vagrant, etc.

It's just a real pain and people usually avoid it by never upgrading. Or delve in once they accidentally do. But there's actually a nice & automated way of keeping your VM's guest additions in sync with virtualbox.

I was fed up with missing mountpoints and messages like these:

$ vagrant up
[default] Importing base box 'ubuntu-12.04-64bit'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.

Guest Additions Version: 4.1.22
VirtualBox Version: 4.2.6

So, Vbguest plugin to the rescue!

To install, you type this one time in the directory that also keeps your Vagrantfile:

$ # For vagrant < 1.1.5:
$ # vagrant gem install vagrant-vbguest

$ # For vagrant 1.1.5+ (thanks Lars Haugseth):
$ vagrant plugin install vagrant-vbguest

On succesful execution, the output should look something like this:

Fetching: micromachine-1.0.4.gem (100%)
Fetching: vagrant-vbguest-0.6.4.gem (100%)
Successfully installed micromachine-1.0.4
Successfully installed vagrant-vbguest-0.6.4
2 gems installed
Installing ri documentation for micromachine-1.0.4...
Installing ri documentation for vagrant-vbguest-0.6.4...
Installing RDoc documentation for micromachine-1.0.4...
Installing RDoc documentation for vagrant-vbguest-0.6.4...

And that's it. From now on every vagrant up will check & install the correct guest additions right after booting:

[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] GuestAdditions 4.2.6 running --- OK.

And if it's outdated, here's what happens:

$ vagrant up
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] GuestAdditions versions on your host (4.2.10) and guest (4.2.6) do not match.
stdin: is not a tty
Reading package lists...
Building dependency tree...
Reading state information...
linux-headers-3.2.0-23-generic is already the newest version.
dkms is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 126 not upgraded.
[default] Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
stdin: is not a tty
mount: warning: /mnt seems to be mounted read-only.
[default] Installing Virtualbox Guest Additions 4.2.10 - guest version is 4.2.6
stdin: is not a tty
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.2.10 Guest Additions for Linux..........
VirtualBox Guest Additions installer
Removing installed version 4.2.6 of VirtualBox Guest Additions...
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Building the VirtualBox Guest Additions kernel modules ...done.
Doing non-kernel setup of the Guest Additions ...done.
You should restart your guest to make sure the new modules are actually used

Tight! : )