Moving Virtual Boxes in Ubuntu

This is perhaps the 100 millionth post on the topic, yet all I could find was how to move the virtual box files through vboxmanage, which appears to imply having to remove snapshots. Here is a very easy different process that will just repoint to where the machine’s folder is located.

I need this process since I relocate files between different mount points, when I add some space somewhere. They all live on the same machine, so this is only telling the virtual box service, where a given machine is. Since VirtualBox 4, a central configuration file for the service, ~/.VirtualBox/VirtualBox.xml, contains only the pointers to those machine directories, while these directories then contain the information about which files they talk to. Notice, that if you share virtual disk images between machines, this process will likely not be so easy. I haven’t tested it, but I assume you’d have to modify the .vbox file of each machine as well.

So here is what you do:

1. Stave state of all running virtual machines and collect a list in /tmp/savedvms:

rm /tmp/savedvms
for i in $(ps ax | grep VBox | grep comment | grep -v grep | awk '{ print $7};'); do echo Suspending $i...; echo $i >> /tmp/savedvms; vboxmanage controlvm $i savestate; done

2. Modify the machine’s location in the file


There are elements like

where you can change the location of the .vbox file – that file, in turn,
contains the locations of the attached hard disks. This information is
cached by the running VBox Service, which is why we need to restart that
service or

3. Make the VBox Service stop:

ps ax | grep VBoxSVC | grep -v grep | awk '{ print $1;}' | xargs kill -1

Note: This stops (ungracefully) all vms. That’s why we’ve stopped them
above. Also, if you have phpVirtualBox, it’ll stop working at this point.
We could also have done

/etc/init.d/vboxsvc restart

yet I’m still working on getting the service re-read its configuration
file without having to stop the service, and hence having to do savestate
all the machines. That’s why I’m using the longer command ending doing a
kill -1: Perhaps someone could help with showing how to have the service
reread its configuration on demand.

4. Restart the machines that were running and remove the temporary file

for i in $(cat /tmp/savedvms); do echo Resuming $i...; vboxmanage startvm $i -type vrdp; done
rm /tmp/savedvms

Note: The parameter -type vrdp makes it start headless.