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 -e "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##*/}"
233
XMR=`xm restore $dom 2>&1 1>/dev/null`
235
if [ $? -ne 0 ]; then
236
echo -e "\nAn error occurred while restoring domain ${dom##*/}:\n$XMR"
240
# mv $dom ${dom%/*}/.${dom##*/}
249
if contains_something "$XENDOMAINS_AUTO"
252
echo -n "Starting auto Xen domains:"
253
# We expect config scripts for auto starting domains to be in
254
# XENDOMAINS_AUTO - they could just be symlinks to files elsewhere
256
# Create all domains with config files in XENDOMAINS_AUTO.
257
# TODO: We should record which domain name belongs
258
# so we have the option to selectively shut down / migrate later
259
# If a domain statefile from $XENDOMAINS_SAVE matches a domain name
260
# in $XENDOMAINS_AUTO, do not try to start that domain; if it didn't
261
# restore correctly it requires administrative attention.
262
for dom in $XENDOMAINS_AUTO/*; do
263
echo -n " ${dom##*/}"
264
shortdom=$(echo $dom | sed -n 's/^.*\/\(.*\)$/\1/p')
265
echo $saved_domains | grep -w $shortdom > /dev/null
266
if [ $? -eq 0 ] || is_running $dom; then
269
XMC=`xm create --quiet --defconfig $dom`
270
if [ $? -ne 0 ]; then
271
echo -e "\nAn error occurred while creating domain ${dom##*/}: $XMC\n"
275
usleep $XENDOMAINS_CREATE_USLEEP
286
parseln "$LN" || continue
287
if test $id = 0; then continue; fi
288
if test "$state" != "-b---d" -a "$state" != "-----d"; then
291
done < <(xm list -l | grep '(\(domain\|domid\|name\)')
295
# Wait for max $XENDOMAINS_STOP_MAXWAIT for xm $1 to finish;
296
# if it has not exited by that time kill it, so the init script will
297
# succeed within a finite amount of time; if $2 is nonnull, it will
298
# kill the command as well as soon as no domain (except for zombies)
299
# are left (used for shutdown --all). Third parameter, if any, suppresses
300
# output of dots per working state (formatting issues)
303
if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then
308
for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do
309
# exit if xm save/migrate/shutdown is finished
310
PSAX=`ps axlw | grep "xm $1" | grep -v grep`
311
if test -z "$PSAX"; then exit; fi
312
if ! test -n "$3"; then echo -n '.'; fi
314
# go to kill immediately if there's only zombies left
315
if all_zombies && test -n "$2"; then break; fi
318
read PSF PSUID PSPID PSPPID < <(echo "$PSAX")
320
kill $PSPID >/dev/null 2>&1
327
exec 3>&2 2> /dev/null
329
# Collect list of domains to shut down
330
if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
333
echo -n "Shutting down Xen domains:"
336
parseln "$LN" || continue
337
if test $id = 0; then continue; fi
339
if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
352
# XENDOMAINS_SYSRQ chould be something like just "s"
353
# or "s e i u" or even "s e s i u o"
354
# for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so
355
if test -n "$XENDOMAINS_SYSRQ"; then
356
for sysrq in $XENDOMAINS_SYSRQ; do
357
echo -n "(SR-$sysrq)"
358
XMR=`xm sysrq $id $sysrq 2>&1 1>/dev/null`
359
if test $? -ne 0; then
360
echo -e "\nAn error occurred while doing sysrq on domain:\n$XMR\n"
364
# usleep just ignores empty arg
365
usleep $XENDOMAINS_USLEEP
368
if test "$state" = "-b---d" -o "$state" = "-----d"; then
372
if test -n "$XENDOMAINS_MIGRATE"; then
374
watchdog_xm migrate &
376
XMR=`xm migrate $id $XENDOMAINS_MIGRATE 2>&1 1>/dev/null`
377
if test $? -ne 0; then
378
echo -e "\nAn error occurred while migrating domain:\n$XMR\n"
382
kill $WDOG_PID >/dev/null 2>&1
384
kill $WDOG_PID >/dev/null 2>&1
391
if test -n "$XENDOMAINS_SAVE"; then
395
mkdir -p "$XENDOMAINS_SAVE"
396
XMR=`xm save $id $XENDOMAINS_SAVE/$name 2>&1 1>/dev/null`
397
if test $? -ne 0; then
398
echo -e "\nAn error occurred while saving domain:\n$XMR\n"
401
kill $WDOG_PID >/dev/null 2>&1
403
kill $WDOG_PID >/dev/null 2>&1
409
if test -n "$XENDOMAINS_SHUTDOWN"; then
410
# XENDOMAINS_SHUTDOWN should be "--halt --wait"
412
watchdog_xm shutdown &
414
XMR=`xm shutdown $id $XENDOMAINS_SHUTDOWN 2>&1 1>/dev/null`
415
if test $? -ne 0; then
416
echo -e "\nAn error occurred while shutting down domain:\n$XMR\n"
420
kill $WDOG_PID >/dev/null 2>&1
422
done < <(xm list -l | grep '(\(domain\|domid\|name\)')
424
# NB. this shuts down ALL Xen domains (politely), not just the ones in
426
# This is because it's easier to do ;-) but arguably if this script is run
427
# on system shutdown then it's also the right thing to do.
428
if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then
429
# XENDOMAINS_SHUTDOWN_ALL should be "--all --halt --wait"
430
echo -n " SHUTDOWN_ALL "
431
watchdog_xm shutdown 1 false &
433
XMR=`xm shutdown $XENDOMAINS_SHUTDOWN_ALL 2>&1 1>/dev/null`
434
if test $? -ne 0; then
435
echo -e "\nAn error occurred while shutting down all domains: $XMR\n"
439
kill $WDOG_PID >/dev/null 2>&1
442
# Unconditionally delete lock file
452
parseln "$LN" || continue
453
if test $id = 0; then continue; fi
459
done < <(xm list -l | grep '(\(domain\|domid\|name\)')
463
check_all_auto_domains_up()
465
if ! contains_something "$XENDOMAINS_AUTO"
470
for nm in $XENDOMAINS_AUTO/*; do
473
if check_domain_up "$NM"; then
476
missing="$missing $NM"
479
if test -n "$missing"; then
480
echo -n " MISS AUTO:$missing"
486
check_all_saved_domains_up()
488
if ! contains_something "$XENDOMAINS_SAVE"
492
missing=`/bin/ls $XENDOMAINS_SAVE`
493
echo -n " MISS SAVED: " $missing
497
# This does NOT necessarily restart all running domains: instead it
498
# stops all running domains and then boots all the domains specified in
499
# AUTODIR. If other domains have been started manually then they will
501
# Commented out to avoid confusion!
519
if test -f $LOCKFILE; then rc_status -v; fi
535
echo -n "Checking for xendomains:"
536
if test ! -f $LOCKFILE; then
539
check_all_auto_domains_up
541
check_all_saved_domains_up
548
echo "Usage: $0 {start|stop|restart|reload|status}"