updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / libvirt-git / libvirtd-guests.rc.d
bloba97a8d9d16ed63c66104f8342470f4af455cb2be
1 #!/bin/sh
3 . /etc/conf.d/libvirtd-guests
4 . /etc/rc.conf
5 . /etc/rc.d/functions
7 LIBVIRTD_LISTFILE="/var/state/libvirtd/vm-list"
9 # get guest state by name
10 libvirt_get_guest_state()
12 virsh $LIBVIRTD_URI dominfo "$1" | grep -E '^State:' | awk '{print $2}'
15 # list IDs of running guests
16 libvirt_list()
18 virsh $LIBVIRTD_URI list | awk 'NR > 2 {print $2}'
21 # suspend guest by name
22 libvirt_suspend()
24 virsh $LIBVIRTD_URI $LIBVIRTD_BYPASS_CACHE managedsave "$1" >/dev/null
25 timeout=$LIBVIRTD_SHUTDOWN_TIMEOUT
26 while [ "$timeout" -gt 0 ]; do
27 sleep 1
28 timeout=$((timeout - 1))
29 state=`libvirt_get_guest_state "$1"`
30 [ "x$state" == "xshut" ] && return 0
31 done
32 return 1
35 # shutdown guest by name
36 libvirt_shutdown()
38 virsh $LIBVIRTD_URI shutdown "$1" >/dev/null
39 timeout=$LIBVIRTD_SHUTDOWN_TIMEOUT
40 while [ "$timeout" -gt 0 ]; do
41 sleep 1
42 timeout=$((timeout - 1))
43 state=`libvirt_get_guest_state "$1"`
44 [ "x$state" != "xshut" ] && return 0
45 done
46 return 1
49 # start guest by name
50 libvirt_start()
52 virsh $LIBVIRTD_URI $LIBVIRTD_BYPASS_CACHE start "$1" >/dev/null
55 # stop all guests
56 libvirt_stop_all()
58 mkdir -p `dirname $LIBVIRTD_LISTFILE`
59 echo -n >$LIBVIRTD_LISTFILE
60 for i in `libvirt_list`; do
61 if [ "x$LIBVIRTD_STOP_ACTION" == "xsuspend" ]; then
62 stat_busy "Suspending libvirtd/$i guest"
63 libvirt_suspend "$i"
64 else
65 stat_busy "Shutting libvirtd/$i guest down"
66 libvirt_shutdown "$i"
68 [ $? -eq 0 ] && stat_done || stat_fail
69 echo $i >>$LIBVIRTD_LISTFILE
70 done
73 # start all guests
74 libvirt_start_all()
76 if [ -f $LIBVIRTD_LISTFILE ]; then
77 for i in `cat $LIBVIRTD_LISTFILE`; do
78 stat_busy "Starting/resuming libvirtd/$i guest"
79 libvirt_start "$i"
80 [ $? -eq 0 ] && { sleep $LIBVIRTD_START_DELAY; stat_done; } || stat_fail
81 done
83 rm -f $LIBVIRTD_LISTFILE
86 # main
87 LC_ALL=C
88 LANG=C
89 case "$1" in
90 start)
91 libvirt_start_all
92 add_daemon libvirtd-guests
94 stop)
95 libvirt_stop_all
96 rm_daemon libvirtd-guests
98 restart)
99 $0 stop
100 sleep 1
101 $0 start
104 echo $"Usage: $0 {start|stop|restart}"
106 esac
107 exit 0