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