Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant - set the location of the virtual hard drive for VirtualBox

I have executed following commands (on Windows, using Git Bash) in the directory D:\vagrant\precise32\02-lamp\

$ vagrant box add precise32 http://files.vagrantup.com/precise32.box
$ vagrant init precise32
$ vagrant up

Note. I haven't changed original Vagrantfile.

I thought the directory D:\vagrant\precise32\02-lamp\ would be the place of the VDI-like file but it is not. The working directory serves as the shared folder.

I found the location of the Vagrant box C:\Users\USER\.vagrant.d\boxes\precise32\0\virtualbox

According to Where is Vagrant saving changes to the VM I found in the VirtualBox GUI the location of the Virtual hard drive file. Which is

C:\Users\USER\VirtualBox VMs\02-lamp_default_1458429875795_57100\

I would like to put this file not in the system drive C:\ but in the data drive which is D:\. How to set such vagrant configuration?

like image 538
mwloda Avatar asked Mar 20 '16 12:03

mwloda


People also ask

What does vagrant configure 2 mean?

Vagrant.configure("2") do |config| # ... end. The "2" in the first line above represents the version of the configuration object config that will be used for configuration for that block (the section between the do and the end ). This object can be very different from version to version.

What is the difference between vagrant and VirtualBox?

VirtualBox is basically inception for your computer. You can use VirtualBox to run entire sandboxed operating systems within your own computer. Vagrant is software that is used to manage a development environment.


2 Answers

For VirtualBox, you can change the location of what is known as the Default Machine Folder through the GUI's Preferences dialog box.

VBox GUI (Preferences)

This guide, while a couple of years old, works fine and I followed it last week for how to move an existing vagrant/VirtualBox drive to a new location.

EDIT

I have quoted the steps from the above link/guide, for posterity:

  • Move ~/.vagrant.d to the external drive. I renamed it vagrant_home so I'd be able to see it without ls -a.

  • Set VAGRANT_HOME to /path/to/drive/vagrant_home in ~/.bash_profile.

  • Open the VirtualBox app, open Preferences, and set its Default Machine Folder to /path/to/drive/VirtualBox VMs.

  • Close VirtualBox.

  • Move your VirtualBox VMs folder to the drive. Reopen VirtualBox. You'll see your VMs are listed as "inaccessible". Remove them from the list.

  • For each VM in your VirtualBox VMs folder on the external drive, browse to its folder in Finder and double-click the .vbox file to restore it to the VirtualBox Manager. (Is there an easier method than this?)

  • Finally, move any existing Vagrant directories you've made with vagrant init (these are the directories with a Vagrantfile in each) to the external drive. Since these directories only store metadata you could leave them on your main drive, but it's nice to keep everything together so you could fairly easily plug the whole drive into another machine and start your VMs from there.

like image 88
Brian Brownton Avatar answered Oct 09 '22 21:10

Brian Brownton


It is also possible to do this via CLI for when you ALWAYS want to change where Virtualbox creates the VMs during import (because Virtualbox usually wants to put them in a single place rather than tracking wherever they live on the disk the way VMware does it).

Note that changing this setting via the GUI or CLI does NOT move existing VMs, it will simply set a new path to be used by the next machine imported/created. If you have existing machines you want to move, you could shutdown and close all of the instances of Virtualbox and then use mv /old/path /new/path from a cmd/shell window or cut and paste the folder to the new location in the GUI and then change the machinefolder to that path and open Virtualbox and it should detect all the existing VMs.

Using the CLI makes it much easier to script/automate if you have a large number of users needing to move the VMs path out of their home directory to avoid huge files getting automatically backed up. The "best" place for the VMs depends a little bit on your system, but /usr/local/ can be a good place to create a new folder on macOS or Linux.

# Look at the current path

vboxmanage list systemproperties | grep machine

# Output (commented for easier copying and pasting of commands)
# Default machine folder:          /Users/<YourUser>/VirtualBox VMs

# Set it to a different folder in your home aka ~
# If you user has access to the path and can create files/folders, then
# the folder doesn't need to exist beforehand, Virtualbox will create it

vboxmanage setproperty machinefolder ~/VirtualMachines

# No output produced

vboxmanage list systemproperties | grep machine

# Output (commented for easier copying and pasting of commands)
# Default machine folder:          /Users/<YourUser>/VirtualMachines

You can also set it to a folder outside of home, but this usually requires creation of the folder and the permissions to be fixed before Virtualbox can use it.

# [Optional] Only needed if moving out of the home directory to
# a place the user doesn't have permission to access by default
sudo mkdir -p /usr/local/VirtualMachines && \
sudo chown -R ${USER} /usr/local/VirtualMachines

# If you add : like this `${USER}:` to the above, instead of 
# setting the group to admin or wheel it will use the user's default group
# which can be seen by running `id -g`
vboxmanage setproperty machinefolder /usr/local/VirtualMachines

# No output produced

vboxmanage list systemproperties | grep machine

# Output (commented for easier copying and pasting of commands)
# Default machine folder:          /usr/local/VirtualMachines

If you change your mind you can easily set it back to the default, but you'll need to move your VMs back again yourself.

vboxmanage setproperty machinefolder default

vboxmanage list systemproperties | grep machine

# Output (commented for easier copying and pasting of commands)
# Default machine folder:          /Users/<YourUser>/VirtualBox VMs
like image 20
dragon788 Avatar answered Oct 09 '22 21:10

dragon788