Hyper-V Virtual Hard Disk (VHD) Operations Explained

image

 Build your test lab with Boot-to-VHD. Here are the steps.
 Deploy a VM to cloud and build your lab in Windows Azure with 90-day free trial. Here's how.
 Preping for Microsoft certifications? Join our Windows Server 2012 "Early Experts" Study Group.

imageVHD is a file format employed in Microsoft virtualization solution. Essentially it operates and behaves much just like a physical hard disk, while in fact it is a file. There has been much information already available regarding VHD and those who are not familiar with this format should review Virtual Hard Disk Getting Started Guide first.

There are various way to create and manage a VHD. For those who are deployment focused or prefer operating via a command prompt, DiskPart is available. On the other hand, with GUI there are also Hyper-V Manager and Disk Manager with VHD operations.

In this post, the focuses are on the VHD operations with Hyper-V Manager. And there are really just three routines: creating, editing, and inspecting a VHD. One can start these routines from Action dropdown menu and Actions pane of Hyper-V manager once a Hyper-V host is highlighted. To create, edit, or inspect a VHD, simply click the corresponding option as shown above.

The following individual routines present the user experience after a user starts a particular routine by clicking a particular option indicated by the top level heading. Also notice that the term, VHD, depending on the context stands for either a virtual hard disk itself or the format of a virtual hard disk.

   

1. (Creating) New VHD

 
 image When creating a VM in Hyper-V Manager, one can at the same time create a VHD on the fly. Here the dialog shows the default settings of a new VHD to be added along with creating a VM. Normally this is in a process of installing OS to the VHD, then or later, from an installation media or a network image store.
image In Hyper-V Manager, first highlight an intended Hyper-V host, then create a new VHD by clicking the New option in Action dropdown menu or Actions pane.
image  SNAGHTML6b3841 Using Disk Manager in Computer Management, one can also create or attach a VHD. Notice there are however only two VHD types available, Fixed and Dynamic, when creating one in Disk Manager. In Windows Server 2012 desktop, there is a useful desktop shortcut to access Disk Manager. Simply holding down Windows key and pressing the X key at the same time will pop up a menu of frequently used tools with a shortcut to Disk Manager.
   

VHD Formats

 
image During the process of creating a VHD, need to first specify the format. In Windows Server 2012, a new format, VHDX, is available in addition to VHD. There is a noticeable difference in the storage capacity between the two formats. Further VHDX also provides data corruption protection during power failures and optimizes structural alignments to prevent performance degradation on new, large-sector physical disks. Hyper-V Virtual Hard Disk Format Overview has additional information detailing these VHD formats.
   

VHD Types

There are three VHD types and each is with target scenarios.
imageimageimageimage 

Fixed Size

This type allocates storage at VHD creation time. The size of a Fixed Size or Fixed VHD, as the name indicates, stays the same throughout the life of a disk. Since all available storage is allocated at creation time, a Fixed VHD offers a predictable and best performance on operations relevant to storage allocation and is recommended for production use.

In the process, Windows Server 2012 defaults the format of a new blank VHD to VHDX and the size to 127 GB. Here, the shown routine reset the size and created a 5GB VHD on the local hard disk. The 5 GB size here is chosen due to limited disk space availability on the associated hard disk. To create a VHD for installing OS, for example, the size of the VHD should be large enough to include OS, patches, applications, temp storage, page files, buffer space, etc.      

   
image

imageimageimage

Dynamically Expanding

This type of a VHD is first created with just housekeeping (or header/footer) information, i.e. the name, location, maximum size, etc. of the disk. As data are written into a Dynamic VHD, the total size of the VHD will grow accordingly. Here is a routine to create a 5 GB Dynamic VHD.

So a Dynamically VHD is rather small in size when first created and the size grows as data are written into the disk. At any given time, a Dynamic VHD is with a size of the actual data written to it and the housekeeping information. Notice, upon deleting data from a Dynamic VHD, the space of those deleted data is not reclaimed till an Edit Disk/Compact operation is operated upon which.

A Dynamic VHD is recommended for development and testing, since relatively small footprint to manage. A server intended to run applications not disk intensive is also a possible candidate for a Dynamic VHD. Still when it comes to performance, a Fixed VHD always performs better than a comparable Dynamic VHD in most scenarios by roughly 10% to 15% with the exception of 4k writes, where Fixed VHD performs significantly better as documented in Hyper-V and VHD Performance - Dynamic vs. Fixed.

   
imageimageimageimage

