A snapshot is reproduction of the virtual machine just as it was when you took the snapshot. The snapshot includes the state of the data on all virtual machine disks and the virtual machine power state (on, off, or suspended). You can take a snapshot when a virtual machine is powered on, powered off, or suspended.
When you create a snapshot, the system creates a delta disk file for that snapshot in the datastore and writes any changes to that delta disk. You can later revert to the previous state of the virtual machine.
The VirtualMachine object has methods for creating snapshots, reverting to any snapshot in the tree, and removing snapshots.
Virtual Machine Snapshots
Snapshot hierarchies can become fairly complex. For example, assume that, in the example in Virtual Machine Snapshots, you revert to snapshot_a. You might then work with and make changes to the snapshot_a virtual machine, and create a new snapshot, creating, in effect, a branching tree.
Creating a Snapshot
The VirtualMachine.CreateSnapshot_Task method creates a new snapshot of a virtual machine. As a side effect, the current snapshot becomes the parent of the new snapshot.
The method allows you to specify a name for the snapshot and also requires you set the memory and quiesce properties:
memory – If true, a dump of the internal state of the virtual machine (basically a memory dump) is included in the snapshot. Memory snapshots consume time and resources, and take a while to create. When set to false, the power state of the snapshot is set to powered off.
quiesce – If true and the virtual machine is powered on when the snapshot is taken, VMware Tools is used to quiesce the file system in the virtual machine. This ensures that a disk snapshot represents a consistent state of the guest file systems. If the virtual machine is powered off or VMware Tools is not available, the quiesce flag is ignored.
The example calls this method as follows:
ManagedObjectReference taskMor = service.createSnapshot_Task(
vmMor, snapshotName, desc, false, false);
The method returns MOR to a Task object with which to monitor the operation. The info.result property in the Task contains the newly created VirtualMachineSnapshot upon success.
Reverting to a Snapshot
When you revert to a snapshot, you restore a virtual machine to the state it was in when the snapshot was taken. The VirtualMachine.RevertToSnapshot_Task allows you to specify a target host and whether the virtual machine should be powered on.
If the virtual machine was running when the snapshot was taken, and you restore it, you must either specify the host to restore the snapshot to, or set the SupressPowerOn flag to true.
Deleting a Snapshot
You can delete all snapshots by calling VirtualMachine.RemoveAllSnapshots or by calling the VirtualMachineSnapshot.RemoveSnapshot_Task method. The VirtualMachineSnapshot object was previously returned in the task returned by the CreateSnapshot_Task method.