Cloning VirtualBox images (or how I save hours a day when testing software)

First things first. If you’re testing software and you’re not using some sort of virtualization solution, stop reading this and go install one. My product of choice is VirtualBox. It’s free (as in no cost and most of it is open source), user friendly, runs on an Ubuntu host computer and I’m familiar with it.

Lately I’ve been doing a lot of testing of the Ubuntu One desktop software and I need to be able to quickly get various versions of Ubuntu up and running. Below I outline how I do that on an Ubuntu host computer. My steps assume that you’re familiar with VirtualBox enough that you know how to setup a virtual machine (VM) already.

Create a master image

The master image is the one we’ll use to clone test images off of. By doing this we can worry about keeping our master image up-to-date and configured the way we need it and then simply clone that image when we have to test.

  1. Create a new VM in VirtualBox and install the OS  (see Lifehacker’s guide if you’re not sure how to do this)
  2. After restarting the VM when the install is done, install all the latest updates on the master image and restart
  3. Install the VirtualBox Guest Additions (allows nice integration with the host computer)
  4. Shutdown the master image

Periodically you’ll want to make sure your master image has all the latest updates, so just boot it up, install the updates and then shut it down.

Clone the master image

Now we’re ready to start testing some software. Instead of using the master image we created above, we’re going to clone that image. This should take less than 5 minutes start to finish.

  1. In a terminal session do the following:
  2. In VirtualBox, create a new VM by clicking the New button
  3. Go through each screen selecting the appropriate values and clicking the Next button until you get to the Virtual Hard Disk part
  4. Select the Use existing hard disk radio button
    VirtualBox Hard Disk Setup screenshot
  5. Click on the folder icon next to the pull down menu listing existing VDI files
  6. Click the Add button
    Add VirtualBox VDI screenshot
  7. Select the image you created (should be in ~/.VirtualBox/HardDisks) to add it to the list of available hard disks
  8. Click on the image you just added and then click the Select button
  9. Click the Next button
  10. Click the Finish button

You now have a brand new VM to use for testing. Once you test with this image and decide its usefulness is over you can delete the virtual disk image (VDI) file in ~/.VirtualBox/HardDisks, repeat step 1 above to create a new cloned image, and then edit your cloned VM in VirtualBox to use the new clone image. In other words, you don’t have to setup a new VM (steps 2-10) every time you want to use another VDI if you don’t want to.

  • Love it.
    BTW, most are not aware there are 2 versions of virtualbox: an “enterprise” and OSE version (see http://www.virtualbox.org/wiki/Editions). My guess is if you apt-get install virtualbox you’ll get the OSE version which does not include RDP (remote desktop) and USB support. So I install the “enterprise” version since I need USB support. Both are free to use.

  • Joshua Hoover

    Good point James. I use the enterprise version as well. I install from the VirtualBox repositories so that updates come automatically.

  • Nice blog Joshua!

    In many test cases you can prevent repeating steps 2-10. Just take a snapshot of your clone state on which all subsequent tests rely on. Now do dirty stuff with your clone and finish testing. To get back to the initial state close the clone and indicate that you want to restore the current snapshot. Next time you start the clone again it is ready for the next test round.

    But you can do even more mature stuff with snapshots. Just think of forking test projects. Virtual machines, i.e. VirtualBox in our case, saved us a hell of a lot of time while testing various YAWL installations (http://www.yawlfoundation.org/) on up to 5 different OS.

    • Joshua Hoover

      Great input Stephan! I hadn’t thought of using snapshots like that. I’ve used them in other ways in the past, but you’re right, I can use them to do testing tasks like this as well. I think the combo of snapshots and cloning is a good one. There are times where I need multiple VMs up at the same time to run certain (often tedious, hard to reproduce) tests, which I still need clones. But, in many cases I could use snapshots. Cool!

  • Holden Daugherty

    are you doing this temianl in the virtual box or on the host computer?