Differencing

                             A Differencing VHD is a so-called child disk based on a linked parent disk. Creating a child disk by specifying the parent disk establishes the parent-child relationship. Since then a child disk stores those changed/modified data of the parent disk, i.e. the write operations to the parent disk. Here the screen flow shows how to create a Differencing VHD.                      Again, a Differencing VHD is a child disk which stores the delta of an associated parent disk. For instance, if a differencing disk is created and linked to a parent disk containing a generalized sysprep image, a VM based on the child disk will then store all subsequent changes and customization like system identity, accounts, profiles, applications, data, etc. Using a child disk to deploy a VM maintains a consistent base image, however the parent-child dependency also decreases the portability. For instance, when a parent disk is relocated, all child disks must reconnect with the parent disk to validate the relationship with a current path. The concept of a child disk and the ability to separate/isolate changes from the parent disk also introduces interesting scenarios to facilitate IT operations by capturing, applying, undoing, reverting, or merging a child disk (i.e. implementing changes stored in a child disk) to an associated parent disk. In fact, taking a snapshot of a VM is to in essence freeze the current state and make it a parent disk based on current state, and at the same time create a child disk to capture all subsequent changes. And a best practice in testing a patch for an examined VM is to take a snapshot of the VM before and after applying the patch to ensure the ability to predictably and precisely apply or back out those changes introduced by a patch, should it become necessary.           For testing, troubleshooting, forensic analysis, and those processes requiring capturing a particular state of a runtime environment, a VM snapshot which is based on the concept of a Differencing VHD is a great tool. A VM snapshot is nevertheless not to be employed a backup solution. Since each snapshot introduces a parent-child dependency of the runtime environment when the snapshot was taken, and over time a series of backups will results in a multi-level hierarchy of snapshots with nested parent-child dependencies which is not only prone to data corruption and operational errors, but likely prolonging the restore time with a chain of dependencies. 
   
image Here in File Explorer, Dynamic and Differencing VHDs initially contain only housekeeping information and do not allocate all declared storage. The initial size of each is far from the declared size, 5 GB. While a Fixed VHD allocates all declared storage at creation time.
   

2. Editing Disk

Depending on the type of a VHD, various editing options are available. The following are a few examples.
image

Example: Compacting VHD

This operation compacts the file size of a VHD, while the configured storage capacity remains the same. Notice for a Dynamic disk, the size of the disk grows as data are written. However deleting content does not automatically reclaim the associated space. A compact operations is necessary to possibly reduce the file size.
   
imageimageimageimageimageimage

Example: Converting Format

For backward compatibility, here is a routine to edit and change the format of a disk from VHDX to VHD. Since this operations will create a new disk with a copy of the source content, there is an opportunity to specify both the format and the type of the new disk. And here in addition to the format, the type is changed from Fixed to Dynamic. In other words, the operations to convert a VHD in effect copy the source disk to a newly created disk with a specified format and a selected type.

Converting a format does not apply to a Differencing VHD since both the format and the type are dependencies between a child disk and its parent and not to be changed for the parent-child link to work, although the Convert option is available for a Differencing VHD.

   
imageimageimageimage

Example: Expanding Dynamic Disk

To increase the size of a Dynamic VHD, edit and expand the disk. The process is fairly straightforward.

   
imageimageimage

Example: Merging Disk

To permanent introduce changes captured in a child disk, edit a child disk and select the option to merge the child disk into the parent disk. On the left, the process shows that the changes can be directly merged into the parent disk itself or a newly created Dynamic or Fixed disk. This routine is likely to follow a successful test/validation of a target patch or a new device driver against a child disk with an existing deployment image as the parent disk, for example.

   

3. Inspecting Disk

In an event that some inconsistency is identified in a parent-child relationship, a disk inspection is necessary.
image From Hyper-V Manager, highlight a target Hyper-V host and click Action menu to inspect a VHD. An inspection will display pertinent information of a disk including: format, type, location, and size. Here an inspection shows the Dynamic VHD which I created (originally as a 5 GB dynamic disk indicated by the file name) was extended to 10 GB.
   

Validating Differencing Disk

 
imageimage For a differencing disk, an inspection displays the information of a child disk and reveals the parent-child relationship. And for an existing parent-child pair, the Inspect Parent button indicates the relationship is currently validated. And clicking the button will display the properties of the parent disk as shown here.
   

Reconnecting Parent Disk

 
image Once a parent-child relationship is established by successfully creating a Differencing Disk, i.e. child disk, any changes of the parent disk such as applying a new patch or changing the path to the parent disk will invalidate the parent-child link. The recommendation is to set a parent disk to read only. In an event that the parent disk is relocated, the child disk needs to reconnect with its parent disk. At this time, inspecting the child disk will display a red cross indicating an error and a Reconnect button. Here this error was introduced by relocating the parent disk to a new location. To validate the link, click Reconnect button at this time.
imageimageimageimage Clicking the Reconnect button and specifying the new location to reference the parent disk will resolve the issue as shown in this routine. Once validated, the wizard displays the information of the child disk with the Inspect Parent button for inspecting the parent disk and indicating the parent-child relationship is again validated.