aboutsummaryrefslogtreecommitdiff
path: root/src/libvirt/libvirtd-guests.rc.d
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2012-11-25 21:24:58 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2012-11-25 21:24:58 +0100
commit16262790cb6ddacf6c632625cc865e03b1b8671f (patch)
tree09898d65deef518380915ecdc7575756c9ca8595 /src/libvirt/libvirtd-guests.rc.d
parent7bb1499a7cd539f714bb7f603d7fc0a38fd8a963 (diff)
downloadrcdscripts-16262790cb6ddacf6c632625cc865e03b1b8671f.tar.xz
rcdscripts: import first snapshot of rc.d scripts as of 11.25.20122012.11.25
In 30 days these scripts will start dissapearing from official Arch Linux packages. This is an attempt to conserve them, and keep sysvinit usable.
Diffstat (limited to 'src/libvirt/libvirtd-guests.rc.d')
-rwxr-xr-xsrc/libvirt/libvirtd-guests.rc.d161
1 files changed, 161 insertions, 0 deletions
diff --git a/src/libvirt/libvirtd-guests.rc.d b/src/libvirt/libvirtd-guests.rc.d
new file mode 100755
index 0000000..3100b34
--- /dev/null
+++ b/src/libvirt/libvirtd-guests.rc.d
@@ -0,0 +1,161 @@
+#!/bin/bash
+
+source /etc/conf.d/libvirtd-guests
+[ -f /etc/rc.conf ] && source /etc/rc.conf
+
+if [ -f /etc/rc.d/functions ]; then
+ . /etc/rc.d/functions
+else
+ stat_busy() {
+ echo "$*"
+ }
+
+ stat_fail() {
+ echo "FAIL"
+ }
+
+ stat_done() {
+ echo "DONE"
+ }
+
+ add_daemon() {
+ true
+ }
+
+ rm_daemon() {
+ true
+ }
+fi
+
+
+LIBVIRTD_LISTFILE="/var/state/libvirtd/vm-list"
+
+# get guest state by name
+libvirt_get_guest_state()
+{
+ virsh $LIBVIRTD_URI dominfo "$1" | grep -E '^State:' | awk '{print $2}'
+}
+
+# list IDs of running guests
+libvirt_list()
+{
+
+ list=$(virsh $LIBVIRTD_URI list)
+
+ if [ $? -ne 0 ]; then
+ RETVAL=1
+ return 1
+ fi
+
+ uuids=
+ for id in $(echo "$list" | awk 'NR > 2 {print $1}'); do
+ uuid=$(virsh $LIBVIRTD_UTI dominfo $id | awk '/^UUID:/{print $2}')
+ if [ -z "$uuid" ]; then
+ RETVAL=1
+ return 1
+ fi
+ uuids="$uuids $uuid"
+ done
+
+ echo $uuids
+
+}
+
+libvirt_domname()
+{
+ uuid=$1
+ name=$(virsh $LIBVIRTD_URI dominfo $uuid | awk 'NR == 2 {$1=""; print}')
+
+ echo $name
+}
+
+# suspend guest by name
+libvirt_suspend()
+{
+ virsh $LIBVIRTD_URI $LIBVIRTD_BYPASS_CACHE managedsave "$1" >/dev/null
+ timeout=$LIBVIRTD_SHUTDOWN_TIMEOUT
+ while [ "$timeout" -gt 0 ]; do
+ sleep 1
+ timeout=$((timeout - 1))
+ state=`libvirt_get_guest_state "$1"`
+ [ "x$state" == "xshut" ] && return 0
+ done
+ return 1
+}
+
+# shutdown guest by name
+libvirt_shutdown()
+{
+ virsh $LIBVIRTD_URI shutdown "$1" >/dev/null
+ timeout=$LIBVIRTD_SHUTDOWN_TIMEOUT
+ while [ "$timeout" -gt 0 ]; do
+ sleep 1
+ timeout=$((timeout - 1))
+ state=`libvirt_get_guest_state "$1"`
+ [ "x$state" == "xshut" ] && return 0
+ done
+ return 1
+}
+
+# start guest by name
+libvirt_start()
+{
+ virsh $LIBVIRTD_URI $LIBVIRTD_BYPASS_CACHE start "$1" >/dev/null
+}
+
+# stop all guests
+libvirt_stop_all()
+{
+ mkdir -p `dirname $LIBVIRTD_LISTFILE`
+ echo -n >$LIBVIRTD_LISTFILE
+
+ for i in `libvirt_list`; do
+ name=`libvirt_domname $i`
+ if [ "x$LIBVIRTD_STOP_ACTION" == "xsuspend" ]; then
+ stat_busy "Suspending libvirtd/$name guest"
+ libvirt_suspend "$i"
+ else
+ stat_busy "Shutting libvirtd/$i guest down"
+ libvirt_shutdown "$i"
+ fi
+ [ $? -eq 0 ] && stat_done || stat_fail
+ echo $i >>$LIBVIRTD_LISTFILE
+ done
+}
+
+# start all guests
+libvirt_start_all()
+{
+ if [ -f $LIBVIRTD_LISTFILE ]; then
+ for i in `cat $LIBVIRTD_LISTFILE`; do
+ name=`libvirt_domname $i`
+ stat_busy "Starting/resuming libvirtd/$name guest"
+ libvirt_start "$i"
+ [ $? -eq 0 ] && { sleep $LIBVIRTD_START_DELAY; stat_done; } || stat_fail
+ done
+ fi
+ rm -f $LIBVIRTD_LISTFILE
+}
+
+# main
+LC_ALL=C
+LANG=C
+case "$1" in
+ start)
+ libvirt_start_all
+ add_daemon libvirtd-guests
+ ;;
+ stop)
+ libvirt_stop_all
+ rm_daemon libvirtd-guests
+ ;;
+ restart)
+ $0 stop
+ sleep 1
+ $0 start
+ ;;
+ *)
+ echo $"Usage: $0 {start|stop|restart}"
+ ;;
+esac
+exit 0