Restoring Hyper-V VM with snapshot tree

For whatever reason, you’ve lost your virtual machine settings in Hyper-V and it just so happened they are snapshots. I sure hope that this was a test server and not a production one! Tsktsk! By now, you’ve probably scoured the interweb searching for a cure, and you’re in luck, you’ve found a way to get back your snapshot tree instead of just merging the disks! Oh joy!

It would seem everyone out there is happy with just merging their VHDs and getting back to work, but not me, no sir! I want things back the way they were! It took me about half a day to get this, so hopefully, this will save you some time for more important things, like tetris!

Steps

  1. Create same number of pre-existing snapshots using the Hyper-V MMC.
  2. Replace new snapshots with old snapshots while renaming the old to the same as the new, except the file extension should be “vhd” instead of “avhd”. The reason we’ll see later, but the Hyper-V tool to repair VHD chains auto-appends .vhd! (Grr Hyper-V Team, grr!)
  3. Repair the vhd’s. Because of our renaming we’ve broken the differencing chain. To fix this, go to open Hyper-V, go to the VM’s Settings and then the hard drive. Change the VHD to the new one (simply changing the extension should work) and click “inspect disk”. This will walk you through fixing the chain.
  4. Modify the ACL to include the VM specific account. This you must to in Powershell since the Explorer GUI can’t seem to find the account. I don’t know why and there may be a way, but I’m to lazy to dig into it and this works. (Below you can find some sample code.) The account to add from the old VHD, is the one that starts with “NT VIRTUAL MACHINE” and ends with a GUID.
    ##START POWERSHELL CODE
    #get the old vhd's permissions
    $vm_perm = (get-acl .\old.vhd).access | where { $_.IdentityReference.tostring().contains("NT VIRTUAL MACHINE") }
    
    #Load the new vhd's acl into memory
    $newvhd = get-acl .\new.vhd
    
    $newvhd.AddAccessRule($vm_perm)
    
    $newvhd | set-acl .\new.vhd
    ##END POWERSHELL CODE
  5. Open all the VM’s XML setting files and make sure that any settings related the the VHD’s is correct. This should only be the filename. If you need to edit these files be sure to turn off the Hyper-V Management Service, this will not affect the VM’s running, only the managing of them. The XML files can be found wherever you made the VM.
  6. You should now have a restored VM with the snapshot tree! Woo! Go you!
You can leave a response, or trackback from your own site.

Leave a Reply