Fixing a broken vagrant box

Posted on Thu 17 October 2013 in Tech

The first time this happened I went into a cold sweat. My box with all dev versions of my companies live databases had died. Of course I had the box provisioned with salt so configuration wasn't a problem, but grabbing all those databases, downloading, altering specific internal values and inserting them was going to cost me hours. These steps should've been salted too but I hadn't gotten round to it yet.

When I brought my vagrant box up, vagrant kept thinking that my original box had gone and kept trying to re-create a new one and provisioning it. Instead of starting from scratch I dug down and tried to figure out the problem. This issue has happened on several occasions for me so I figured I'd document the steps to fix it. -

  1. Start up virtualbox and make sure the box is still there and working.
  2. Sometimes I find that the .vbox file can get corrupted. This sucks but go into the folder that contains the vbox file and see if there's a VM_NAME.vbox-prev and copy that back to VM_NAME.vbox and try to run it just via virtualbox to see if it picks back up. This was always my issue, I believe because my laptop sometimes ran out of space and virtualbox tries to write a new file it can't and simply writes something corrupted.

Make sure Vagrant points to your working VirtualMachine

  1. While trying to get vagrant up and running again it may have create a bunch of similar boxes, get rid of those and retain the box you want to keep.
  2. Run this VBoxManage list vms and get the GUID of your newly fixed box.
  3. From the vagrant box directory go to .vagrant/machines/default/virtualbox and replace the contents of this file with just the GUID. Make sure no newline exists at the end of this file. This is key. A lot of editors will automatically add a newline here and won't present it when you attempt to delete the newline. Try diffing this id file with the id file from another vagrant box to see if the newline still appears. Otherwise, vim will also state noeol in it's statusbar to confirm there is no end of line.
  4. Run vagrant status and see if it picks up your old box, if it doesn't make sure step 4 is correct.

Test your VM

  1. Make sure none of the other VMs are still running. I use the vagrant list plugin for this. Once installed run vagrant list.
  2. Then I shut down any that might conflict with my VM using VBoxManage controlvm VM_NAME poweroff
  3. Then vagrant reload my vagrant box to ensure everything works.