virsh snapshot-create-as win7 snap01 --description "something here"
我如何列出带有描述的快照列表?
我设法做了一个简单的 bash 脚本来列出所有虚拟机和快照描述:
#!/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
编辑:输出示例
~$ 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
您可以生成所有机器的列表,其中包含有关其快照的信息,包括使用这个小脚本的描述。
#!/bin/sh
#\
exec sudo tclsh "$0" "$@"
# List KVM snapshots of all machines (domains) including their description
# Holger@Jakobs.com 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 ""
您可以从快照 xml 转储中获取描述:
virsh snapshot-dumpxml --domain {domain name} --snapshotname {snapshot name}