How do I associate a Vagrant project directory with an existing VirtualBox VM?
Asked Answered
G

11

140

Somehow my Vagrant project has disassociated itself from its VirtualBox VM, so that when I vagrant up Vagrant will import the base-box and create a new virtual machine.

Is there a way to re-associate the Vagrant project with the existing VM? How does Vagrant internally associate a Vagrantfile with a VirtualBox VM directory?

Grimonia answered 24/2, 2012 at 16:26 Comment(0)
M
91

WARNING: The solution below works for Vagrant 1.0.x but not Vagrant 1.1+.

Vagrant uses the ".vagrant" file in the same directory as your "Vagrantfile" to track the UUID of your VM. This file will not exist if a VM does not exist. The format of the file is JSON. It looks like this if a single VM exists:

{
   "active":{
      "default":"02f8b71c-75c6-4f33-a161-0f46a0665ab6"
   }
}

default is the name of the default virtual machine (if you're not using multi-VM setups).

If your VM has somehow become disassociated, what you can do is do VBoxManage list vms which will list every VM that VirtualBox knows about by its name and UUID. Then manually create a .vagrant file in the same directory as your Vagrantfile and fill in the contents properly.

Run vagrant status to ensure that Vagrant picked up the proper changes.

Note: This is not officially supported by Vagrant and Vagrant may change the format of .vagrant at any time. But this is valid as of Vagrant 0.9.7 and will be valid for Vagrant 1.0.

Malfeasance answered 24/2, 2012 at 20:47 Comment(8)
Awesome! Thank you. I had a feeling something was missing.Grimonia
Could be 'default' changes to custom name without using multi-VM config?Dm
For Vagrant 1.1, the UUIDs are stored in a machine-specific file at .vagrant/machines/{name}/{provider}/id. In fact, the only thing that file contains is the UUID.Archangel
I tried this trick but vagrant status simply reports the state as not created. Vagrant up then creates a new vm and works fine. If I change the id file it then reports as not created, even if I change it back to the uuid of the box it just created.Joacimah
With Vagrant 1.1+, make sure you don't add a newline to the end of the id. See github.com/mitchellh/vagrant/issues/1755 for detailsSluggish
+1 to @JustinY - the {provider} specific directory structure was the key for meReader
I was looking for .vagrant in my profile (Mac). Note that (at least for me) it was in the same folder as the cookbooks folder and VagrantFile. (Vagrant version 1.4.3)Cystic
@Mitchell, there is a popular plugin for Vagrant github.com/tknerr/vagrant-managed-servers which allows you to use existing virtual machine from many projects. For example, I develop on Windows/OSX, have one vagrant docker-host machine, and all my other projects just mapp some folders to docker-host, and start containers there. Unfortunately this plugin has some issues, in particular rsync/rsync-auto do not work with it. Why not to make this possibility integral part of Vagrant, so we don't have to hack .vagrant folders ?Liddle
T
223

For Vagrant 1.6.3 do the following:

1) In the directory where your Vagrantfile is located, run the command

VBoxManage list vms

You will have something like this:

"virtualMachine" {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

2) Go to the following path:

cd .vagrant/machines/default/virtualbox

3) Create a file called id with the ID of your VM xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

4) Save the file and run vagrant up

Together answered 26/8, 2014 at 23:35 Comment(8)
A good way to create the id file without a newline is echo -n '<your id here>' > id .Electrophysiology
Works great. But why does this happen?Tolbooth
Worked for me with Vagrant 1.7.2. Thanks!Gon
Worked perfectly.Foliole
# test1 and test2 boxes... for box in test1 test2; do virtualboxid=$(VBoxManage list vms | grep $box | cut -d' ' -f2); echo -n $virtualboxid > .vagrant/machines/$box/virtualbox/id; doneDoloritas
When running VBoxManage list vms on Windows, if you get an error "VBoxManage: command not found", you can try this instead: "C:\Program Files\Oracle\VirtualBox\VBoxManage" list vmsDog
Works with Vagrant 2.x also. btw, if using Vim, be sure to :set binary and :set noeol so it won't add a newline at the end.Theologize
Thank you, this fixed my issue too. I had a cluster so I just copied over all the machine directories in the machines directory.Frederik
M
91

WARNING: The solution below works for Vagrant 1.0.x but not Vagrant 1.1+.

