#!/bin/sh # Shutdown running KVM guests # copied from http://launchpadlibrarian.net/56254248/libvirt-bin.conf URIS='qemu:///system' SHUTDOWN_TIMEOUT=300 run_virsh() { # We parse the output for things like domain state; # make sure the output is in the language we expect. LANG=C virsh "$@" } for uri in $URIS; do for domain in $(run_virsh -c "$uri" list | awk '$3 == "running" {print $2}'); do run_virsh -c "$uri" shutdown "$domain" >/dev/null done done delay=$SHUTDOWN_TIMEOUT while [ $delay -gt 0 ]; do for uri in $URIS; do if ! run_virsh -c "$uri" list | awk '$3 == "running" {exit 1}'; then # VMs at this URI are still running. Wait, then # start at the beginning looking for running VMs. sleep 1 delay=$(($delay - 1)) continue 2 fi done break done for uri in $URIS; do for domain in $(run_virsh -c "$uri" list | awk '$3 == "running" {print $2}'); do run_virsh -c "$uri" destroy "$domain" >/dev/null done done exit 0