3

I'm using Vagrant on two machines (home and office) with my working directories stored in Dropbox. I regularly run into the pronblem:

The VirtualBox VM was created with a user that doesn't match the
current user running Vagrant. VirtualBox requires that the same user
be used to manage the VM that was created. Please re-run Vagrant with
that user. This is not a Vagrant issue.

This is easily solved, brute-force, by rm -rf .vagrant, but I'd like to find a more elegant solution -- like an easily automated way to put the .vagrant files elsewhere.

Update

Here's the scenario: a working directory shared between office and home machine via Dropbox.

  1. I vagrant up on office machine, successfully.
  2. I shut down office machine and go home.
  3. I get the message about on vagrant up, saying the id values aren't the same as when I created. (503 vs 501 in general)
  4. I do rm -rf .vagrant and do vagrant up again, successfully.
  5. When I go back to the office, same problem.
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • when does it happen ? when you run `vagrant up` ? so to make clear your `.vagrant` directory is in dropbox but not the vm ? are your VMs in dropbox too ? the issue with virtual box is that the VM are within a central folder (like `/Users/user/Documents/Virtual Machines` for mac) (vmware for example store the VM within the `.vagrant` directory which is easier then when you backup a project but another story) so probably not part of the working directory then each of your 2 machines have created their own VMs with their own ids so when you switch machines you have issues – Frederic Henri Mar 22 '16 at 17:15
  • Yes, it happens when I run `vagrant up`. See above – Charlie Martin Mar 22 '16 at 19:16
  • Can I ask why you want to do this? The general philosophy behind vagrant is that the VMs are easily recreate-able, so you should be easily able to create boxes for your home and office and not need to share the `.vagrant` directory - it's ok that they have different machine IDs, because your provision script should make sure the boxes are built the same. – Brian Brownton Mar 24 '16 at 18:04
  • Well, I want to do it because I work on the same files at home and at the office. The provisioning script is no help because this is an issue where the machine image was built under id 501 on one box and 503 on another; vbox isn't looking inside the machine at all. And unfortunately that *does* make a difference, since it leads to the error message above. – Charlie Martin Mar 24 '16 at 18:07

2 Answers2

2

On the original issue

I get the message about on vagrant up, saying the id values aren't the same as when I created. (503 vs 501 in general)

you can fix it by editing your UID in the following file .vagrant\machines\default\virtualbox\creator_uid and changing the 501/503 to a 0. (0 means you run as sudo)

But I think another issue that will pop up is that each of your machines is creating its own VM and stores it under your /Users/user/Documents/Virtual Machines and you're not sharing those files.

when you create a VM using VirtualBox, vb will assign the VM and id - this id is key and will make the link between vagrant and vb. As you create 2 different VMs, the VM have different Id and vagrant is not able to switch between the 2.

Hope this is clear so far, but how to go from there - you would have couple of options:

  1. apparently you don't mind much about the content of the VM (as you do rm -rf so you recreate the VM each time you switch computer) so you can create a .vagrant.home and .vagrant.office and switch the .vagrant to point to the corresponding folder when you change machine so at least you don't need to recreate the VM, you can just start the VM from the corresponding computer. This is not ideal but will work.

  2. you can avoid switching the .vagrant directory from above points by setting the env variable VAGRANT_CWD so you will not store the .vagrant directory under your dropbox account but you can have a script that export this variable and store the .vagrant directory separately on each of your machine (a folder that is not shared) so each machine will create its own VM. You can have the VAGRANT_CWD set in your bash profile for each computer if you plan to have a different location, but you can also have for example a workhere.sh script that will just do export VAGRANT_CWD=/folder... if you have the same settings on both computer and just do source workhere before you do vagrant up

  3. the other option (I did not test) will involve a few steps: export/import the VM once created from VirtualBox into one computer to another. The issue here will be to keep Id. I am not sure but normally virtual box should keep the Id when you import into the other computer.

    • you can check the file under /Users/<user>/Library/VirtualBox/VirtualBox.xml and review the entry under <MachineRegistry>, you will get the uuid of the machine entry, make sure they match between your 2 computers after you export/import
    • for vagrant there is another file you need to sync between your 2 computer /Users/<user>/.vagrant.d/data/machine-index/index which list of available VM on the computer. If you have other VM on one computer not available on this other, it would be ok to sync but best is to copy only the necessary entry for the VM you want to sync.

After you've done this and you sync the .vagrant folder, you should be able to vagrant up from one computer or the other.

Again, I did not test all the steps but I assume this would work.

Few notes:

  • this will work only if the 2 computers have the same settings (same OS (if not version at least same family), folder structure as some files involve full path, same virtual box version ...)
  • vagrant box should be in sync on the 2 computers (but I assume this is the case as you can vagrant up from same Vagrantfile)
  • if you destroy/create the VM on any of the computer, you will need to sync again the Id as virtual box will assign a new Id each time you create a new VM
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • The issue isn't the vagrant key, but the fact that the image was created by uid 503 on one box and 501 on the other. – Charlie Martin Mar 24 '16 at 18:16
  • well it may be not the machine Id yet but it will be - I doubt vagrant assign same machine Id on the 2 machines - for now edit `.vagrant\machines\default\virtualbox\creator_uid` and put `0` – Frederic Henri Mar 24 '16 at 19:54
  • @FrédéricHenri, when you said `create a .vagrant.home and .vagrant.office and switch the .vagrant to point to the corresponding folder` , are you saying to have .vagrant being a symlink to the appropriate folder? – clarity123 Mar 24 '16 at 20:36
  • @user454038, symlink would be ideal but as it would need to be shared in the dropbox you cannot really have it - I added another option using the `VAGRANT_CWD` env variable that you can use to point to a.vagrant directory that would not be in the dropbox folder – Frederic Henri Mar 24 '16 at 22:04
  • What about a symlink chain `a -> b -> actual target`, so dropbox holds a symlink `a` to an intermediate symlink `b` outside of dropbox folder. This intermediate symlink `b` then points to target home when at home, whereas the one at office points to office. – clarity123 Mar 24 '16 at 22:41
  • @user454038 that could work right, even though working with `VAGRANT_CWD` env var is probably easier so you can directly target the correct folder – Frederic Henri Mar 25 '16 at 07:10
1

Vagrant provides an environment variable for exactly this use case: VAGRANT_DOTFILE_PATH

See the documentation here:

https://www.vagrantup.com/docs/other/environmental-variables.html#vagrant_dotfile_path

VAGRANT_DOTFILE_PATH

VAGRANT_DOTFILE_PATH can be set to change the directory where Vagrant stores VM-specific state, such as the VirtualBox VM UUID. By default, this is set to .vagrant. If you keep your Vagrantfile in a Dropbox folder in order to share the folder between your desktop and laptop (for example), Vagrant will overwrite the files in this directory with the details of the VM on the most recently-used host. To avoid this, you could set VAGRANT_DOTFILE_PATH to .vagrant-laptop and .vagrant-desktop on the respective machines. (Remember to update your .gitignore!)

finn
  • 3,237
  • 1
  • 20
  • 12