Vagrant uses the ".vagrant" file in the same directory as your "Vagrantfile" to track the UUID of your VM. This file will not exist if a VM does not exist. The format of the file is JSON. It looks like this if a single VM exists:

{
   "active":{
      "default":"02f8b71c-75c6-4f33-a161-0f46a0665ab6"
   }
}

default is the name of the default virtual machine (if you're not using multi-VM setups).

If your VM has somehow become disassociated, what you can do is do VBoxManage list vms which will list every VM that VirtualBox knows about by its name and UUID. Then manually create a .vagrant file in the same directory as your Vagrantfile and fill in the contents properly.

Run vagrant status to ensure that Vagrant picked up the proper changes.

Note: This is not officially supported by Vagrant and Vagrant may change the format of .vagrant at any time. But this is valid as of Vagrant 0.9.7 and will be valid for Vagrant 1.0.

Malfeasance answered 24/2, 2012 at 20:47 Comment(8)
Awesome! Thank you. I had a feeling something was missing.Grimonia
Could be 'default' changes to custom name without using multi-VM config?Dm
For Vagrant 1.1, the UUIDs are stored in a machine-specific file at .vagrant/machines/{name}/{provider}/id. In fact, the only thing that file contains is the UUID.Archangel
I tried this trick but vagrant status simply reports the state as not created. Vagrant up then creates a new vm and works fine. If I change the id file it then reports as not created, even if I change it back to the uuid of the box it just created.Joacimah
With Vagrant 1.1+, make sure you don't add a newline to the end of the id. See github.com/mitchellh/vagrant/issues/1755 for detailsSluggish
+1 to @JustinY - the {provider} specific directory structure was the key for meReader
I was looking for .vagrant in my profile (Mac). Note that (at least for me) it was in the same folder as the cookbooks folder and VagrantFile. (Vagrant version 1.4.3)Cystic
@Mitchell, there is a popular plugin for Vagrant github.com/tknerr/vagrant-managed-servers which allows you to use existing virtual machine from many projects. For example, I develop on Windows/OSX, have one vagrant docker-host machine, and all my other projects just mapp some folders to docker-host, and start containers there. Unfortunately this plugin has some issues, in particular rsync/rsync-auto do not work with it. Why not to make this possibility integral part of Vagrant, so we don't have to hack .vagrant folders ?Liddle
E
48

The solution with upper version is quite the same.

But first you need to launch the .vbox file by hand so that it appear in VBoxManage list vms

Then you can check the .vagrant/machines/default/virtualbox/id to check that the uuid is the right one.

Elsyelton answered 10/5, 2013 at 15:27 Comment(3)
Also, it's worth noting that if you're editing the id file, you must keep it to a single line. For some reason it doesn't like newline at end (my text editor adds it by default, so I had problems).Downer
I ran into an issue with this because I had renamed the folder that it was in. Vagrant insisted on creating a new VM with the same name as the folder, instead of using the existing VM. I fixed it by creating a .vagrant file in the 1.0 format, and it upgraded it automatically. As soon as I did, it actually renamed the old VM to the new project directory name.Andromeda
I am not sure what caused the disconnect where my vagrant/machines/default/virtualbox/id pointed to the wrong VirtualBox machine id. The file which helped was $HOME/.VirtualBox/VirtualBox.xml. It has a MachineRegistry section having the VirtualBox machine UUIDs mapped to the machine names. For instance <MachineEntry uuid="{f232f951-103b-af28-9f8d-b2ab21bs258f}" src="$HOME/VirtualBox VMs\<machine-name>\<machine-name>.vbox"/>Crites
C
15

Had the issue today, my .vagrant folder was missing and found that there was a few more steps than simply setting the id:

  1. Set the id:

    VBoxManage list vms
    

    Find the id and set in {project-folder}/.vagrant/machines/default/virtualbox/id.

    Note that default may be different if set in your Vagrantfile e.g. config.vm.define "someothername".

  2. Stop the machine from provisioning:

    Create a file named action_provision in the same dir as the id file, set it's contents to: 1.5:{id} replacing {id} with the id found in step 1.

  3. Setup a new public/private key:

    Vagrant uses a private key stored in .vagrant/machines/default/virtualbox/private_key to ssh into the machine. You'll need to generate a new one.

    ssh-keygen -t rsa
    

    name it private_key.

    vagrant ssh then copy the private_key.pub into /home/vagrant/.ssh/authorized_keys.

Coptic answered 28/9, 2016 at 9:10 Comment(4)
is "authorized_keys" a file or directory?Atharvaveda
it's a file, if you search on it you'll find plenty of stuff, it's a standard ssh thingCoptic
Because I was using the same keys, I didn't have to do the last setp (third). Otherwise, really well explained and everything perfect.Intercede
How to do the third step on win10?Vipul
D
9

Update with same problem today with Vagrant 1.7.4:

For example, to pair box 'vip-quickstart_default_1431365185830_12124' to vagrant.

$ VBoxManage list
"vip-quickstart_default_1431365185830_12124" {50feafd3-74cd-40b5-a170-3c976348de27}
$ echo -n "50feafd3-74cd-40b5-a170-3c976348de27" > .vagrant/machines/default/virtualbox/id
Dirge answered 9/11, 2015 at 11:4 Comment(0)
H
6

For multi-VM setups, it would look like this:

{
   "active":{
        "web":"a1fc9ae4-5d43-49cb-be31-ab3c4f74745d",
        "db":"13503bc5-76b8-4c26-95c4-32435b372212"
   }
}

You can get the vm names from the Vagrantfile used to create those VMs. Look for this line:

config.vm.define :web do |web_config|

"web" is the name of the vm in this case.

Harbinger answered 31/10, 2012 at 23:45 Comment(2)
And db is a default VM ??Halsted
I don't think there is a default here, just web and db. Depending on the Vagrantfile, there is an order of which vm gets built first.Harbinger
L
6

This is modified from @Petecoop's answer.

Run vagrant halt if you haven't shut down the box yet.

Then list your virtualboxes: VBoxManage list vms

It'll list all of your virtualboxes. Identify the box you want to revert to and grab the id between the curly brackets: {}.

Then edit the project id file: sudo nano .vagrant/machines/default/virtualbox/id (from the project directory)

Replace it with the id you copied from the list of VBs.

Try vagrant reload.

If that doesn't work and gets hung on SSH authorization (where I stumbled), copy the insecure public key from the vagrant git. Replace the content of /.vagrant/machines/default/virtualbox/private_key. Backup the original of course: cp private_key private_key-bak.

Then run vagrant reload. It'll say it's identified the insecure key and create a new one.

default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!

You should be all set.

Lucilius answered 23/2, 2017 at 19:45 Comment(0)
S
5

I'm using Vagrant 1.8.1 on OSX El Capitan

My vm was not shut correctly when my computer restarted, so when i tried vagrant up it was always creating new vm. No solutions here worked for me. But what did work was a variation of ingmmurillo's answer

So instead of creating .vagrant/machines/default/virtualbox/id based on the id from running VBoxManage list vms. I had to update the id in .vagrant/machines/local/virtual_box/id

I've got a one liner that essentially does this for me:

echo -n `VBoxManage list vms | head -n 1 | awk '{print substr($2, 2, length($2)-2)}'` > .vagrant/machines/local/virtualbox/id

This assumes the first box is the one i need to start from running VBoxManage list vms

Stupa answered 11/4, 2016 at 4:44 Comment(0)
L
3

In Vagrant 1.9.1:

I had a VM in Virtual Box named 'Ubuntu 16.04.1' so I packaged it as a vagrant box with:

vagrant package --base "Ubuntu 16.04.1"

responds with...

==> Ubuntu 16.04.1: Exporting VM...
==> Ubuntu 16.04.1: Compressing package to: blah blah/package.box
Lyrate answered 27/12, 2016 at 21:57 Comment(0)
L
0

I'm on macos and found that removing the .locks on the boxes solved my problem.

For some reason

vagrant halt

did not remove these locks, and after restoring all my settings in .vagrant/machine/default/virtualbox using timemachine, removing the locks, the right machine booted up.

Only 1 minor problem remains, It booted into grub so I had to press enter once, don't know if this is staying, but I will find out soon enough.

I'm running vagrant 1.7.4 and virtualbox 5.0.2

Longstanding answered 4/3, 2016 at 9:35 Comment(0)
T
0

for me deleting the

cd yourVagrantProject/.vagrant/machines/default/virtualbox/
rm id

worked.

Takishatakken answered 13/1, 2021 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.