'how could "virsh snapshot-list" with description
virsh snapshot-create-as win7 snap01 --description "something here"
How could I list snapshots list with the description ?
Solution 1:[1]
You can get the description from the snapshot xml dump:
virsh snapshot-dumpxml --domain {domain name} --snapshotname {snapshot name}
Solution 2:[2]
You can produce a list of all machines with information about their snapshots including the description with this little script.
#!/bin/sh
#\
exec sudo tclsh "$0" "$@"
# List KVM snapshots of all machines (domains) including their description
# [email protected] 2020-08-21
package require tdom
### Acquire list of machines (domains) from "virsh list --all"
set machines ""
foreach machineInfo [lrange [split [exec virsh list --all] \n] 2 end-1] {
set name [string trim [string range $machineInfo 7 37]]
set state [string trim [string range $machineInfo 38 end]]
dict set machines $name state $state
} ;# foreach
### Acquire list of snapshots for all machines (name, time and description)
foreach m [dict keys $machines] {
foreach snapshot [lrange [split [exec virsh snapshot-list --domain $m] \n] 2 end-1] {
set name [string trim [string range $snapshot 1 21]]
set xmlRoot [[dom parse [exec virsh snapshot-dumpxml --domain $m --snapshotname $name]] documentElement]
set descr [[$xmlRoot selectNodes /domainsnapshot/description/text()] data]
set creaTime [clock format [[$xmlRoot selectNodes /domainsnapshot/creationTime/text()] data] -format {%Y-%m-%d %H:%M}]
dict set machines $m snapshots $name time $creaTime
dict set machines $m snapshots $name descr $descr
} ;# foreach snapshot
} ;# foreach machine
### Output a list of all machines with their snapshots including time and description
foreach m [dict keys $machines] {
puts [format "\nMACHINE '%s' (%s)" $m [dict get $machines $m state]]
catch {unset snapshots}
dict with machines $m {
if [info exists snapshots] {
foreach sn [dict keys $snapshots] {
puts " SNAPSHOT '$sn', created: [dict get $snapshots $sn time]"
foreach line [split [dict get $snapshots $sn descr] \n] {
puts " $line"
}
} ;# foreach snapshot
} ;# if snapshot exists
} ;# dict with
} ;# foreach machine
puts ""
Solution 3:[3]
I managed to do a simple bash script to list all VMs and the snapshots description:
#!/bin/bash
vmdomains=`sudo virsh list --all | sed 1,2d | awk '{ print $2 }'`
echo -n "Domain\t\t\tSnapshot Name\t\t\tDescription\n"
echo "-----------------------------------------------------------------------------------------"
for vmdomain in $vmdomains; do
echo -n "$vmdomain\t\t"
snapshots=`sudo virsh snapshot-list $vmdomain | sed 1,2d | awk '{ print $1 }'`
snapshotflag=0
for snapshot in $snapshots; do
snapshotflag=1
echo -n "$snapshot\t\t"
sudo virsh snapshot-dumpxml --domain $vmdomain --snapshotname $snapshot | grep "<description>" | sed 's/ <description>//' | sed 's/<\/description>//'
done
if [ $snapshotflag -eq 0 ]; then
echo
fi
done
Edit: Output example
~$ sh listsnap.sh
Domain Snapshot Name Description
-------------------------------------------------------------------------------------
Win10 Win10_Disabled_Updates Used gpedit to disable updates
Win10 Win10_games_installed Gaming Rig with passtrhough gpu
CentOS CentOS_origin Installed Nginx and web dependencies
CentOS CentOS_Wednesday Installed Anaconda and Julia
CentOS CentOS_weekend Updated PIP dependencies
Fedora
Ubuntu Testing_nfs_Ubuntu Configured nfs server
Solution 4:[4]
I tried Holger Jakobs' solution but it required packages not on my Linux system.
Then I tried Faustino Aguilar's solution and it almost works. However, it is legal to embed spaces in the snapshot name and his script doesn't handle that case. I also had serious formatting problems for snapshots that didn't include a description.
So, I made significant modifications and believe I have a working script with the caveat that a domain name or a snapshot name over 20 characters won't work as desired without modifying the script.
#!/bin/bash
#
# Prints snapshots with descriptions for virsh.
#
# Caveats
# This script has undefined behavior if snapshot name > 20 characters.
# (If that is a problem, consider changing "formatPre" and "cut -c2-21" below.)
vmdomains=`virsh list --all | sed 1,2d | awk '{ print $2 }'`
formatPre="%-21s%-21s"
format="$formatPre %s\n"
printf "$format" "Domain" "Snapshot Name" "Description"
printf "$format" "--------------------" "---------------------" "--------------------------------------"
SAVE_IFS="$IFS"
IFS=$(echo -en "\n\b")
for vmdomain in $vmdomains; do
snapshots=`virsh snapshot-list $vmdomain | sed 1,2d | cut -c2-21 | sed 's/ *$//'`
for snapshot in $snapshots; do
printf "$formatPre " "$vmdomain" "${snapshot}"
description=`virsh snapshot-dumpxml --domain "$vmdomain" --snapshotname "${snapshot}" | grep "<description>" | sed 's/ <description>//' | sed 's/<\/description>//'`
echo "$description"
done
done
IFS="$SAVE_IFS"
Sample output. (Not all my snapshots have descriptions.)
Domain Snapshot Name Description
-------------------- --------------------- --------------------------------------
ps111 0-init
ps111 1-setup
ps111 2-named
ps111 3-python
ps111 4-tmp
skit20 00initial initial
skit20 01setup apt update/upgrade + vim + ssh
skit20-clone 00initial initial
train20 01 Initial
train20 02 LinuxBase
watchdog20 00initial initial
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Toasted Salad |
Solution 2 | Holger Jakobs |
Solution 3 | |
Solution 4 |