James O'Neill's blog

Windows Platform, Virtualization and PowerShell with a little Photography for good measure.

Hyper-v and Snapshots (Part 1)

Hyper-v and Snapshots (Part 1)

  • Comments 5
  • Likes

We often talk about "rolling back" to a snapshot, but here, some of the snapshots we can apply aren't simply forward or backward, hence Hyper-V talks about applying snapshots.

It also talks about Deleting snapshots which causes some confusion. Deleting a snapshot means foregoing the ability to return to that point - we can't apply a deleted snapshot, that's obvious enough. The saved memory state (if there is one) is deleted but what about the data in the AVHD file ? If a Snapshot has children we can either delete the whole subtree, or we can delete just the parent snapshot. Internally Hyper-v works out when it can merge and/or delete AVHD files - a process which can take some time (you can tell if this operation is pending because the edit button for the hard disk in the machine's settings is grey'd out). 

Here's an example The machine is running quite happily and has never been snapped. Late on Monday make a snapshot. This does two thingsHyper-V-Snapshots

1.If the Machine is running or in a saved state we make a copy of memory

2. We stop writing changes to its VHD and start writing changes to a new AVHD file. (Lets call this AVHD-1)

At any point when we want to revert, we go back to the original hard disk and the Monday memory state. Throughout Tuesday changes are written to the AVHD-1then on Tuesday night we do another Snapshot. The same thing happens

1.If the Machine is running or in a saved state we make a copy of memory

2. We stop writing changes to AVHD-1 file and start writing changes to a new AVHD file. (Lets call this one AVHD-2)

Now we can revert to two points, Monday's memory state and the original VHD or Tuesdays memory state and the combination of the original VHD and AVHD-1

Lets assume that on Wednesday something happens to cause us to go back to Monday's state. We can either (a) Keep AVHD-2 and save the memory state as it was on Wednesday or (b) Discard AVHD-2 and memory state. Either way the server now starts a new AVHD file - lets call this one AVHD3. Thursdays changes get written to this, and on Thursday night as before we do another snapshot and start AVHD4 for Friday's changes and keep the memory state as it was on Thursday.

Now we can apply 3 states (or 4 if we kept Wednesday's).

Now for the obligatory bit of powershell, because of course this is scriptable.

   Function New-VmSnapshot  
{Param( $vm=$(Throw "You must specify a VM") )
if ($vm -is [string]) {$vm=(Get-VM $vm) }

$arguments=($vm,$Null,$null) $result=$VSMgtSvc.psbase.invokeMethod("CreateVirtualSystemSnapshot",$arguments) if ($result -eq 4096) { $arguments[2] }  else  {"Error, code:" + $result}
}

[Update. A bit of PowerShell 2.0 crept into the above. In 1.0 you can't call the .InvokeMethod  method of a WMI object directly, you have to call it via .psbase]

In an earlier post I described Get-VM and explained that I set up a variable

   $vsMgtSvc = Get-wmiObject -nameSpace root\virtualization -class Msvm_virtualSystemManagementService

All the work here is done by the CreateVirtualSystemSnapshot method, and we pass it the Machine and 2 nulls. Normally it will return 4096 - the code for "started processing in the background" and the second Null magically contains the job ID so the function returns that. So you can invoke the function as $JobId=(New-VmSnapshot  $Tenby) and make tracking the job afterwards that bit easier. There are two more methods which we can call, RemoveVirtualSystemSnapshot, and ApplyVirtualSystemSnapshot oddly the latter only requires the machine and the snapshot reference, even though the snapshot says which machine it came from but trying to apply a snapshot to a new, clean VM fails. Maybe it's something that's under consideration for a future version. I'll describe these two in my next post.

 

[Update somehow the order of the paragraphs got scrambled, they're back in the right order now]

Comments
  • In my last post I explained how snapshots work and gave a little bit of PowerShell for creating a one

  • In which we see how to set the number of CPUs I started with getting MSVM Computer System objects - which

  • (updated 7/23/08) My current project involves being the only dedicated technical resource on the Virtualization

  • (updated 9/26/08) My current project involves being the only dedicated technical resource on the Virtualization

  • 下面的内容,是由微软全球技术中心工程师收集和整理的虚拟化技术资料集锦。基本上涵盖了学习和应用基于Hyper-V架构的虚拟化平台所需要掌握的知识和资料。非常的有价值。 1 .Hyper-V的安装部署 在Windows

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment