Differencing Disk or Snapshot Parent/Child Chain
Hyper-V differencing virtual hard disk (VHD) is an interesting and sometimes very useful subject in virtualization world.
As a matter of fact Snapshot AVHDs are also differencing virtual hard disks. My favorite scenarios for using snapshots are stateless VDI (Virtual Desktop Infrastructure), training VMs and short term test labs. Differencing VHDs provide us with deployment speed and great storage saving.
If you need to know more about creating Differencing disks, please see the following article;
Hyper-V Virtual Machine (VM) Parent-Child Configuration Using Differencing Disks
One of my outstanding questions regarding Snapshots and Diff Disks has been walking the chain of child and parents up to the top parent. Off course, you can use Hyper-V Management Console and use Inspect Disk option to find the parent but you might be looking for another method to programmatically walk the chain and show you the relationship of multiple differencing disks in a tree. This made me look at few WMI classes and write few lines of code.
The first step is to find the AVHD (Snapshot) or VHD (Diff Disk) that is the newest one in the directory. A simple dir (PowerShell alias for Get-ChildItem) and Sort-Object will take care of this.
The second step is to inspect the VHD and you can take care of it with GetVirtualHardDiskInfo method of Msvm_ImageManagementService WMI class under Root\Virtualization namespace
Get-WmiObject –ComputerName “Localhost” -Namespace root\virtualization -Class Msvm_ImageManagementService
The output of this method, shows the parent of virtual hard disk being inspected in XML format. You can retrieve the parent virtual disk name and use it as the input for the next round of check. like a recursive loop.
So, I created the following function to take care of this repetitive task;
Function Inspect-DiffVHD([string]$VHDPath,[string]$Server = ".")
{
$HyperVNamespace = "root\virtualization"
$Server = "."
$ImageManagementServiceName = "Msvm_ImageManagementService"
$ImageManagementService = Get-WmiObject -ComputerName $Server `
-Namespace $HyperVNamespace -Class $ImageManagementServiceName
$result = $ImageManagementService.GetVirtualHardDiskInfo($vhdPath)
$VHDInfo = [xml]$result.info
$VHDInfo.INSTANCE.PROPERTY[4].value
}
The attached sample PowerShell script would generate the following output when pointed to a folder containing differencing VHDs (.VHD) or Snapshots (.AVHD). (The sample script assumes the original Child/Parent relationship hasn’t been altered).
Sample output;
Enter Path: D:\Hyper-V\Test
D:\hyper-v\test\test_72A34A5A-0B86-4858-9663-CCE3AD9828A2.avhd
D:\Hyper-V\test\test_9330B5ED-248D-442D-9705-F7A320F7AC62.avhd
D:\Hyper-V\test\test_F6BC8CA6-BC70-4D02-9D6D-5E3C7CAB9D6E.avhd
D:\Hyper-V\test\test_84D8C8BC-1723-49DB-92D8-CEC1752ADC3F.avhd
D:\Hyper-V\test\test.vhd
Please note that I didn’t get a chance to test my sample script against multiple tree snapshot scenario but you should be able to do this with a simple modification.
Hope you find this post helpful. So long…