How does Virtual Machine snapshotting work

virtualization

Having used drive imaging software for over a decade, I find it mind boggling that Virtual Machines can be snapshotted and restored in a matter of seconds, while drive imaging often takes hours.

I can snapshot a VM, reinstall to a different OS, then do a restore and somehow magically within seconds my old VM is back up perfectly in the state it was previously in.

How is this able to happen? What does the VM host actually do to the VM that makes this possible?

Best Answer

When you create a snapshot, all changes made on the initial virtual disk image are not actually made on the image itself, but they are written to a new (snapshot) disk file. This action is so fast because there is no need to copy whole virtual disk image, because it works on copy on write principle (only changed, i.e. written blocks are written to snapshot image). Note that snapshot image grows as you change more and more data on your original virtual disk image (which stays as it was on the moment you took the snapshot). It will most probably be much smaller than original image, but in the worst case it will be exactly the same size (if all blocks were changed).

There are two actions you can make with this new snapshot image:

  1. Discard snapshot: very fast action. Virtual machine manager simply deletes snapshot image file and returns control onto the original disk image or some of the previous snapshots
  2. Merge snapshot: virtual machine manager merges changed blocks (i.e. snapshot image) with original image. This action will take time depending on how much blocks from original image have changed. Again, worst case scenario is that whole original image will be overwritten in case that all blocks from original image have been written after creation of a snapshot.

All procedures described above also work for multiple snapshots. In that case original image may be one snapshot, and next snapshot may reference block on that (first) snapshot. This way you can have many snapshots you can discard or merge with ease.