Created
April 18, 2010 19:00
-
-
Save fukusaka/370474 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#SAVEVMDIR="/var/lib/libvirt/save" | |
#LISTVM_SHUTDOWN="linux_sid linux_fedora" | |
#LISTVM_SAVE="freebsd_cur netbsd_cur solaris" | |
VIRSH=${VIRSH:-/usr/bin/virsh} | |
SAVEVMDIR=${SAVEVMDIR:-/var/lib/libvirt/save} | |
TIMEOUT_SHUTDOWN=${TIMEOUT_SHUTDOWN:-900} | |
usage() { | |
echo "$0: {start|stop}" | |
} | |
inword() { | |
word=$1; shift; list="$*" | |
echo $list | tr " " "\n" | grep -q $word | |
} | |
list_running() { | |
$VIRSH list | awk 'NR>2 && NF==3 {print $2}' | |
} | |
savevm() { | |
[ -n "$1" -a -n "$SAVEVMDIR" ] || return | |
#[ -e $SAVEVMDIR ] || mkdir $SAVEVMDIR | |
$VIRSH save $1 $SAVEVMDIR/savevm.$1 | |
} | |
restorevm() { | |
[ -f $1 ] || return | |
$VIRSH restore $1 | |
retval=$? | |
if [ $retval -eq 0 ]; then | |
rm -f $1 | |
fi | |
return $retval | |
} | |
start() { | |
for file in $SAVEVMDIR/savevm.*; do | |
restorevm $file | |
done | |
} | |
stop() { | |
starttime=$(date +%s) | |
for vm in $(list_running); do | |
if inword $vm $LISTVM_SHUTDOWN; then | |
$VIRSH shutdown $vm | |
fi | |
done | |
for vm in $(list_running); do | |
if inword $vm $LISTVM_SAVE; then | |
savevm $vm | |
fi | |
done | |
while [ $(($(date +%s) - $starttime)) -le $TIMEOUT_SHUTDOWN ]; do | |
sleep 10 | |
countvm=0 | |
for vm in $(list_running); do | |
if inword $vm $LISTVM_SHUTDOWN; then | |
countvm=$(($countvm+1)) | |
$VIRSH shutdown $vm | |
fi | |
done | |
[ $countvm -eq 0 ] && break | |
done | |
for vm in $(list_running); do | |
savevm $vm | |
done | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
*) | |
usage | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment