Virtual Hard Disks (abbreviated to VHDs) are files on the physical machine that store the hard disk contents of a Virtual Machine (VM). Each VHD file is treated as a separate hard disk by Windows® Virtual PC (WVPC).
The Virtual Hard Disk file format is an open standard. For the curious, the specifications of VHD file format can be found here: http://technet.microsoft.com/en-us/virtualserver/bb676673.aspx. As can be seen, the format is quite straightforward and is completely agnostic to the virtualization technology in use and to the host and guest operating systems.
WVPC, Windows® XP Mode, Microsoft Hyper-V®, Virtual PC 2007® and other Microsoft’s virtualization solutions use the VHD format.
There are three types of Virtual Hard Disks: fixed disks, dynamically expanding disks and differencing disks.
Fixed disks take up all the space that the VHD is allowed to have up front. For instance, if you create a fixed disk of size 64GB, the VHD file will occupy 64GB of hard disk space right from the time of creation, and its size will never vary.
Dynamically expanding disks (also called sparse disks) increase in size to take up space as required. The size specified when creating a dynamically expanding disk indicates the maximum size that the disk is allowed to grow to. For instance, if you create a dynamically expanding disk of size 64GB, the VHD file might initially occupy only a few KB. It will slowly grow upon usage to occupy up to the maximum size specified (64GB). Note however, that the guest operating system will be made to believe it has the full 64GB right from the start.
Differencing disks are a little different. They merely save the changes to the VHD relative to a base disk. Differencing disks are always linked to a dynamically expanding disk or a fixed disk, either directly or through one or more other differencing disks. To understand differencing disks, let us say you create a fixed disk with Windows® XP installed on it. Now if you create a differencing disk and install Microsoft Office® on this, the differencing disk will contain mainly* the content of the hard disk locations changed by the installation of Microsoft Office®, which will be the locations containing the new files and any modifications to existing files.
* I say “mainly” since the operating system will make changes to the hard disk too, as part of its regular operation (writing to the page file, for instance).
To illustrate this point, take a look at the figure below. There are two VMs using two differencing disks that branch from the same base disk.
The changes made by VM#1 are saved in its differencing disk (depicted as orange blocks) and the changes made by VM#2 are saved in VM#2’s differencing disk (depicted as green blocks). Wherever there is no data in the differencing disk (i.e., a blank space between the orange blocks), the data will be automatically fetched from the base disk. Thus, VM#1 will see the resultant hard disk as a mix of orange and blue blocks. Neither VM#1 nor VM#2 will be able to see the changes made by the other since these changes are on different differencing disks, and both think they have their own copy of the base disk.
The base reference disk is called the “parent” of the differencing disk, and the differencing disk is called its “child”. As you might have guessed, a differencing disk is unusable without the parent disk. What’s more, any change made to the parent will potentially invalidate the child since the reference file is now different. This is why it is recommended to choose a base parent disk and mark it “read-only” for good.
And what’s the size of the differencing disk, you ask? Well, the differencing disk grows in size as required, and inherits the maximum size from its parent disk.
Needless to say, all types of VHDs will have the “.vhd” extension. The maximum size of the VHDs supported by Windows Virtual PC is 127GB.
Dynamically expanding disks and differencing disks are by far the most commonly used VHD types.
The biggest advantage of differencing disks is that you can have as many child disks as you like from a single parent disk. So if you need 5 virtual machines with Windows® XP with each configured differently, you can simply create a single sparse/fixed VHD with Windows® XP installed on it and then create 5 differencing disks from this. So that’s saving 4 times the space needed to install Windows® XP!
Indeed, this is the approach used in Windows® XP Mode! Well, let me explain that in detail.
When a user installs the Windows® XP Mode package, a dynamically expanding VHD is placed on the system. This is the base VHD, which contains a sys-prepped copy of Windows XP SP3®. The base VHD has already been marked read-only, and what’s more, it’s permissions have been set in such a way that only TrustedInstaller can make changes to it (and the only change TrustedInstaller will make is to remove the file on uninstall). This takes care of having a pristine base VHD that won’t be modified (Figure 2).
When you click on the “Windows XP Mode” link on the start menu after installing the VHD, VPC detects that the VHD has been installed and launches a wizard that takes basic information needed to create the VM. As you can see, this wizard is different from the wizard used to create a VM – for starters, this wizard has already decided to create a differencing disk with the Windows XP Mode VHD as the parent; it merely asks you where you want to place the differencing disk.
Each user on the computer will be shown this wizard and will have a new differencing disk created for them. This way, every user has access to their personal copy of Windows XP without taking up extra space for the Windows XP install.
This above example is a typical use-case for differencing disks. You can have a similar setup on your machine(s) with Windows® Vista and/or Windows® 7 VMs too. Of course, it is strongly advised that you optimize the base VHD before calling it pristine and marking it read-only. Ah, we haven’t discussed VHD optimizations yet, have we? Well, considering that topic is big enough to merit its own post, I’ll save that for another day. Stay tuned for more on VHDs!
Microsoft Virtualization Team