virsh-snap.sh: create and revert external libvirt snapshots easily

There are two types of disk snapshots in libvirt: internal and external. Internal snapshots (virsh default) store all data in one file. With external snapshots, base data and snapshot data is stored separately: base disk file is left intact, and all changes are written into a separate snapshot overlay file. For me, this is the way 😉 Unfortunately, external snapshots support in virsh is limited, virt-manager GUI can't create / use them, and reverting external snapshots involves manual editing config xml to change disk file path.

The manual xml editing part was what irritated me most, so I figured I'd just save VM config with virsh dumpxml before creating a snapshot, and then restore it with virsh define if / when I'd need to revert. Then I decided to wrap this and other routine commands needed to work with external snapshots into a script, virsh-snap.sh.

Installation

  • Download script:

    wget -P ~/.local/bin https://github.com/shvchk/virsh-external-snapshots/raw/main/virsh-snap.sh
    
  • Make it executable:

    chmod +x ~/.local/bin/virsh-snap.sh
    

Usage

  • Create snapshot named snapshot-1 of vm-42:

    virsh-snap.sh create vm-42 snapshot-1
    

    Snapshot name is optional, by default current date and time is used as a name, e.g. 2022.07.29_23.55.12.

  • List snapshots of vm-42:

    virsh-snap.sh list vm-42
    

    Note that this is not the same as virsh snapshot-list. Snapshots are created without metadata and won't be shown with virsh snapshot-list.

  • Revert and delete snapshot-1 of vm-42:

    virsh-snap.sh revert vm-42 snapshot-1
    
  • Revert, but don't delete snapshot-1 of vm-42:

    virsh-snap.sh soft-revert vm-42 snapshot-1
    

    This allows to use this snapshot later with unrevert.

  • Undo revert of a snapshot-1 of vm-42, i.e. make it active again:

    virsh-snap.sh unrevert vm-42 snapshot-1
    
  • Delete snapshot snapshot-1 of vm-42:

    virsh-snap.sh delete vm-42 snapshot-1
    

    Might be useful if you change your mind after soft-revert.

  • Show help:

    virsh-snap.sh help
    
    virsh-snap.sh: create and revert external libvirt snapshots easily
    
    Usage: virsh-snap.sh <action> <domain> [snapshot name]
    
    
    Actions:
    --------
    create, c           Create snapshot
                        If no snapshot name is provided, current date and time is used
    
    list, ls, l         List snapshots
    
    disk                Show active disk
    
    revert, rev, r      Revert snapshot and delete it (same as soft-revert + delete)
    
    soft-revert,
    srev, sr            Revert snapshot without deleting it (allows unrevert)
    
    unrevert,
    unrev, ur           Unrevert snapshot, i.e. make soft-reverted snapshot active again
    
    delete, del, rm     Delete snapshot
    
    help, h             This message
    
    
    Snapshots are created without metadata, so they won't be shown with `virsh snapshot-list`