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

后端 未结 10 1794
梦如初夏
梦如初夏 2020-12-02 03:57

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

相关标签:
10条回答
  • 2020-12-02 04:05

    Update with same problem today with Vagrant 1.7.4:

    • useful thread at https://github.com/mitchellh/vagrant/issues/1755 and specially with following commands:

    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
    
    0 讨论(0)
  • 2020-12-02 04:09

    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

    0 讨论(0)
  • 2020-12-02 04:14

    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

    0 讨论(0)
  • 2020-12-02 04:15

    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

    0 讨论(0)
  • 2020-12-02 04:21

    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.

    0 讨论(0)
  • 2020-12-02 04:21

    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.

    0 讨论(0)
提交回复
热议问题