By Brian Leonard on Oct 23, 2008
Another common problem that can plague developers is that their machines can become "friendly" to their application. How often have you tried to set up a development environment using someone else's build instructions to no avail, all because they forgot to include some minor configuration setting? By using VirtualBox, you can ensure that the next time you need to write build instructions, they'll actually work as intended.
In both cases, however, how do you keep the virtual machine clean? Over time it will become as friendly to your application as your host operating system. VirtualBox does have a Snapshot feature. However, if you have multiple machines, it could be tedious to roll them back individually. A better option in this case is to use a ZFS snapshot. By putting VirtualBox in its own ZFS file system, you can take a snapshot whenever you like, and then roll back to your clean slate once testing is complete. I've been doing this for about a month now and I find it quite powerful. Here's how I set it up:
VirtualBox stores both its virtual hard disks and machine configurations in a hidden directory off your home directory:
As you can see, you can change the location of these directories. However, I didn't find an option to change the location of the ~/.VirtualBox/VirtualBox.xml configuration file, which I also want under ZFS control. Therefore, we'll simply move some things around so we can mount our new ZFS file system at ~/.VirtualBox.
- Quit VirtualBox if it is running.
- Rename the existing .VirtualBox directory
mv .VirtualBox .VirtualBoxHold
- Create a new ZFS file system:
pfexec zfs create -o mountpoint=~/.VirtualBox rpool/vbox
- Change the group and owner for the new .VirtualBox directory, substituting your user name for bleonard:
pfexec chown bleonard .VirtualBox pfexec chgrp staff .VirtualBox
- Move the data into the new .VirtualBox directory. This will take several minutes, depending on the size and number of machines you are moving:
mv .VirtualBoxHold/\* .VirtualBox/.
And that's it. You now have your VirtualBox environment in its own ZFS file system:
bleonard@opensolaris:~$ zfs list NAME USED AVAIL REFER MOUNTPOINT rpool 40.7G 10.9G 55.5K /rpool rpool@install 18K - 55.5K - rpool/ROOT 6.32G 10.9G 18K /rpool/ROOT rpool/ROOT@install 15K - 18K - rpool/ROOT/opensolaris 6.32G 10.9G 4.72G legacy rpool/ROOT/opensolaris@install 480M - 2.22G - rpool/ROOT/opensolaris/opt 1.14G 10.9G 1.14G /opt rpool/ROOT/opensolaris/opt@install 80K - 3.60M - rpool/export 21.7G 10.9G 19K /export rpool/export@install 15K - 19K - rpool/export/home 21.7G 10.9G 21.7G /export/home rpool/export/home@install 20K - 21K - rpool/vbox 12.8G 10.9G 12.8G /export/home/bleonard/.VirtualBox
To take a snapshot of your current machine state:
pfexec zfs snapshot rpool/vbox@clean
Now, start VirtualBox and do whatever you please. There's not even a need to shut down the systems when you're done. Just power off the machines:
And then rollback to your snapshot. Be sure to quit VirtualBox before rolling back:
pfexec zfs rollback rpool/vbox@clean
You'll then find your machines in their original pristine state. Happy testing.