Monday, July 6, 2009

Snapshots in VirtualBox

Firstly, I want to give credit to this thread - as it steered me onto the right path.

The way snapshots and virtual media (hard disks) work in VirtualBox is clearly not entirely obvious to the newbie, myself included. If you don't understand it, it looks plain buggy... however once you've understood it... there is reason in the madness - and more importantly it works!

In VirtualBox there are 3 types (or modes) of virtual hard disk images, Normal, Immutable and Write-through. These are fairly well explained in the manual - I highly recommend reading at least that section of the manual, as it also helps make sense of snapshots.

I'll discuss only the 'Normal' image type here. This is the default type set, when you create a new machine and hard disk. You start up your guest OS, and everything works nicely. You try powering off the machine, saving the state, everything works as you'd expect.

Then you see the 'Take snapshot' option, and try it. Now, you need to understand how they work - whether you know it or not :)

The snapshot stores the machine state, just like the 'current state' you've used up until now. It also contains a differential image of the hard disk. Ie. from now on, the (normal) hard disk image file (eg. xp.vdi) that was previously being updated, is no longer updated. Instead, when your guest OS writes to disk, it writes into the differential image.

If you create another snapshot, a new differential image is created, so now, the current content of the hard disk is stored across 3 files, the original, and the two snapshots. This of course gives the possibility to go back to an exact, earlier copy of the entire system (including hard disk).

So what's the problem?

Snapshot files, including the differential hard disk image aren't particularly portable. So whilst you can easily move your original hard disk to different machines, moving the updates in your differential files is difficult. Unless you run on basically the same host machine, with the same vbox version you can restore the changes from your differential images.

Or can you?

The solution is to discard your snapshots!

Sounds dangerous? Well, not quite as dangerous as it seems. When you discard a snapshot, it merges the differential image into it's parent image.

So, if you have snapshots like this -
- Snapshot 1
- Snapshot 2
- Current State
And discard 'Snapshot 2', the differential image changes will be merged back into Snapshot 1, leaving -
- Snapshot 1
- Current State
Then discarding 'Snapshot 1', will merge back into your original hard disk image 'xp.vdi'... leaving no snapshots, but also a portable hard disk image.

I recommend experimenting by taking a snapshot, then create some files in the guest OS, and then create another snapshot. Then discard away, and see that the files survive.