ia64/xen-unstable

annotate tools/hotplug/Linux/init.d/xendomains @ 18770:4bfc67b09e9c

tools/hotplug: Separate OS-specific scripts.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Nov 04 12:13:42 2008 +0000 (2008-11-04)
parents
children 67d9d2a4b988
rev   line source
keir@18770 1 #!/bin/bash
keir@18770 2 #
keir@18770 3 # /etc/init.d/xendomains
keir@18770 4 # Start / stop domains automatically when domain 0 boots / shuts down.
keir@18770 5 #
keir@18770 6 # chkconfig: 345 99 00
keir@18770 7 # description: Start / stop Xen domains.
keir@18770 8 #
keir@18770 9 # This script offers fairly basic functionality. It should work on Redhat
keir@18770 10 # but also on LSB-compliant SuSE releases and on Debian with the LSB package
keir@18770 11 # installed. (LSB is the Linux Standard Base)
keir@18770 12 #
keir@18770 13 # Based on the example in the "Designing High Quality Integrated Linux
keir@18770 14 # Applications HOWTO" by Avi Alkalay
keir@18770 15 # <http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/>
keir@18770 16 #
keir@18770 17 ### BEGIN INIT INFO
keir@18770 18 # Provides: xendomains
keir@18770 19 # Required-Start: $syslog $remote_fs xend
keir@18770 20 # Should-Start:
keir@18770 21 # Required-Stop: $syslog $remote_fs xend
keir@18770 22 # Should-Stop:
keir@18770 23 # Default-Start: 3 4 5
keir@18770 24 # Default-Stop: 0 1 2 6
keir@18770 25 # Default-Enabled: yes
keir@18770 26 # Short-Description: Start/stop secondary xen domains
keir@18770 27 # Description: Start / stop domains automatically when domain 0
keir@18770 28 # boots / shuts down.
keir@18770 29 ### END INIT INFO
keir@18770 30
keir@18770 31 # Correct exit code would probably be 5, but it's enough
keir@18770 32 # if xend complains if we're not running as privileged domain
keir@18770 33 if ! [ -e /proc/xen/privcmd ]; then
keir@18770 34 exit 0
keir@18770 35 fi
keir@18770 36
keir@18770 37 LOCKFILE=/var/lock/subsys/xendomains
keir@18770 38 XENDOM_CONFIG=/etc/sysconfig/xendomains
keir@18770 39
keir@18770 40 test -r $XENDOM_CONFIG || { echo "$XENDOM_CONFIG not existing";
keir@18770 41 if [ "$1" = "stop" ]; then exit 0;
keir@18770 42 else exit 6; fi; }
keir@18770 43
keir@18770 44 . $XENDOM_CONFIG
keir@18770 45
keir@18770 46 # Use the SUSE rc_ init script functions;
keir@18770 47 # emulate them on LSB, RH and other systems
keir@18770 48 if test -e /etc/rc.status; then
keir@18770 49 # SUSE rc script library
keir@18770 50 . /etc/rc.status
keir@18770 51 else
keir@18770 52 _cmd=$1
keir@18770 53 declare -a _SMSG
keir@18770 54 if test "${_cmd}" = "status"; then
keir@18770 55 _SMSG=(running dead dead unused unknown)
keir@18770 56 _RC_UNUSED=3
keir@18770 57 else
keir@18770 58 _SMSG=(done failed failed missed failed skipped unused failed failed)
keir@18770 59 _RC_UNUSED=6
keir@18770 60 fi
keir@18770 61 if test -e /etc/init.d/functions; then
keir@18770 62 # REDHAT
keir@18770 63 . /etc/init.d/functions
keir@18770 64 echo_rc()
keir@18770 65 {
keir@18770 66 #echo -n " [${_SMSG[${_RC_RV}]}] "
keir@18770 67 if test ${_RC_RV} = 0; then
keir@18770 68 success " [${_SMSG[${_RC_RV}]}] "
keir@18770 69 else
keir@18770 70 failure " [${_SMSG[${_RC_RV}]}] "
keir@18770 71 fi
keir@18770 72 }
keir@18770 73 elif test -e /lib/lsb/init-functions; then
keir@18770 74 # LSB
keir@18770 75 . /lib/lsb/init-functions
keir@18770 76 if alias log_success_msg >/dev/null 2>/dev/null; then
keir@18770 77 echo_rc()
keir@18770 78 {
keir@18770 79 echo " [${_SMSG[${_RC_RV}]}] "
keir@18770 80 }
keir@18770 81 else
keir@18770 82 echo_rc()
keir@18770 83 {
keir@18770 84 if test ${_RC_RV} = 0; then
keir@18770 85 log_success_msg " [${_SMSG[${_RC_RV}]}] "
keir@18770 86 else
keir@18770 87 log_failure_msg " [${_SMSG[${_RC_RV}]}] "
keir@18770 88 fi
keir@18770 89 }
keir@18770 90 fi
keir@18770 91 else
keir@18770 92 # emulate it
keir@18770 93 echo_rc()
keir@18770 94 {
keir@18770 95 echo " [${_SMSG[${_RC_RV}]}] "
keir@18770 96 }
keir@18770 97 fi
keir@18770 98 rc_reset() { _RC_RV=0; }
keir@18770 99 rc_failed()
keir@18770 100 {
keir@18770 101 if test -z "$1"; then
keir@18770 102 _RC_RV=1;
keir@18770 103 elif test "$1" != "0"; then
keir@18770 104 _RC_RV=$1;
keir@18770 105 fi
keir@18770 106 return ${_RC_RV}
keir@18770 107 }
keir@18770 108 rc_check()
keir@18770 109 {
keir@18770 110 return rc_failed $?
keir@18770 111 }
keir@18770 112 rc_status()
keir@18770 113 {
keir@18770 114 rc_failed $?
keir@18770 115 if test "$1" = "-r"; then _RC_RV=0; shift; fi
keir@18770 116 if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi
keir@18770 117 if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi
keir@18770 118 if test "$1" = "-v"; then echo_rc; shift; fi
keir@18770 119 if test "$1" = "-r"; then _RC_RV=0; shift; fi
keir@18770 120 return ${_RC_RV}
keir@18770 121 }
keir@18770 122 rc_exit() { exit ${_RC_RV}; }
keir@18770 123 rc_active()
keir@18770 124 {
keir@18770 125 if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi
keir@18770 126 if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi
keir@18770 127 return 1
keir@18770 128 }
keir@18770 129 fi
keir@18770 130
keir@18770 131 if ! which usleep >&/dev/null
keir@18770 132 then
keir@18770 133 usleep()
keir@18770 134 {
keir@18770 135 if [ -n "$1" ]
keir@18770 136 then
keir@18770 137 sleep $(( $1 / 1000000 ))
keir@18770 138 fi
keir@18770 139 }
keir@18770 140 fi
keir@18770 141
keir@18770 142 # Reset status of this service
keir@18770 143 rc_reset
keir@18770 144
keir@18770 145 ##
keir@18770 146 # Returns 0 (success) if the given parameter names a directory, and that
keir@18770 147 # directory is not empty.
keir@18770 148 #
keir@18770 149 contains_something()
keir@18770 150 {
keir@18770 151 if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ]
keir@18770 152 then
keir@18770 153 return 0
keir@18770 154 else
keir@18770 155 return 1
keir@18770 156 fi
keir@18770 157 }
keir@18770 158
keir@18770 159 # read name from xen config file
keir@18770 160 rdname()
keir@18770 161 {
keir@18770 162 NM=$(xm create --quiet --dryrun --defconfig "$1" |
keir@18770 163 sed -n 's/^.*(name \(.*\))$/\1/p')
keir@18770 164 }
keir@18770 165
keir@18770 166 rdnames()
keir@18770 167 {
keir@18770 168 NAMES=
keir@18770 169 if ! contains_something "$XENDOMAINS_AUTO"
keir@18770 170 then
keir@18770 171 return
keir@18770 172 fi
keir@18770 173 for dom in $XENDOMAINS_AUTO/*; do
keir@18770 174 rdname $dom
keir@18770 175 if test -z $NAMES; then
keir@18770 176 NAMES=$NM;
keir@18770 177 else
keir@18770 178 NAMES="$NAMES|$NM"
keir@18770 179 fi
keir@18770 180 done
keir@18770 181 }
keir@18770 182
keir@18770 183 parseln()
keir@18770 184 {
keir@18770 185 if [[ "$1" =~ "\(domain" ]]; then
keir@18770 186 name=;id=
keir@18770 187 else if [[ "$1" =~ "\(name" ]]; then
keir@18770 188 name=$(echo $1 | sed -e 's/^.*(name \(.*\))$/\1/')
keir@18770 189 else if [[ "$1" =~ "\(domid" ]]; then
keir@18770 190 id=$(echo $1 | sed -e 's/^.*(domid \(.*\))$/\1/')
keir@18770 191 fi; fi; fi
keir@18770 192
keir@18770 193 [ -n "$name" -a -n "$id" ] && return 0 || return 1
keir@18770 194 }
keir@18770 195
keir@18770 196 is_running()
keir@18770 197 {
keir@18770 198 rdname $1
keir@18770 199 RC=1
keir@18770 200 name=;id=
keir@18770 201 while read LN; do
keir@18770 202 parseln "$LN" || continue
keir@18770 203 if test $id = 0; then continue; fi
keir@18770 204 case $name in
keir@18770 205 ($NM)
keir@18770 206 RC=0
keir@18770 207 ;;
keir@18770 208 esac
keir@18770 209 done < <(xm list -l | grep '(\(domain\|domid\|name\)')
keir@18770 210 return $RC
keir@18770 211 }
keir@18770 212
keir@18770 213 start()
keir@18770 214 {
keir@18770 215 if [ -f $LOCKFILE ]; then
keir@18770 216 echo -n "xendomains already running (lockfile exists)"
keir@18770 217 return;
keir@18770 218 fi
keir@18770 219
keir@18770 220 saved_domains=" "
keir@18770 221 if [ "$XENDOMAINS_RESTORE" = "true" ] &&
keir@18770 222 contains_something "$XENDOMAINS_SAVE"
keir@18770 223 then
keir@18770 224 mkdir -p $(dirname "$LOCKFILE")
keir@18770 225 touch $LOCKFILE
keir@18770 226 echo -n "Restoring Xen domains:"
keir@18770 227 saved_domains=`ls $XENDOMAINS_SAVE`
keir@18770 228 for dom in $XENDOMAINS_SAVE/*; do
keir@18770 229 if [ -f $dom ] ; then
keir@18770 230 HEADER=`head -c 16 $dom | head -n 1 2> /dev/null`
keir@18770 231 if [ $HEADER = "LinuxGuestRecord" ]; then
keir@18770 232 echo -n " ${dom##*/}"
keir@18770 233 xm restore $dom
keir@18770 234 if [ $? -ne 0 ]; then
keir@18770 235 rc_failed $?
keir@18770 236 echo -n '!'
keir@18770 237 else
keir@18770 238 # mv $dom ${dom%/*}/.${dom##*/}
keir@18770 239 rm $dom
keir@18770 240 fi
keir@18770 241 fi
keir@18770 242 fi
keir@18770 243 done
keir@18770 244 echo .
keir@18770 245 fi
keir@18770 246
keir@18770 247 if contains_something "$XENDOMAINS_AUTO"
keir@18770 248 then
keir@18770 249 touch $LOCKFILE
keir@18770 250 echo -n "Starting auto Xen domains:"
keir@18770 251 # We expect config scripts for auto starting domains to be in
keir@18770 252 # XENDOMAINS_AUTO - they could just be symlinks to files elsewhere
keir@18770 253
keir@18770 254 # Create all domains with config files in XENDOMAINS_AUTO.
keir@18770 255 # TODO: We should record which domain name belongs
keir@18770 256 # so we have the option to selectively shut down / migrate later
keir@18770 257 # If a domain statefile from $XENDOMAINS_SAVE matches a domain name
keir@18770 258 # in $XENDOMAINS_AUTO, do not try to start that domain; if it didn't
keir@18770 259 # restore correctly it requires administrative attention.
keir@18770 260 for dom in $XENDOMAINS_AUTO/*; do
keir@18770 261 echo -n " ${dom##*/}"
keir@18770 262 shortdom=$(echo $dom | sed -n 's/^.*\/\(.*\)$/\1/p')
keir@18770 263 echo $saved_domains | grep -w $shortdom > /dev/null
keir@18770 264 if [ $? -eq 0 ] || is_running $dom; then
keir@18770 265 echo -n "(skip)"
keir@18770 266 else
keir@18770 267 xm create --quiet --defconfig $dom
keir@18770 268 if [ $? -ne 0 ]; then
keir@18770 269 rc_failed $?
keir@18770 270 echo -n '!'
keir@18770 271 else
keir@18770 272 usleep $XENDOMAINS_CREATE_USLEEP
keir@18770 273 fi
keir@18770 274 fi
keir@18770 275 done
keir@18770 276 fi
keir@18770 277 }
keir@18770 278
keir@18770 279 all_zombies()
keir@18770 280 {
keir@18770 281 name=;id=
keir@18770 282 while read LN; do
keir@18770 283 parseln "$LN" || continue
keir@18770 284 if test $id = 0; then continue; fi
keir@18770 285 if test "$state" != "-b---d" -a "$state" != "-----d"; then
keir@18770 286 return 1;
keir@18770 287 fi
keir@18770 288 done < <(xm list -l | grep '(\(domain\|domid\|name\)')
keir@18770 289 return 0
keir@18770 290 }
keir@18770 291
keir@18770 292 # Wait for max $XENDOMAINS_STOP_MAXWAIT for xm $1 to finish;
keir@18770 293 # if it has not exited by that time kill it, so the init script will
keir@18770 294 # succeed within a finite amount of time; if $2 is nonnull, it will
keir@18770 295 # kill the command as well as soon as no domain (except for zombies)
keir@18770 296 # are left (used for shutdown --all).
keir@18770 297 watchdog_xm()
keir@18770 298 {
keir@18770 299 if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then
keir@18770 300 exit
keir@18770 301 fi
keir@18770 302 usleep 20000
keir@18770 303 for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do
keir@18770 304 # exit if xm save/migrate/shutdown is finished
keir@18770 305 PSAX=`ps axlw | grep "xm $1" | grep -v grep`
keir@18770 306 if test -z "$PSAX"; then exit; fi
keir@18770 307 echo -n "."; sleep 1
keir@18770 308 # go to kill immediately if there's only zombies left
keir@18770 309 if all_zombies && test -n "$2"; then break; fi
keir@18770 310 done
keir@18770 311 sleep 1
keir@18770 312 read PSF PSUID PSPID PSPPID < <(echo "$PSAX")
keir@18770 313 # kill xm $1
keir@18770 314 kill $PSPID >/dev/null 2>&1
keir@18770 315 }
keir@18770 316
keir@18770 317 stop()
keir@18770 318 {
keir@18770 319 # Collect list of domains to shut down
keir@18770 320 if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
keir@18770 321 rdnames
keir@18770 322 fi
keir@18770 323 echo -n "Shutting down Xen domains:"
keir@18770 324 name=;id=
keir@18770 325 while read LN; do
keir@18770 326 parseln "$LN" || continue
keir@18770 327 if test $id = 0; then continue; fi
keir@18770 328 echo -n " $name"
keir@18770 329 if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
keir@18770 330 eval "
keir@18770 331 case \"\$name\" in
keir@18770 332 ($NAMES)
keir@18770 333 # nothing
keir@18770 334 ;;
keir@18770 335 (*)
keir@18770 336 echo -n '(skip)'
keir@18770 337 continue
keir@18770 338 ;;
keir@18770 339 esac
keir@18770 340 "
keir@18770 341 fi
keir@18770 342 # XENDOMAINS_SYSRQ chould be something like just "s"
keir@18770 343 # or "s e i u" or even "s e s i u o"
keir@18770 344 # for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so
keir@18770 345 if test -n "$XENDOMAINS_SYSRQ"; then
keir@18770 346 for sysrq in $XENDOMAINS_SYSRQ; do
keir@18770 347 echo -n "(SR-$sysrq)"
keir@18770 348 xm sysrq $id $sysrq
keir@18770 349 if test $? -ne 0; then
keir@18770 350 rc_failed $?
keir@18770 351 echo -n '!'
keir@18770 352 fi
keir@18770 353 # usleep just ignores empty arg
keir@18770 354 usleep $XENDOMAINS_USLEEP
keir@18770 355 done
keir@18770 356 fi
keir@18770 357 if test "$state" = "-b---d" -o "$state" = "-----d"; then
keir@18770 358 echo -n "(zomb)"
keir@18770 359 continue
keir@18770 360 fi
keir@18770 361 if test -n "$XENDOMAINS_MIGRATE"; then
keir@18770 362 echo -n "(migr)"
keir@18770 363 watchdog_xm migrate &
keir@18770 364 WDOG_PID=$!
keir@18770 365 xm migrate $id $XENDOMAINS_MIGRATE
keir@18770 366 if test $? -ne 0; then
keir@18770 367 rc_failed $?
keir@18770 368 echo -n '!'
keir@18770 369 kill $WDOG_PID >/dev/null 2>&1
keir@18770 370 else
keir@18770 371 kill $WDOG_PID >/dev/null 2>&1
keir@18770 372 continue
keir@18770 373 fi
keir@18770 374 fi
keir@18770 375 if test -n "$XENDOMAINS_SAVE"; then
keir@18770 376 echo -n "(save)"
keir@18770 377 watchdog_xm save &
keir@18770 378 WDOG_PID=$!
keir@18770 379 mkdir -p "$XENDOMAINS_SAVE"
keir@18770 380 xm save $id $XENDOMAINS_SAVE/$name
keir@18770 381 if test $? -ne 0; then
keir@18770 382 rc_failed $?
keir@18770 383 echo -n '!'
keir@18770 384 kill $WDOG_PID >/dev/null 2>&1
keir@18770 385 else
keir@18770 386 kill $WDOG_PID >/dev/null 2>&1
keir@18770 387 continue
keir@18770 388 fi
keir@18770 389 fi
keir@18770 390 if test -n "$XENDOMAINS_SHUTDOWN"; then
keir@18770 391 # XENDOMAINS_SHUTDOWN should be "--halt --wait"
keir@18770 392 echo -n "(shut)"
keir@18770 393 watchdog_xm shutdown &
keir@18770 394 WDOG_PID=$!
keir@18770 395 xm shutdown $id $XENDOMAINS_SHUTDOWN
keir@18770 396 if test $? -ne 0; then
keir@18770 397 rc_failed $?
keir@18770 398 echo -n '!'
keir@18770 399 fi
keir@18770 400 kill $WDOG_PID >/dev/null 2>&1
keir@18770 401 fi
keir@18770 402 done < <(xm list -l | grep '(\(domain\|domid\|name\)')
keir@18770 403
keir@18770 404 # NB. this shuts down ALL Xen domains (politely), not just the ones in
keir@18770 405 # AUTODIR/*
keir@18770 406 # This is because it's easier to do ;-) but arguably if this script is run
keir@18770 407 # on system shutdown then it's also the right thing to do.
keir@18770 408 if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then
keir@18770 409 # XENDOMAINS_SHUTDOWN_ALL should be "--all --halt --wait"
keir@18770 410 echo -n " SHUTDOWN_ALL "
keir@18770 411 watchdog_xm shutdown 1 &
keir@18770 412 WDOG_PID=$!
keir@18770 413 xm shutdown $XENDOMAINS_SHUTDOWN_ALL
keir@18770 414 if test $? -ne 0; then
keir@18770 415 rc_failed $?
keir@18770 416 echo -n '!'
keir@18770 417 fi
keir@18770 418 kill $WDOG_PID >/dev/null 2>&1
keir@18770 419 fi
keir@18770 420
keir@18770 421 # Unconditionally delete lock file
keir@18770 422 rm -f $LOCKFILE
keir@18770 423 }
keir@18770 424
keir@18770 425 check_domain_up()
keir@18770 426 {
keir@18770 427 name=;id=
keir@18770 428 while read LN; do
keir@18770 429 parseln "$LN" || continue
keir@18770 430 if test $id = 0; then continue; fi
keir@18770 431 case $name in
keir@18770 432 ($1)
keir@18770 433 return 0
keir@18770 434 ;;
keir@18770 435 esac
keir@18770 436 done < <(xm list -l | grep '(\(domain\|domid\|name\)')
keir@18770 437 return 1
keir@18770 438 }
keir@18770 439
keir@18770 440 check_all_auto_domains_up()
keir@18770 441 {
keir@18770 442 if ! contains_something "$XENDOMAINS_AUTO"
keir@18770 443 then
keir@18770 444 return 0
keir@18770 445 fi
keir@18770 446 missing=
keir@18770 447 for nm in $XENDOMAINS_AUTO/*; do
keir@18770 448 rdname $nm
keir@18770 449 found=0
keir@18770 450 if check_domain_up "$NM"; then
keir@18770 451 echo -n " $name"
keir@18770 452 else
keir@18770 453 missing="$missing $NM"
keir@18770 454 fi
keir@18770 455 done
keir@18770 456 if test -n "$missing"; then
keir@18770 457 echo -n " MISS AUTO:$missing"
keir@18770 458 return 1
keir@18770 459 fi
keir@18770 460 return 0
keir@18770 461 }
keir@18770 462
keir@18770 463 check_all_saved_domains_up()
keir@18770 464 {
keir@18770 465 if ! contains_something "$XENDOMAINS_SAVE"
keir@18770 466 then
keir@18770 467 return 0
keir@18770 468 fi
keir@18770 469 missing=`/bin/ls $XENDOMAINS_SAVE`
keir@18770 470 echo -n " MISS SAVED: " $missing
keir@18770 471 return 1
keir@18770 472 }
keir@18770 473
keir@18770 474 # This does NOT necessarily restart all running domains: instead it
keir@18770 475 # stops all running domains and then boots all the domains specified in
keir@18770 476 # AUTODIR. If other domains have been started manually then they will
keir@18770 477 # not get restarted.
keir@18770 478 # Commented out to avoid confusion!
keir@18770 479
keir@18770 480 restart()
keir@18770 481 {
keir@18770 482 stop
keir@18770 483 start
keir@18770 484 }
keir@18770 485
keir@18770 486 reload()
keir@18770 487 {
keir@18770 488 restart
keir@18770 489 }
keir@18770 490
keir@18770 491
keir@18770 492 case "$1" in
keir@18770 493 start)
keir@18770 494 start
keir@18770 495 rc_status
keir@18770 496 if test -f $LOCKFILE; then rc_status -v; fi
keir@18770 497 ;;
keir@18770 498
keir@18770 499 stop)
keir@18770 500 stop
keir@18770 501 rc_status -v
keir@18770 502 ;;
keir@18770 503
keir@18770 504 restart)
keir@18770 505 restart
keir@18770 506 ;;
keir@18770 507 reload)
keir@18770 508 reload
keir@18770 509 ;;
keir@18770 510
keir@18770 511 status)
keir@18770 512 echo -n "Checking for xendomains:"
keir@18770 513 if test ! -f $LOCKFILE; then
keir@18770 514 rc_failed 3
keir@18770 515 else
keir@18770 516 check_all_auto_domains_up
keir@18770 517 rc_status
keir@18770 518 check_all_saved_domains_up
keir@18770 519 rc_status
keir@18770 520 fi
keir@18770 521 rc_status -v
keir@18770 522 ;;
keir@18770 523
keir@18770 524 *)
keir@18770 525 echo "Usage: $0 {start|stop|restart|reload|status}"
keir@18770 526 rc_failed 3
keir@18770 527 rc_status -v
keir@18770 528 ;;
keir@18770 529 esac
keir@18770 530
keir@18770 531 rc_exit