Saturday, July 3, 2010

VirtualBox: Using Differencing Disks and Immutable Images

As I promised some time ago, I'm now writing an introductory blog post about a subject that some VirtualBox desktop users probably aren't aware of: differencing disks.

When you configure your guest's storage, you might have noticed a Differencing Disks checkbox in the configuration panel. If you tried to check it, chances are that nothing seems to happen. The VirtualBox GUI, indeed, provides no way to configure disks types and, consequently, not even differencing disks. The only clue that differencing disks are there can be seen if you made a snapshot of a virtual machine disk image. In that case, if you inspect the Storage settings of your virtual machine, you will notice that the attached disk is a "Normal (VDI)" disk type, as shown in the following screenshot:


If you now click the Differencing Disks checkbox, you will see that the attached disk actually is a differencing disk:


If you inspect the Hard Disk list box you will notice that what's really attached to your virtual machine is a differencing disk by UUID:


It won't be difficult to discover that UUID corresponds to the disk that stores the "differencing information" of the snapshot I took:


as can be seen in the file system:


There's a chance, then, that differencing disks are already part of your VirtualBox experience as a hidden "side effect" of taking virtual machine snapshots.

In this blog post we'll discover that immutable images (and differencing disks) are disk image types with its own advantages and we'll explore some use cases you can benefit from them.

Image Write Modes

Every virtual disk image you're using in your guests has a property callled image write mode. The mode can be:
  • Normal.
  • Write-through.
  • Immutable.

Normal Images

The default normal images are what you're accustomed to when running your typical guest. The guest instance can read and write to the image without restrictions and the image can be snapshotted. Snapshot data are written to a differencing image that receives the differences to the original image (or to its parent snapshot.) Normal  images can only be attached to one running guest at a time.

Write-through Images

Write-through images, with respect to normal images, aren't affected by snapshots.

Immutable Images

An immutable images, as its name implies, does not permanently record changes: changes are available only while the machine is running. As soon as it's power cycled, changes are lost and the image its reverted to its original state. As in the case of snapshots, VirtualBox will use a differencing image to record changes to an immutable image. The differencing image is then reset to its original state at guest power cycle. The auto-reset feature of a differencing disk can also be turned off. Immutable images can be attached to as many running hosts as you wish: each guest they'll be attached to will create a new differencing image to store their changes.

Use Cases

Each image type has its own pros and cons:
  • Normal images can be easily snapshotted but can't be used by more than a guest at a time.
  • Write-through images aren't affected by snapshots: they're ideal to host critical data that won't be compromised by a snapshot restore.
  • Immutable images won't ever be compromised: a power-cycle will restore them to their initial state. They can be used by multiple host at a time and are the quickest way to deploy multiple (initially) identical guest instances.

Immutable Image with a Differencing Disk

Using immutable images with differencing disks is one of the quickest way to deploy multiple instances of the "same" virtual machine. Since more than one virtual machine will be sharing the same underlying image you'll also spare a good amount of storage space.

I, for example, had to deploy more than ten instances of a proprietary database: all of the instances could share the same immutable image since all of the instance would use the very same operating system and the same initial data base configuration.

Creating A Virtual Machine

The first step to build your own immutable image is creating a virtual machine. This step is the same as usual:
  • I created a virtual machine (with a normal image).
  • Installed the operating system.
  • Installed the data base.
  • Finished to configure the environment with most of the configurations all instances would share.

Cloning the Disk

To clone your hard disk image you've got to use VirtualBox command line interface. Open your favorite shell and use the VBoxManage command to clone your hard disk:

$ VBoxManage clonehd <uid>|<file> <output>


You may check with either VirtualBox GUI, adding your newly created image or with the command line interface, as shown in the following images:


Marking the Image as Immutable

Once you're finished with your virtual machine you've got to mark the image as immutable. Once again, this step can only be performed using VirtualBox command line interface. Open your favorite shell and use the VBoxManage command to mark to change the image type:

$ VBoxManage modifyhd <file> \
  --type immutable


Both VirtualBox GUI and the VBoxManage command may be used to check an image type:



Attaching the Immutable Image to a Virtual Machine

Now you can attach your immutable image to a virtual machine. Every time you attach an immutable image to a virtual machine, VirtualBox will create a differencing disk for you that will store the changes recorded for a specific virtual machine.

As explained in the previous sections, the default differencing disks created by VirtualBox will be reset every time a virtual machine is stopped and restarted. Depending on your use case, you might want changes to be permanent.

Setting an Image Auto Reset Flag to Off

Every differencing disk has got an autoreset flag which, by default, is set to on. To set it to off you have to use the VBoxManage command:

$ VBoxManage modifyhd <uuid>|<filename> \
  --autoreset off

Your differencing disk won't "forget" any more the changes it records.

Conclusion

I hope that this quick walkthrough helps you discover some lesser known functionality of VirtualBox, either if you're using it as a desktop or as a server virtualization platform. Either if you're deploying many images or if you're simply installing a couple of virtual machines for your family, immutable images and differencing disk will help you spare a lot of storage space and, above all, a lot of your precious time.

Further Readings

If you're a Solaris user (at least Solaris 10, OpenSolaris, OpenIndiana, etc.) you will notice that the basic benefit of using immutable images can be achieved using native Solaris technologies such as ZFS snapshots and clones or ZFS deduplication. Using ZFS instead of VirtualBox immutable images is leaner and reduces VirtualBox administration times.

Although not related to VirtualBox, if you're interested at the advantages that the Solaris' ZFS file system can bring even in a deployment of a virtualized desktop environment, these are interesting reads:









1 comment:

Anonymous said...

Hi Enri,

This is good information. One observation... the graphics are not visible, may be link is broken.

User