VirtualBox Rollback

As a software developer, I use VirtualBox to test how my applications look, feel and behave across different operating systems and/or browser combinations. I can also easily test things like Java Web Start or remote application deployment without needing access to a network of machines.

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.

  1. Quit VirtualBox if it is running.

  2. Rename the existing .VirtualBox directory
    mv .VirtualBox .VirtualBoxHold
  3. Create a new ZFS file system:
    pfexec zfs create -o mountpoint=~/.VirtualBox rpool/vbox
  4. Change the group and owner for the new .VirtualBox directory, substituting your user name for bleonard:
    pfexec chown bleonard .VirtualBox
    pfexec chgrp staff .VirtualBox
  5. 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.


You can also use delegate zfs administration permissions to the user for their own filesystems:

$ pfexec zfs allow -s @usercontrol mount,create,destroy,rename,snapshot,rollback,promote,clone export
$ pfexec zfs allow ivan @usercontrol export/home/ivan
$ zfs create export/home/ivan/.VirtualBox
$ zfs snapshot export/home/ivan/.VirtualBox@20081023

You can also delegate certain properties to the user, then they can change settings such as compression, copies, checksums, or let them manage their own user properties on the filesystem. User properties would be a useful one, I think the upcoming zfs auto snapshot work will look for certain properties to control how often it will take a snapshot and how long to keep older snapshots around.

Posted by Ivan Richwalski on October 23, 2008 at 05:31 PM GMT #

very nice article but the command pfexec zfs rollback rpool/vbox@clean does not work very well on opensolaris 11.08. It freeze my system and do a reset.

Posted by phimic on October 31, 2008 at 03:40 AM GMT #

Phimic, where did you get 2008.11? Regardless, please file a bug.

Posted by Brian Leonard on October 31, 2008 at 08:31 AM GMT #

Post a Comment:
  • HTML Syntax: NOT allowed

The Observatory is a blog for users of Oracle Solaris. Tune in here for tips, tricks and more as we explore the Solaris operating system from Oracle.

Connect with Oracle Solaris:


« April 2014