How do I associate a Vagrant project directory wit

2019-01-09 22:05发布

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?

10条回答
等我变得足够好
2楼-- · 2019-01-09 22:22

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
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-09 22:32

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

查看更多
我命由我不由天
4楼-- · 2019-01-09 22:34

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

查看更多
Summer. ? 凉城
5楼-- · 2019-01-09 22:35

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
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-09 22:35

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

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.

查看更多
甜甜的少女心
7楼-- · 2019-01-09 22:42

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.

查看更多
登录 后发表回答