7

I was working on a virtual machine that I set up with Vagrant when I realized that I needed to access it through port 8000 to test a web interface so I did vagrant halt in order to modify the Vagrantfile and forward port 8000 then I saved the file and did vagrant up to boot back up that machine and to my surprise instead Vagrant built a new machine from scratch. Now when I look inside the folder named 'VirtualBox VMs' I can see two VMs with the same name + a long number that differs. I believe one is the first machine that I want to boot with all my packages and settings while the other is the new empty one. How can I switch back to the first one so when I do vagrant up it actually boots that one? And optionally how do I get rid of the new, empty one?

Bastian
  • 5,625
  • 10
  • 44
  • 68

2 Answers2

8

My understanding is each Vagrant project directory (home of the Vagrantfile) is associates with a Virtual Machine managed by vagrant in VirtualBox (default provider). Normally in the pattern of "baseboxname_id".

Your problem could be caused by bugs in old Vagrant versions. Please make sure you are running the latest Vagrant 1.3.3 + VirtualBox 4.2.18. OR, possibly mixed up multiple Vagrantfile (not likely to be the case based on your description).

Figuring out the ID of the vagrant box

  1. Change directory to where the Vagrantfile resides
  2. View the contents of => /path/to/Vagrantfile/.vagrant/.vagrant/machines/default/virtualbox/id, you'll get an id like a90df491-4c25-4a07-be1a-be137908058c.

    NOTE: This is the uuid of the Virtual Machine associated with the Vagrantfile managed by VirtulBox.

Getting rid of the new/empty VM

Now that you know the uuid, you can unregister the VM and delete it => VBoxManage unregistervm <uuid> --delete.

Re-associate the Vagrantfile with the 1st VM

  1. Get the list of VM names and uuids managed by VirtualBox
  2. Replace the id with the uuid of the 1st VM in /path/to/Vagrantfile/.vagrant/.vagrant/machines/default/virtualbox/id
  3. Bring the 1st VM up => vagrant up
  4. Done!
Terry Wang
  • 13,840
  • 3
  • 50
  • 43
  • good answer thanks :) My version of vagrant is 1.0.3-1ubuntu3 so it looks kind of outdated compared to the one you state (1.3.3) but since it's the latest available in the Ubuntu repo then I'll leave it like that for now. Just an addition to your answer, to list the VMs the command is `VBoxManage list vms` and with that info it is indeed possible to switch VMs within the vagrant folder. – Bastian Oct 01 '13 at 08:32
  • Since 1.1+ vagrant changed the way it is distributed, from gem to OS specific package. For Ubuntu just use the .deb (self-contained) package, it is easy to manage and worth the effort for new features and bug fixes. http://downloads.vagrantup.com/ – Terry Wang Oct 01 '13 at 10:06
0

As stated above collect the desired vm id with something like:

`vboxmanagevm list vms`

Then replace the contents of $vagrant_base_dir/.vagrant/machines/default/virtualbox/id with the desired id.

What is not mentioned above is that the file needs to be a single line with no new line or carriage return: Try something like:

tr -d '\n' < id > id

This tidbit was found How do I associate a Vagrant project directory with an existing VirtualBox VM?

Hope this helps anyone who finds it.

Community
  • 1
  • 1
  • 1
    `vboxmanagevm list vms` didn't work for me. Maybe a typo in this answer, but `vboxmanage list vms` worked. – wickywills Jan 27 '17 at 09:55