Saturday, January 23, 2010

Using differencing disks with VirtualBox

When evaluating new software, you often need to setup an isolated environment using virtual machines. This can be a lot of work when you have to install the machines from scratch. To avoid this you can setup a base configuration (for instance a machine which already has SQL server and Visual Studio installed) and copy the hard disk when creating a new instance. The downside of this method is that you waste a lot of disk space. To avoid this most virtualization products such as VmWare, VirtualBox and Virtual PC use differencing disks. These are disks that store only the different sectors in regard to their base or parent disk.

Following procedure shows how to use differencing disks with VirtualBox:

  1. Build the base system.
    After installing the operating system, apply all necessary update and install the VirtualBox additions.
  2. Release the base disk
    Since the base disk will become ‘read-only’, it must be detached form all virtual machines.

    ReleaseDisc
  3. Make the base disk immutable
    Use the following command:
    VBoxManage modifyhd <disk file>  --type immutable


  4. Create a new system from the base disk
    When creating the new system attach the immutable base disk. A new differencing disk will be created.

    NewSystem

    NewDisc

  5. Turn off the autoreset feature of the differencing disk
    This is necessary since the content of a differencing disk will be destroyed when the virtual machine is started.
    Use the following command:
    VBoxManage modifyhd <differencing disk file> --autoreset false



If you want to start a new hierarchy from a differencing disk, you can use the ‘VBoxManage clonehd’ command to build the new base disk. Following figure shows an example:

 

diskgen

7 comments:

Self-Documenting Developer :o) said...

Great article, just a little more detail please...

I don't understand how you got the GUID differencing disk in the 2nd screenshot at step 4.

I assume you had the base (immutable) disk listed there originally? .... did you start the VM and then save a snapshot (which creates the GUID.VDI file) and then re-attach to that??

This is the fundamental bit that I'm stuck on at the moment... I've searched elsewhere and can't quite get this bit right.

I didn't even know about turning off the autoreset feature that you mention iun step 5 - I just thought you needed to keep hundreds of snapshots?!

Another thing I haven't been able to find anything on - once you have several VMs running from 1 immutable base image, can you then update that base (ie an OS update) so that all the children VMs are automatically updated?

Thanks!

Kees said...

What is the best to use? VmWare, VirtualBox, Virtual PC or HyperV?

Anonymous said...

Great article.
As far as the Virtual software choice. If you want a free solution avoid VmWare Server. They've turned it into a crappy software in their newer version, fraught with bugs and an unusable user interface. Probably they are trying to get people to switch to their paid versions. Some examples:
1. You don't get a console (the VM screen) in their main window. You get a nice empty black box on which you click, then wait for a new window to open. Now try getting into the BIOS - you've got virtually no chance, because by the time the VM screen opens the O/S is already loading. Only way to bypass that is to let the O/S start and then restart.
2. They've got a USB device menu in the VM and in the manager. Only don't try to do anything in the VM - it doesn't work. Only the the manager works.

It may seem like annoying little things but it makes the software unusable with cravings for their 1.x series.

Today, VirtualBox answers my needs, but does have certain limitations. It doesn't have a full range of management options. For example, importing a VM requires some work arounds.

Personally I'd avoid Microsoft VM solutions if you think you might want to use other O/Ses. Actually, even if you plan on using MS O/Ses - they might obsolete the support for your VM's O/S version...

Patrick Honorez said...

Thanks,very interesting and well presented. I had lost the contents of my differential disk becaus eof a missing point you highlighted.
I just wonder if there isn't an easier way to achive the same result by using a snapshot method. Anyone heard about that ?

Anonymous said...

I really like this tutorial, but unfortunatelly it is not valid anymore with latest Oracle versions.

Could you please take a look at it and update it?

Thanks!

Jaded Phoenix said...

Works like charm, on the latest Oracle VirtualBox 4.1.8.
Only few remarks though:
1) to create the initial virtual HD you have to create a temporary virtual machine (don't forget to tell VBox to place the virtual HD to the absolute path which will be convenient for you).
2) Do not delete the temporary VM until you attach the initial HD to another VM (the first one you'll be actually using)
3) After you have managed to set up your VM, the temporary VM can be deleted safely (choose to remove all files, the only one left will be your read-only HD).

Lars Nielsen said...

The latest version of Virtual Box has a "Linked Clones" facility - where you can create a new VM linked to a parent one but with its own differencing disk. I think that will acheive the same thing as is posted here? It's easier because you don't need to use vboxmanagedh