3 # /etc/init.d/xendomains
4 # Start / stop domains automatically when domain 0 boots / shuts down.
7 # description: Start / stop Xen domains.
9 # This script offers fairly basic functionality. It should work on Redhat
10 # but also on LSB-compliant SuSE releases and on Debian with the LSB package
11 # installed. (LSB is the Linux Standard Base)
13 # Based on the example in the "Designing High Quality Integrated Linux
14 # Applications HOWTO" by Avi Alkalay
15 # <http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/>
18 # Provides: xendomains
19 # Required-Start: $syslog $remote_fs xend
21 # Required-Stop: $syslog $remote_fs xend
23 # Default-Start: 3 4 5
24 # Default-Stop: 0 1 2 6
25 # Default-Enabled: yes
26 # Short-Description: Start/stop secondary xen domains
27 # Description: Start / stop domains automatically when domain 0
31 # Correct exit code would probably be 5, but it's enough
32 # if xend complains if we're not running as privileged domain
33 if ! [ -e /proc/xen/privcmd ]; then
37 LOCKFILE=/var/lock/subsys/xendomains
38 XENDOM_CONFIG=/etc/sysconfig/xendomains
40 test -r $XENDOM_CONFIG || { echo "$XENDOM_CONFIG not existing";
41 if [ "$1" = "stop" ]; then exit 0;
46 # Use the SUSE rc_ init script functions;
47 # emulate them on LSB, RH and other systems
48 if test -e /etc/rc.status; then
49 # SUSE rc script library
54 if test "${_cmd}" = "status"; then
55 _SMSG=(running dead dead unused unknown)
58 _SMSG=(done failed failed missed failed skipped unused failed failed)
61 if test -e /lib/lsb/init-functions; then
63 . /lib/lsb/init-functions
66 if test ${_RC_RV} = 0; then
67 log_success_msg " [${_SMSG[${_RC_RV}]}] "
69 log_failure_msg " [${_SMSG[${_RC_RV}]}] "
72 elif test -e /etc/init.d/functions; then
74 . /etc/init.d/functions
77 #echo -n " [${_SMSG[${_RC_RV}]}] "
78 if test ${_RC_RV} = 0; then
79 success " [${_SMSG[${_RC_RV}]}] "
81 failure " [${_SMSG[${_RC_RV}]}] "
88 echo " [${_SMSG[${_RC_RV}]}] "
91 rc_reset() { _RC_RV=0; }
96 elif test "$1" != "0"; then
108 if test "$1" = "-r"; then _RC_RV=0; shift; fi
109 if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi
110 if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi
111 if test "$1" = "-v"; then echo_rc; shift; fi
112 if test "$1" = "-r"; then _RC_RV=0; shift; fi
115 rc_exit() { exit ${_RC_RV}; }
118 if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi
119 if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi
124 if ! which usleep >&/dev/null
130 sleep $(( $1 / 1000000 ))
135 # Reset status of this service
139 # Returns 0 (success) if the given parameter names a directory, and that
140 # directory is not empty.
144 if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ]
152 # read name from xen config file
155 NM=$(xm create --quiet --dryrun --defconfig "$1" |
156 sed -n 's/^.*(name \(.*\))$/\1/p')
162 if ! contains_something "$XENDOMAINS_AUTO"
166 for dom in $XENDOMAINS_AUTO/*; do
168 if test -z $NAMES; then
178 name=`echo "$1" | cut -c0-17`
180 rest=`echo "$1" | cut -c18- `
181 read id mem cpu vcpu state tm < <(echo "$rest")
190 if test $id = 0; then continue; fi
196 done < <(xm list | grep -v '^Name')
202 if [ -f $LOCKFILE ]; then
203 echo -n "xendomains already running (lockfile exists)"
207 if [ "$XENDOMAINS_RESTORE" = "true" ] &&
208 contains_something "$XENDOMAINS_SAVE"
210 mkdir -p $(dirname "$LOCKFILE")
212 echo -n "Restoring Xen domains:"
213 for dom in $XENDOMAINS_SAVE/*; do
214 echo -n " ${dom##*/}"
216 if [ $? -ne 0 ]; then
220 # mv $dom ${dom%/*}/.${dom##*/}
227 if contains_something "$XENDOMAINS_AUTO"
230 echo -n "Starting auto Xen domains:"
231 # We expect config scripts for auto starting domains to be in
232 # XENDOMAINS_AUTO - they could just be symlinks to files elsewhere
234 # Create all domains with config files in XENDOMAINS_AUTO.
235 # TODO: We should record which domain name belongs
236 # so we have the option to selectively shut down / migrate later
237 for dom in $XENDOMAINS_AUTO/*; do
238 echo -n " ${dom##*/}"
239 if is_running $dom; then
242 xm create --quiet --defconfig $dom
243 if [ $? -ne 0 ]; then
247 usleep $XENDOMAINS_CREATE_USLEEP
258 if test $id = 0; then continue; fi
259 if test "$state" != "-b---d" -a "$state" != "-----d"; then
262 done < <(xm list | grep -v '^Name')
266 # Wait for max $XENDOMAINS_STOP_MAXWAIT for xm $1 to finish;
267 # if it has not exited by that time kill it, so the init script will
268 # succeed within a finite amount of time; if $2 is nonnull, it will
269 # kill the command as well as soon as no domain (except for zombies)
270 # are left (used for shutdown --all).
273 if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then
277 for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do
278 # exit if xm save/migrate/shutdown is finished
279 PSAX=`ps axlw | grep "xm $1" | grep -v grep`
280 if test -z "$PSAX"; then exit; fi
282 # go to kill immediately if there's only zombies left
283 if all_zombies && test -n "$2"; then break; fi
286 read PSF PSUID PSPID PSPPID < <(echo "$PSAX")
288 kill $PSPID >/dev/null 2>&1
293 # Collect list of domains to shut down
294 if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
297 echo -n "Shutting down Xen domains:"
300 if test $id = 0; then continue; fi
302 if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
313 # XENDOMAINS_SYSRQ chould be something like just "s"
314 # or "s e i u" or even "s e s i u o"
315 # for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so
316 if test -n "$XENDOMAINS_SYSRQ"; then
317 for sysrq in $XENDOMAINS_SYSRQ; do
318 echo -n "(SR-$sysrq)"
320 if test $? -ne 0; then
324 # usleep just ignores empty arg
325 usleep $XENDOMAINS_USLEEP
328 if test "$state" = "-b---d" -o "$state" = "-----d"; then
332 if test -n "$XENDOMAINS_MIGRATE"; then
334 watchdog_xm migrate &
336 xm migrate $id $XENDOMAINS_MIGRATE
337 if test $? -ne 0; then
340 kill $WDOG_PID >/dev/null 2>&1
342 kill $WDOG_PID >/dev/null 2>&1
346 if test -n "$XENDOMAINS_SAVE"; then
350 mkdir -p "$XENDOMAINS_SAVE"
351 xm save $id $XENDOMAINS_SAVE/$name
352 if test $? -ne 0; then
355 kill $WDOG_PIG >/dev/null 2>&1
357 kill $WDOG_PIG >/dev/null 2>&1
361 if test -n "$XENDOMAINS_SHUTDOWN"; then
362 # XENDOMAINS_SHUTDOWN should be "--halt --wait"
364 watchdog_xm shutdown &
366 xm shutdown $id $XENDOMAINS_SHUTDOWN
367 if test $? -ne 0; then
371 kill $WDOG_PIG >/dev/null 2>&1
373 done < <(xm list | grep -v '^Name')
375 # NB. this shuts down ALL Xen domains (politely), not just the ones in
377 # This is because it's easier to do ;-) but arguably if this script is run
378 # on system shutdown then it's also the right thing to do.
379 if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then
380 # XENDOMAINS_SHUTDOWN_ALL should be "--all --halt --wait"
381 echo -n " SHUTDOWN_ALL "
382 watchdog_xm shutdown 1 &
384 xm shutdown $XENDOMAINS_SHUTDOWN_ALL
385 if test $? -ne 0; then
389 kill $WDOG_PID >/dev/null 2>&1
392 # Unconditionally delete lock file
400 if test $id = 0; then continue; fi
406 done < <(xm list | grep -v "^Name")
410 check_all_auto_domains_up()
412 if ! contains_something "$XENDOMAINS_AUTO"
417 for nm in $XENDOMAINS_AUTO/*; do
420 if check_domain_up "$NM"; then
423 missing="$missing $NM"
426 if test -n "$missing"; then
427 echo -n " MISS AUTO:$missing"
433 check_all_saved_domains_up()
435 if ! contains_something "$XENDOMAINS_SAVE"
439 missing=`/bin/ls $XENDOMAINS_SAVE`
440 echo -n " MISS SAVED: " $missing
444 # This does NOT necessarily restart all running domains: instead it
445 # stops all running domains and then boots all the domains specified in
446 # AUTODIR. If other domains have been started manually then they will
448 # Commented out to avoid confusion!
466 if test -f $LOCKFILE; then rc_status -v; fi
482 echo -n "Checking for xendomains:"
483 if test ! -f $LOCKFILE; then
486 check_all_auto_domains_up
488 check_all_saved_domains_up
495 echo "Usage: $0 {start|stop|restart|reload|status}"