[Ovirt-devel] Useful VM cleanup tool...
Darryl L. Pierce
dpierce at redhat.com
Thu Apr 23 19:57:25 UTC 2009
If anybody's interested, here's a script I whipped up while working on
autotests. It takes as input a set of VM names and destroys then
undefines them. It also removes any network interfaces associated with
them (can be overridden with "-n"). So if you've defined a bunch of VMs
and don't want to manually deal with them, this script can be of use to
you.
--8<[snip]---
#!/bin/bash
#
# Destroys and undefines a virtual machine and all of its elements.
ME=$(basename "$0")
warn() { printf '%s: %s\n' "$ME" "$*" >&2; }
die() { warn "$*"; debug "Dying..."; exit 1; }
debug() { if $debugging; then log "[DEBUG] %s" "$*"; fi }
trap '__st=$?; exit $__st' 0
trap '__st=$?; exit $__st' 1 2 13 15
set -e
set -u
usage () {
cat <<EOF
Usage: $ME NAME
Completely removes the specified virtual machine, and all virtual peripherals, such as
network devices and hard disks.
-n: do not undefine networks (default: undefine networks)
-d: display debugging output (default: off)
-h: display this help and exit
NAME - The name of the virtual machine to remove.
EOF
}
log () {
printf "$(date): $*\n"
}
# $1 - virtual machine name
verify_vm_exists () {
local name=${1-}
local vm=$(sudo virsh list --all | awk '/'"$name"'/ { print $2 }')
if [[ "$vm" == "$name" ]]; then
log "Domain found: ${name}"
else
log "Domain does not exist: ${name}"
die "No such vm: ${name}";
fi
}
# $1 - virtual machine name
destroy_vm () {
local name=${1-}
local xml=$(sudo virsh dumpxml $name)
if [[ "running" == $(sudo virsh list | awk '/'"$name"'/ { print $3 }') ]]; then
log "Shutting down \"${name}\"."
sudo virsh destroy $name > /dev/null 2>&1
fi
debug "include_networks=${include_networks}"
if $include_networks; then
networks=$(echo $xml|awk '/source network=/ {match($0,"network='"'"'(\\w*)'"'"'",data); printf data[1];}')
debug "networks=${networks}"
for network in $networks; do
if [[ "active" == "$(sudo virsh net-list|awk '/^'"$network"'/ { print $2 }')" ]]; then
log "Shutting down network: ${network}"
sudo virsh net-destroy $network > /dev/null 2>&1
fi
log "Undefining network: ${network}"
sudo virsh net-undefine $network > /dev/null 2>&1
done
else
debug "Skipping networks"
fi
log "Undefining ${name}."
sudo virsh undefine $name > /dev/null 2>&1
}
vm_name=''
debugging=false
include_networks=true
while getopts nvdh c; do
case $c in
n) include_networks=false;;
d) debugging=true;;
v) set -v;;
h) usage; exit 0;;
'?') die "invalid option \`-$OPTARG'";;
:) die "missing argument to \`-$OPTARG' option";;
*) die "internal error";;
esac
done
shift $(($OPTIND -1))
if [ $# -le 0 ]; then
die "You must specify at least one virtual machine"
fi
for vm in $*; do
verify_vm_exists $vm
destroy_vm $vm
done
---8<[snip]---
--
Darryl L. Pierce, Sr. Software Engineer @ Red Hat, Inc.
Virtual Machine Management - http://www.ovirt.org/
Is fearr Gaeilge bhriste ná Béarla cliste.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/ovirt-devel/attachments/20090423/da971890/attachment.sig>
More information about the ovirt-devel
mailing list