Saving and restoring your lab's state

Video 015 of 140 in Virtualization Foundations, in the unit “Setting up your lab machine”. 2:53, with English and Hindi audio.

Practice means breaking things, and a lab is only useful if you can recover from mistakes. A snapshot saves the state of a virtual machine at one moment, so you can return to it later. This article explains what a snapshot does, why it is not a backup, and how to take, list, revert and delete one with the virsh tool. It also covers the costs of keeping old snapshots.

What you will learn

What a snapshot does and what it is not

A snapshot is a saved point in a virtual machine. You can return to it, and every change made after it is undone. Think of a game that saves before a hard level. If you fail, you start again from that save, not from the beginning.

Imagine you want to upgrade a package on a practice virtual machine. You take a snapshot first, then try the upgrade. If the upgrade breaks the boot, you revert to the snapshot, and the machine returns to exactly the state it had at that moment.

Note: A snapshot is not a backup. It sits on the same disk, so if that disk fails, the snapshot is lost with it. Keep real backups of anything you cannot rebuild.

How to take, list, revert and delete a snapshot with virsh

Take the snapshot with virsh, and give it a name that says why it exists. In the examples, lab-vm stands for your practice virtual machine, and before-upgrade is the name of the snapshot. Use the name of your own machine.

Take a named snapshot of the practice machine
virsh snapshot-create-as --domain lab-vm --name before-upgrade

The list command shows the snapshots you have for that machine, with their names and times. Use it to check the name before you return to one.

List the snapshots of the practice machine
virsh snapshot-list lab-vm

To return, name the machine and the snapshot. Everything changed after that point is undone. Revert discards every change made after the snapshot, so read the names twice before you run it.

Return the machine to the snapshot
virsh snapshot-revert lab-vm before-upgrade

When a snapshot is no longer needed, delete it, so that it stops holding old data.

Delete the snapshot once it is no longer needed
virsh snapshot-delete lab-vm before-upgrade

Why old snapshots cost space and speed

Take, list, revert and delete a snapshot

You need a practice virtual machine that virsh can see, and access to virsh on the lab machine. The examples use the name lab-vm. Run the commands in this order, and use the name of your own machine in place of lab-vm.

  1. Take a snapshot before a risky change, and give it a name that says why it exists.

    virsh snapshot-create-as --domain lab-vm --name before-upgrade

    What you should see: The snapshot before-upgrade now exists for the practice machine.

  2. List the snapshots of the machine, to check the name and the time before you return to one.

    virsh snapshot-list lab-vm

    What you should see: before-upgrade appears in the list, with its name and time.

  3. Return the machine to the snapshot. Everything changed after it is undone, so check the names first.

    virsh snapshot-revert lab-vm before-upgrade

    What you should see: The machine is back in the state it had when you took the snapshot.

  4. Delete the snapshot once it is no longer needed, so it stops holding old data.

    virsh snapshot-delete lab-vm before-upgrade

    What you should see: The snapshot is removed from the list, and its old data no longer takes up space.

Terms

Snapshot
A saved point in a virtual machine that you can return to, undoing every change made after it.
Backup
A separate copy of data, kept on a different disk or place, so the data survives if the original fails.
virsh
The command line tool that talks to libvirt and manages virtual machines, including their snapshots.

Check yourself

Why is a snapshot not a backup?

It sits on the same disk as the machine, so if that disk fails, the snapshot fails with it.

What does snapshot-revert do?

It returns the virtual machine to the saved snapshot and undoes every change made after it.

What are the costs of keeping old snapshots?

They keep old data, so the disk fills up, and long chains of snapshots can make disk work slower.

Previous: Downloading the Ubuntu cloud image. Next: A cloud machine instead of a laptop.