Recovering Your Virtual Machine. How to Manually Merge Hyper-V Snapshots Back into one VHD
The Problem
A virtual machine has be shut down in some way, and upon rebooting all the changes since data/time X have been lost. After viewing the file system you find that the VHD file’s last modified date is X time in the past, you also find that under the Snapshots directory that a .avhd file exists with the current date/time, holding all your changes to the original Virtual Machine file.
Understanding Hyper-V Snapshots
So what is a Snapshot exactly. A Snapshot in Hyper-V is basically a spot in time where the current running configuration of the Virtual Machines is saved to a Snapshot Differencing Disk file (AVHD), from which you can return to from the future. This point in time is linked back to the original VHD Base disk. When you take another Snapshot it links to the first snapshot which links the the Base VHD (like a daisy chain).
When you take a Snapshot the Base VHD is no longer modified. Now if you had multiple Snapshots and you reverted to one somewhere in the middle, everything after this Snapshot was taken is destroyed (because you have altered the future. Like back to the future when Marty buys the sports almanac and future biff, goes back in time and gives the almanac to past biff. Future biff has altered the future and although not destroyed in the movie it was defiantly a screwed future
)
Manually Merging the Hyper-V Snapshots into a Single VHD file and Getting your Virtual Machine Back
First things first you need to locate the Snapshot Differencing Disk file, the extension is .avhd. You should find this in your VMStorage\VM\Snapshot\LongID or something similar depending on how your store your Virtual Machines.
Once you’ve found the .avhd file, change the extension to .vhd
Once you’ve changed the extension, from the Hyper-V MMC select the virtual machine and from theActions menu select Edit Disk…
On the Edit Virtual Hard Disk Wizard screen, click Next
On the next screen browse the the Snapshot Differencing Disk file you just renamed.
On the next screen select the Merge option.
Select the option To the parent virtual hard disk.
On the complete screen review your selections and click Finish.
The Editing the virtual disk… status dialog box will show up.
Create a new virtual machine, select new -> Virtual Machine… from the Actions menu
Select the name and path as normal.
Select the amount of memory as normal.
Select network as normal.
At the hard disk screen, select Use an existing virtual hard disk. Then navigate to the VHD file from the original Virtual Machine (now merged with the Snapshot Differencing Disk File).
Review your selections as normal and click Finish.
You should now boot into your Virtual Machine and if everything has worked correctly it should contain all the changes made that were in the Snapshot Differencing Disk file, which you have merged back together with the main VHD file.
Updated: Merging Multiple Snapshots/AVHD Files Back Into One VHD
A small update to the above procedure. If you have taken multiple snapshots and therefor have multiple AVHD files, the process is slighly more complex, however not that hard. To merge muliple AVHD files into one VHD you need to do the above procedure working in reverse for each AVHD file created (latest to oldest).
First determine the current/last AVHD file. This will be the last snapshot taken and the file the current changes are being written to. To do this locate the Virtual Machine config file, normally infolder\VM\Virtual Machines\random-string.xml.
Once you’ve located the config file, open it up and locate the location of the last AVHD file as shown below:
Follow the above procedure for merging the AVHD file into the parent disk. (note: you dont need to create the new Virtual Machine at this point.)
Once the merge is complete, I found it was easy to idenify the next AVHD file in the chain by going to the folder\VM\Snapshots directory. Arrange the files/folder by last modified. Go to the Latest XML config file and locate the next AVHD, perform the same merge process. Then goto the next latest XML file.
Do this for each AVHD file until you have one remaining VHD. Once you’ve reached this point follow on from above creating a new Virtual Machine using the new merged VHD file.
Other Hyper-V Tips
- Never save state on a Domain Controller doing so can cause domain synchronisation issues.
- Never pause a Domain Controller, this can cause replication issues.
- Do not take Snapshots of Domain Controllers.
- Use Fixed size disks, this will improve performance, and help reduce disk fragmentation.
- Always defragment a physical disk before creating a virtual hard disk.
- Be mindful of the integration services affect on a Domain Controller. If you provide the time synchronisation service to a Domain Controller, you can cause time synchronisation issues in your domain.
- Dont expand the Virtual Hard Disk if you have snapshots which haven't merged with the main VHD. This will make it impossible to remerge them.
- If you cannot merge your snapshot (avhd) files back into the main VHD. You can attempt to use WinImage (http://www.winimage.com) to retrieve the data from within the snapshot file. To do this simply rename the snapshot file from string.avhd to string.vhd then open with winimage.
- Remember to uninstall the VM Additions before migrating Virtual Machines from Virtual PC or Virtual Server 2005 R2, otherwise you will receive the following error when you try to uninstall them from inside Hyper-V:
{\Tahoma8}You can install Virtual Machine Additions only on a virtual machine that is running a supported guest operating system




