The other day one of our devs ran into a problem. They vagrant uped their VM and instead of a happy working VM they receive the following message:

Scotts-Air:ubuntu scottkeckwarren$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> default: Setting the name of the VM: ubuntu_default_1455929302562_42575
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
A customization command failed:

["modifyvm", :id, "--name", "BrokenVM"]

The following error was experienced:

#<Vagrant::Errors::VBoxManageError: There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["modifyvm", "5b9eb013-65e0-4d4c-91f3-87b0fd19156a", "--name", "BrokenVM"]

Stderr: VBoxManage: error: Could not rename the directory '/Users/scottkeckwarren/VirtualBox VMs/ubuntu_default_1455929302562_42575' to '/Users/scottkeckwarren/VirtualBox VMs/BrokenVM' to save the settings file (VERR_ALREADY_EXISTS)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component SessionMachine, interface IMachine, callee nsISupports
VBoxManage: error: Context: "SaveSettings()" at line 2788 of file VBoxManageModifyVM.cpp
>

Please fix this customization and try again.

Instead of booting the VM that already existed it created a new VM. When we looked at Virtual Box we saw that the old VM (BrokenVM in this example) still existed and the new vm (ubuntu_default_1455929302562_42575) had been created in error:

Image of both correct and incorrect vms

Vagrant uses a file to keep track of what VirtualBox VM it’s using. If you look in the “.vagrant/machines/default/virtualbox/” directory (“default” is the name of the Vagrant box so it might be different if you have multiple VMs) in the folder that contains your Vagrantfile you’ll see a file named “id”. This represents the VirtualBox ID of the VM it’s using. In my case it looks like this:

5b9eb013-65e0-4d4c-91f3-87b0fd19156a

Now we need to figure out what is the “correct” VM ID by using VBoxManage

Scotts-Air:ubuntu scottkeckwarren$ VBoxManage list vms
"BrokenVM" {cb533ed8-4267-4450-9955-b8b11add10b4}
"ubuntu_default_1455929302562_42575" {5b9eb013-65e0-4d4c-91f3-87b0fd19156a}

As you can see the second line refers to the ID of the newly created/wrong VM but the first line contains the correct VM. To fix this problem we just need to copy and paste the “cb533ed8-4267-4450-9955-b8b11add10b4” in the id file we looked at earlier.

Scotts-Air:ubuntu scottkeckwarren$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => /Users/scottkeckwarren/Projects/ubuntu

Because we’re using Vagrant it wouldn’t have been the end of the world if we had to reset the development box but this way the dev didn’t loose their test data.