3 # xendomains Start / stop domains automatically when domain 0 boots / shuts down.
6 # description: Start / stop Xen domains.
8 # This script offers fairly basic functionality. It should work on Redhat
9 # but also on LSB-compliant SuSE releases and on Debian with the LSB package
10 # installed. (LSB is the Linux Standard Base)
12 # Based on the example in the "Designing High Quality Integrated Linux
13 # Applications HOWTO" by Avi Alkalay
14 # <http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/>
17 . /etc/rc.d/init.d/functions
20 $CMD list >/dev/null 2>/dev/null || CMD=xl
21 $CMD list >/dev/null 2>/dev/null || exit 0
23 [ -e /proc/xen/privcmd ] || exit 0
25 if [ -r /etc/sysconfig/xendomains ]; then
26 . /etc/sysconfig/xendomains
28 echo "/etc/sysconfig/xendomains does not exist"
29 if [ "$1" = "stop" ]; then
37 # Returns 0 (success) if the given parameter names a directory, and that
38 # directory is not empty.
40 contains_something() {
41 if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ]; then
48 # read name from xen config file
50 NM=$($CMD create --quiet --dryrun --defconfig "$1" | sed -n 's/^.*(name \(.*\))$/\1/p')
55 if ! contains_something "$XENDOMAINS_AUTO"; then
58 for dom in $XENDOMAINS_AUTO/*; do
60 if test -z $NAMES; then
69 if [[ "$1" = "*(domain*" ]]; then
71 elif [[ "$1" = "*(name*" ]]; then
72 name=$(echo $1 | sed -e 's/^.*(name \(.*\))$/\1/')
73 elif [[ "$1" = "*(domid*" ]]; then
74 id=$(echo $1 | sed -e 's/^.*(domid \(.*\))$/\1/')
76 [ -n "$name" -a -n "$id" ] && return 0 || return 1
82 $CMD list -l | grep '(\(domain\|domid\|name\)' | \
84 parseln "$LN" || continue
85 [ $id = 0 ] && continue
96 if [ -f /var/lock/subsys/xendomains ]; then
97 echo -e "xendomains already running (lockfile exists)"
102 if [ "$XENDOMAINS_RESTORE" = "true" ] && contains_something "$XENDOMAINS_SAVE"; then
103 mkdir -p $(dirname "/var/lock/subsys/xendomains")
104 touch /var/lock/subsys/xendomains
105 echo -n "Restoring Xen domains:"
106 saved_domains=`ls $XENDOMAINS_SAVE`
107 for dom in $XENDOMAINS_SAVE/*; do
108 if [ -f $dom ] ; then
109 HEADER=`head -c 16 $dom | head -n 1 2> /dev/null`
110 if [ $HEADER = "LinuxGuestRecord" ]; then
111 echo -n " ${dom##*/}"
112 XMR=`$CMD restore $dom 2>&1 1>/dev/null`
114 if [ $? -ne 0 ]; then
115 echo -e "\nAn error occurred while restoring domain ${dom##*/}:\n$XMR"
118 # mv $dom ${dom%/*}/.${dom##*/}
127 if contains_something "$XENDOMAINS_AUTO" ; then
128 touch /var/lock/subsys/xendomains
129 echo -n "Starting auto Xen domains:"
130 # We expect config scripts for auto starting domains to be in
131 # XENDOMAINS_AUTO - they could just be symlinks to files elsewhere
133 # Create all domains with config files in XENDOMAINS_AUTO.
134 # TODO: We should record which domain name belongs
135 # so we have the option to selectively shut down / migrate later
136 # If a domain statefile from $XENDOMAINS_SAVE matches a domain name
137 # in $XENDOMAINS_AUTO, do not try to start that domain; if it didn't
138 # restore correctly it requires administrative attention.
139 for dom in $XENDOMAINS_AUTO/*; do
140 echo -n " ${dom##*/}"
141 shortdom=$(echo $dom | sed -n 's/^.*\/\(.*\)$/\1/p')
142 echo $saved_domains | grep -w $shortdom > /dev/null
143 if [ $? -eq 0 ] || is_running $dom; then
146 XMC=`$CMD create --quiet --defconfig $dom`
147 if [ $? -ne 0 ]; then
148 echo -e "\nAn error occurred while creating domain ${dom##*/}: $XMC\n"
151 usleep $XENDOMAINS_CREATE_USLEEP
160 $CMD list -l | grep '(\(domain\|domid\|name\)' | \
162 parseln "$LN" || continue
163 if test $id = 0; then continue; fi
164 if test "$state" != "-b---d" -a "$state" != "-----d"; then
171 # Wait for max $XENDOMAINS_STOP_MAXWAIT for $CMD $1 to finish;
172 # if it has not exited by that time kill it, so the init script will
173 # succeed within a finite amount of time; if $2 is nonnull, it will
174 # kill the command as well as soon as no domain (except for zombies)
175 # are left (used for shutdown --all). Third parameter, if any, suppresses
176 # output of dots per working state (formatting issues)
178 if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then
183 for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do
184 # exit if $CMD save/migrate/shutdown is finished
185 PSAX=`ps axlw | grep "$CMD $1" | grep -v grep`
186 if test -z "$PSAX"; then exit; fi
187 if ! test -n "$3"; then echo -n '.'; fi
189 # go to kill immediately if there's only zombies left
190 if all_zombies && test -n "$2"; then break; fi
193 PSPID=$($PSAX | awk '{ print $3 }')
195 kill $PSPID >/dev/null 2>&1
201 # Collect list of domains to shut down
202 if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
205 echo -n "Shutting down Xen domains:"
207 $CMD list -l | grep '(\(domain\|domid\|name\)' | \
209 parseln "$LN" || continue
210 if test $id = 0; then continue; fi
212 if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
225 # XENDOMAINS_SYSRQ chould be something like just "s"
226 # or "s e i u" or even "s e s i u o"
227 # for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so
228 if test -n "$XENDOMAINS_SYSRQ"; then
229 for sysrq in $XENDOMAINS_SYSRQ; do
230 echo -n "(SR-$sysrq)"
231 XMR=`$CMD sysrq $id $sysrq 2>&1 1>/dev/null`
232 if test $? -ne 0; then
233 echo -e "\nAn error occurred while doing sysrq on domain:\n$XMR\n"
236 # usleep just ignores empty arg
237 usleep $XENDOMAINS_USLEEP
240 if test "$state" = "-b---d" -o "$state" = "-----d"; then
244 if test -n "$XENDOMAINS_MIGRATE"; then
246 watchdog_xencmd migrate &
248 XMR=`$CMD migrate $id $XENDOMAINS_MIGRATE 2>&1 1>/dev/null`
249 if test $? -ne 0; then
250 echo -e "\nAn error occurred while migrating domain:\n$XMR\n"
253 kill $WDOG_PID >/dev/null 2>&1
255 kill $WDOG_PID >/dev/null 2>&1
262 if test -n "$XENDOMAINS_SAVE"; then
264 watchdog_xencmd save &
266 mkdir -p "$XENDOMAINS_SAVE"
267 XMR=`$CMD save $id $XENDOMAINS_SAVE/$name 2>&1 1>/dev/null`
268 if test $? -ne 0; then
269 echo -e "\nAn error occurred while saving domain:\n$XMR\n"
271 kill $WDOG_PID >/dev/null 2>&1
273 kill $WDOG_PID >/dev/null 2>&1
279 if test -n "$XENDOMAINS_SHUTDOWN"; then
280 # XENDOMAINS_SHUTDOWN should be "--halt --wait"
282 watchdog_xencmd shutdown &
284 XMR=`$CMD shutdown $id $XENDOMAINS_SHUTDOWN 2>&1 1>/dev/null`
285 if test $? -ne 0; then
286 echo -e "\nAn error occurred while shutting down domain:\n$XMR\n"
289 kill $WDOG_PID >/dev/null 2>&1
293 # NB. this shuts down ALL Xen domains (politely), not just the ones in
295 # This is because it's easier to do ;-) but arguably if this script is run
296 # on system shutdown then it's also the right thing to do.
297 if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then
298 # XENDOMAINS_SHUTDOWN_ALL should be "--all --halt --wait"
299 echo -n " SHUTDOWN_ALL "
300 watchdog_xencmd shutdown 1 false &
302 XMR=`$CMD shutdown $XENDOMAINS_SHUTDOWN_ALL 2>&1 1>/dev/null`
303 if test $? -ne 0; then
304 echo -e "\nAn error occurred while shutting down all domains: $XMR\n"
307 kill $WDOG_PID >/dev/null 2>&1
310 # Unconditionally delete lock file
311 rm -f /var/lock/subsys/xendomains
317 $CMD list -l | grep '(\(domain\|domid\|name\)' | \
319 parseln "$LN" || continue
320 if test $id = 0; then continue; fi
330 check_all_auto_domains_up()
332 if ! contains_something "$XENDOMAINS_AUTO"; then
336 for nm in $XENDOMAINS_AUTO/*; do
339 if check_domain_up "$NM"; then
342 missing="$missing $NM"
345 if test -n "$missing"; then
346 echo -n " MISS AUTO:$missing"
352 check_all_saved_domains_up()
354 if ! contains_something "$XENDOMAINS_SAVE"; then
357 missing=`/bin/ls $XENDOMAINS_SAVE`
358 echo -n " MISS SAVED: " $missing
363 # See how we were called.
372 # This does NOT necessarily restart all running domains: instead it
373 # stops all running domains and then boots all the domains specified in
374 # AUTODIR. If other domains have been started manually then they will
380 if [ -f /var/lock/subsys/xendomains; then
381 echo -n "Checking for xendomains:"
382 check_all_auto_domains_up
383 check_all_saved_domains_up
387 msg_usage "$0 {start|stop|restart|reload|status}"