How to Manage KVM Snapshots with virsh

Snapshots are a powerful feature of KVM that allow you to capture the state of a virtual machine (VM) at a specific point in time. This capability is valuable for system administrators and developers who need to create temporary backups, test changes, or recover from errors. In this lesson, we’ll explore how to manage KVM snapshots using the virsh command-line tool on a Linux server.

Prerequisites

Before you start, ensure that:

  • You have a KVM hypervisor installed on your Linux server.
  • virsh is installed and configured on your system.

Creating snapshots

Creating a snapshot captures the current state of the VM, including its disk and optionally its memory. This allows you to revert to this state at any time.

  1. List existing VMs:
    First, identify the VM you want to snapshot by listing all VMs:
   virsh list --all
  1. Create a snapshot:
    Use the virsh snapshot-create-as command to create a snapshot. Replace <domain> with your VM’s name and <snapshot-name> with a name for your snapshot:
   virsh snapshot-create-as <domain> <snapshot-name> \
     --description "<description>"

Example:

   virsh snapshot-create-as myvm snapshot1 --description "Pre-update snapshot"
  1. Verify the snapshot:
    List the snapshots for the VM to ensure it was created successfully:
   virsh snapshot-list <domain>

Example:

   virsh snapshot-list myvm

Reverting to snapshots

Reverting to a snapshot restores the VM to the state it was in when the snapshot was taken. This is useful for undoing changes or recovering from issues.

  1. Identify the snapshot:
    List all snapshots for the VM to find the one you want to revert to:
   virsh snapshot-list <domain>

Example:

   virsh snapshot-list myvm
  1. Revert to the snapshot:
    Use the virsh snapshot-revert command to revert to the desired snapshot:
   virsh snapshot-revert <domain> <snapshot-name>

Example:

   virsh snapshot-revert myvm snapshot1

Deleting snapshots

Deleting a snapshot frees up storage and removes the snapshot from the list of available states to revert to.

  1. List snapshots:
    List the snapshots for the VM to find the one you want to delete:
   virsh snapshot-list <domain>

Example:

   virsh snapshot-list myvm
  1. Delete the snapshot:
    Use the virsh snapshot-delete command to remove the snapshot:
   virsh snapshot-delete <domain> <snapshot-name>

Example:

   virsh snapshot-delete myvm snapshot1

Understanding snapshot implications

  • Reverting: When you revert to a snapshot, the VM’s state, including its disk and memory, is restored to the point when the snapshot was taken. Any changes made after the snapshot are lost.
  • Deleting: If you delete a snapshot, the snapshot data is removed, and you cannot revert to it again. If you revert to a snapshot and then delete it, the VM continues running in the reverted state, but the snapshot is no longer available for future use.

Managing KVM snapshots with virsh on a Linux server is an essential skill for system administrators and developers. Snapshots provide a safety net, allowing you to experiment, update, and manage your VMs with confidence. By following the steps outlined in this lesson, you can easily create, revert to, and delete snapshots, ensuring your virtual environments are always in a reliable state.

For further reading and advanced options, refer to the official libvirt documentation.

If you want to discuss the topic with other technology-minded people, join my Discord: https://discord.gg/YbSYGsQYES

Now we have an IRC channel as well: irc.libera.chat / #tomsitcafe

One thought on “How to Manage KVM Snapshots with virsh

Leave a comment