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 /etc/init.d/functions; then
63
. /etc/init.d/functions
66
#echo -n " [${_SMSG[${_RC_RV}]}] "
67
if test ${_RC_RV} = 0; then
68
success " [${_SMSG[${_RC_RV}]}] "
70
failure " [${_SMSG[${_RC_RV}]}] "
73
elif test -e /lib/lsb/init-functions; then
75
. /lib/lsb/init-functions
76
if alias log_success_msg >/dev/null 2>/dev/null; then
79
echo " [${_SMSG[${_RC_RV}]}] "
84
if test ${_RC_RV} = 0; then
85
log_success_msg " [${_SMSG[${_RC_RV}]}] "
87
log_failure_msg " [${_SMSG[${_RC_RV}]}] "
95
echo " [${_SMSG[${_RC_RV}]}] "
98
rc_reset() { _RC_RV=0; }
101
if test -z "$1"; then
103
elif test "$1" != "0"; then
115
if test "$1" = "-r"; then _RC_RV=0; shift; fi
116
if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi
117
if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi
118
if test "$1" = "-v"; then echo_rc; shift; fi
119
if test "$1" = "-r"; then _RC_RV=0; shift; fi
122
rc_exit() { exit ${_RC_RV}; }
125
if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi
126
if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi
131
if ! which usleep >&/dev/null
137
sleep $(( $1 / 1000000 ))
142
# Reset status of this service
146
# Returns 0 (success) if the given parameter names a directory, and that
147
# directory is not empty.
151
if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ]
159
# read name from xen config file
162
NM=$(xm create --quiet --dryrun --defconfig "$1" |
163
sed -n 's/^.*(name \(.*\))$/\1/p')
169
if ! contains_something "$XENDOMAINS_AUTO"
173
for dom in $XENDOMAINS_AUTO/*; do
175
if test -z $NAMES; then
185
if [[ "$1" =~ "\(domain" ]]; then
187
else if [[ "$1" =~ "\(name" ]]; then
188
name=$(echo $1 | sed -e 's/^.*(name \(.*\))$/\1/')
189
else if [[ "$1" =~ "\(domid" ]]; then
190
id=$(echo $1 | sed -e 's/^.*(domid \(.*\))$/\1/')
193
[ -n "$name" -a -n "$id" ] && return 0 || return 1
202
parseln "$LN" || continue
203
if test $id = 0; then continue; fi
209
done < <(xm list -l | grep '(\(domain\|domid\|name\)')
215
if [ -f $LOCKFILE ]; then
216
echo -n "xendomains already running (lockfile exists)"
221
if [ "$XENDOMAINS_RESTORE" = "true" ] &&
222
contains_something "$XENDOMAINS_SAVE"
224
mkdir -p $(dirname "$LOCKFILE")
226
echo -n "Restoring Xen domains:"
227
saved_domains=`ls $XENDOMAINS_SAVE`
228
for dom in $XENDOMAINS_SAVE/*; do
229
if [ -f $dom ] ; then
230
HEADER=`head -c 16 $dom | head -n 1 2> /dev/null`
231
if [ $HEADER = "LinuxGuestRecord" ]; then
232
echo -n " ${dom##*/}"
234
if [ $? -ne 0 ]; then
238
# mv $dom ${dom%/*}/.${dom##*/}
247
if contains_something "$XENDOMAINS_AUTO"
250
echo -n "Starting auto Xen domains:"
251
# We expect config scripts for auto starting domains to be in
252
# XENDOMAINS_AUTO - they could just be symlinks to files elsewhere
254
# Create all domains with config files in XENDOMAINS_AUTO.
255
# TODO: We should record which domain name belongs
256
# so we have the option to selectively shut down / migrate later
257
# If a domain statefile from $XENDOMAINS_SAVE matches a domain name
258
# in $XENDOMAINS_AUTO, do not try to start that domain; if it didn't
259
# restore correctly it requires administrative attention.
260
for dom in $XENDOMAINS_AUTO/*; do
261
echo -n " ${dom##*/}"
262
shortdom=$(echo $dom | sed -n 's/^.*\/\(.*\)$/\1/p')
263
echo $saved_domains | grep -w $shortdom > /dev/null
264
if [ $? -eq 0 ] || is_running $dom; then
267
xm create --quiet --defconfig $dom
268
if [ $? -ne 0 ]; then
272
usleep $XENDOMAINS_CREATE_USLEEP
283
parseln "$LN" || continue
284
if test $id = 0; then continue; fi
285
if test "$state" != "-b---d" -a "$state" != "-----d"; then
288
done < <(xm list -l | grep '(\(domain\|domid\|name\)')
292
# Wait for max $XENDOMAINS_STOP_MAXWAIT for xm $1 to finish;
293
# if it has not exited by that time kill it, so the init script will
294
# succeed within a finite amount of time; if $2 is nonnull, it will
295
# kill the command as well as soon as no domain (except for zombies)
296
# are left (used for shutdown --all).
299
if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then
303
for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do
304
# exit if xm save/migrate/shutdown is finished
305
PSAX=`ps axlw | grep "xm $1" | grep -v grep`
306
if test -z "$PSAX"; then exit; fi
308
# go to kill immediately if there's only zombies left
309
if all_zombies && test -n "$2"; then break; fi
312
read PSF PSUID PSPID PSPPID < <(echo "$PSAX")
314
kill $PSPID >/dev/null 2>&1
319
# Collect list of domains to shut down
320
if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
323
echo -n "Shutting down Xen domains:"
326
parseln "$LN" || continue
327
if test $id = 0; then continue; fi
329
if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
342
# XENDOMAINS_SYSRQ chould be something like just "s"
343
# or "s e i u" or even "s e s i u o"
344
# for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so
345
if test -n "$XENDOMAINS_SYSRQ"; then
346
for sysrq in $XENDOMAINS_SYSRQ; do
347
echo -n "(SR-$sysrq)"
349
if test $? -ne 0; then
353
# usleep just ignores empty arg
354
usleep $XENDOMAINS_USLEEP
357
if test "$state" = "-b---d" -o "$state" = "-----d"; then
361
if test -n "$XENDOMAINS_MIGRATE"; then
363
watchdog_xm migrate &
365
xm migrate $id $XENDOMAINS_MIGRATE
366
if test $? -ne 0; then
369
kill $WDOG_PID >/dev/null 2>&1
371
kill $WDOG_PID >/dev/null 2>&1
375
if test -n "$XENDOMAINS_SAVE"; then
379
mkdir -p "$XENDOMAINS_SAVE"
380
xm save $id $XENDOMAINS_SAVE/$name
381
if test $? -ne 0; then
384
kill $WDOG_PID >/dev/null 2>&1
386
kill $WDOG_PID >/dev/null 2>&1
390
if test -n "$XENDOMAINS_SHUTDOWN"; then
391
# XENDOMAINS_SHUTDOWN should be "--halt --wait"
393
watchdog_xm shutdown &
395
xm shutdown $id $XENDOMAINS_SHUTDOWN
396
if test $? -ne 0; then
400
kill $WDOG_PID >/dev/null 2>&1
402
done < <(xm list -l | grep '(\(domain\|domid\|name\)')
404
# NB. this shuts down ALL Xen domains (politely), not just the ones in
406
# This is because it's easier to do ;-) but arguably if this script is run
407
# on system shutdown then it's also the right thing to do.
408
if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then
409
# XENDOMAINS_SHUTDOWN_ALL should be "--all --halt --wait"
410
echo -n " SHUTDOWN_ALL "
411
watchdog_xm shutdown 1 &
413
xm shutdown $XENDOMAINS_SHUTDOWN_ALL
414
if test $? -ne 0; then
418
kill $WDOG_PID >/dev/null 2>&1
421
# Unconditionally delete lock file
429
parseln "$LN" || continue
430
if test $id = 0; then continue; fi
436
done < <(xm list -l | grep '(\(domain\|domid\|name\)')
440
check_all_auto_domains_up()
442
if ! contains_something "$XENDOMAINS_AUTO"
447
for nm in $XENDOMAINS_AUTO/*; do
450
if check_domain_up "$NM"; then
453
missing="$missing $NM"
456
if test -n "$missing"; then
457
echo -n " MISS AUTO:$missing"
463
check_all_saved_domains_up()
465
if ! contains_something "$XENDOMAINS_SAVE"
469
missing=`/bin/ls $XENDOMAINS_SAVE`
470
echo -n " MISS SAVED: " $missing
474
# This does NOT necessarily restart all running domains: instead it
475
# stops all running domains and then boots all the domains specified in
476
# AUTODIR. If other domains have been started manually then they will
478
# Commented out to avoid confusion!
496
if test -f $LOCKFILE; then rc_status -v; fi
512
echo -n "Checking for xendomains:"
513
if test ! -f $LOCKFILE; then
516
check_all_auto_domains_up
518
check_all_saved_domains_up
525
echo "Usage: $0 {start|stop|restart|reload|status}"