Tuesday, December 13, 2011

Manually merging shapshots in differencing VHDs using Diskpart

Yesterday I had to move a stopped VM off a Hyper-V server that had run out of disk space. The server had 2 ongoing snapshots dating back to 2009. The snapshots had to be merged before I could restart the server and it wasn't immediately obvious how to do it.

Eventually I rediscovered DiskPart which is a frontend to the Logical Disk Manager in Windows. I'd used this before for special partition trickery on USB keys: in 7 and 2008 Server this includes vdisk (VHD) functionality as well.

To merge the snapshots first I Selected the last snapshot in the chain, then told DiskPart to merge with the parent. Differencing VHDs store a relative and an absolute path to their parent, so the disk structure needs to be approximately the same:

DISKPART> select vdisk file=d:\vm\SERVERNAME\Snapshots\3209A29B-8693-45CC-A9E4-444A9CE3B245\\3209A29B-8693-45CC-A9E4-444A9CE3B245.avhd
DISKPART> merge vdisk depth=2

I used depth=2 because there were two snapshots in my chain. It took a while for the merge to complete during which the file size on the parent VHD was incrementing, and then a little longer for the disk management service to release its lock on the files, but after that I was able to delete the snapshot files and boot the VHDs in a new VM on the new server.