[kernel-xen] Test init script for Xen 4.1.2
Steven Haigh
netwiz at crc.id.au
Mon May 21 19:59:56 EST 2012
Hi all,
I have attempted a re-write of the xendomains init script to interpret a
few things better and hopefully allow me to move away from lock files
for starts DomUs which the original does not track correctly.
As 'xm create blah' does not create a lock file, sometimes the original
initscript does not correctly shutdown or save running domains that are
not started at boot.
I have attached the script for testing (please back up the original
first!). It should be dropped into /etc/init.d
Please post feedback to the list!
--
Steven Haigh
Email: netwiz at crc.id.au
Web: http://www.crc.id.au
Phone: (03) 9001 6090 - 0412 935 897
Fax: (03) 8338 0299
-------------- next part --------------
#!/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.
#
# Re-written from scratch by Steven Haigh <netwiz at crc.id.au> for EL6 as stock
# script has a lot of problems. May work on other distros, YMMV.
#
### 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 4 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
## Check to see what we use to communicate with the hypervisor - xm or xl
## We will prefer xm, but fall back to xl if we have to. Exit if none work.
CMD=`which xm`
if [ -x $CMD ]; then
$CMD list &> /dev/null
if test $? -ne 0; then
CMD=`which xl`
$CMD list &> /dev/null
if test $? -ne 0; then
echo "Couldn't find a usable xl or xm. Bailing..."
exit 0
fi
fi
fi
## Check to see if we're running as Dom0. If so, the following file should
## exist.
if ! [ -e /proc/xen/privcmd ]; then
echo "Not running as Domain0. Exiting..."
exit 0
fi
## Load the xendomains options, bailing if we can't.
if [ -r /etc/sysconfig/xendomains ]; then
. /etc/sysconfig/xendomains
else
echo "/etc/sysconfig/xendomains not found or unreadable. This is an issue. Exiting..."
exit 0
fi
if [ -d /var/lock/subsys ]; then
LOCKFILE=/var/lock/subsys/xendomains
else
LOCKFILE=/var/lock/xendomains
fi
## Source function library.
. /etc/init.d/functions
get_domu_name() {
DOMU_NAME=$($CMD create --quiet --dryrun --defconfig "$1" | sed -n 's/^.*(name \(.*\))$/\1/p')
}
watchdog() {
## Launch a command and wait until the timeout before killing forcefully.
for WAIT in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do
RESULT=`ps -p $PID`
if [ $? -eq 1 ]; then
success
break
fi
echo -n "."
sleep 1
done
RESULT=`ps -p $PID`
if [ $? -eq 0 ]; then
warning
kill $PID
fi
echo
}
start() {
touch $LOCKFILE
## Check to see if we should restore saved DomUs on start
## Also check if the restore directory actually exists.
if [ "$XENDOMAINS_RESTORE" = "true" ] && [ -d $XENDOMAINS_SAVE ] && [ -n "$XENDOMAINS_SAVE" ]; then
for DOMU in $XENDOMAINS_SAVE/*; do
if [ -r $DOMU ]; then
HEADER=`head -c 16 $DOMU | head -n 1 2> /dev/null`
if [ "$HEADER" = "LinuxGuestRecord" ] || [ "$HEADER" = "Xen saved domain" ]; then
echo -n "Restoring Xen Domain ${DOMU##*/}"
RESULT=`$CMD restore $DOMU 2>&1 1>/dev/null`
if [ $? -ne 0 ]; then
failure
else
success
fi
rm -f "$DOMU"
echo
fi
fi
done
fi
## Check to see if our autostart directory exists and is actually set.
if [ -d $XENDOMAINS_AUTO ] && [ -n $XENDOMAINS_AUTO ]; then
## Start each domain in our autostart directory.
for DOMU in $XENDOMAINS_AUTO/*; do
get_domu_name $DOMU
echo -n $"Starting Xen auto domain $DOMU_NAME: "
## Check if domain is already running.
RESULT=`$CMD list $DOMU_NAME >& /dev/null`
if [ $? -eq 0 ]; then
## DomU is already running.
echo -n " (Skipped - Already running)"
else
## Start the DomU...
RESULT=`$CMD create $DOMU >& /dev/null`
if [ $? -eq 0 ]; then
usleep $XENDOMAINS_CREATE_USLEEP
success
else
failure
fi
fi
echo
done
fi
return 0
}
stop() {
## Check to see if we should send a SysRq to the DomUs
if [ -n "$XENDOMAINS_SYSRQ" ]; then
IFS=$'\n'
echo -n "Sending SYSRQs to Xen Domains:"
for DOMU in `$CMD list | tail -n +3 | sort`; do
unset IFS
DOMU_NAME=`echo "$DOMU" | awk '{ print $1 }'`
for sysrq in $XENDOMAINS_SYSRQ; do
$CMD sysrq $DOMU_NAME $sysrq
echo -n "."
done
done
success
echo
fi
## Check to see if we should migrate running domains...
if [ -n "$XENDOMAINS_MIGRATE" ]; then
## We should be doing a migrate. We don't really do much, just pass the
## values to XM/XL - if it fails, save or shutdown can have a go.
IFS=$'\n'
for DOMU in `$CMD list | tail -n +3 | sort`; do
unset IFS
DOMU_NAME=`echo "$DOMU" | awk '{ print $1 }'`
echo -n "Migrating Xen domain $DOMU_NAME:"
$CMD migrate $DOMU_NAME $XENDOMAINS_MIGRATE 2>&1 1>/dev/null &
PID=$!
watchdog
done
fi
## Check to see if we are to save domains...
if [ -d $XENDOMAINS_SAVE ] && [ -n "$XENDOMAINS_SAVE" ]; then
## Cycle through the domains running and do what we need to do.
IFS=$'\n'
for DOMU in `$CMD list | tail -n +3 | sort`; do
unset IFS
DOMU_NAME=`echo "$DOMU" | awk '{ print $1 }'`
## Now we set our ionice to lower than realtime as we don't want the
## system to become unresponsive while saving a DomU. It still may
## need to fulfil requests from other DomUs etc.
ionice -c 2 -n 7 -p $$
echo -n "Saving Xen Domain $DOMU_NAME:"
$CMD save $DOMU_NAME $XENDOMAINS_SAVE/$DOMU_NAME > /dev/null 2>&1 &
PID=$!
watchdog
done
fi
## Shut down the remaining domains...
if [ -n "$XENDOMAINS_SHUTDOWN" ]; then
IFS=$'\n'
for DOMU in `$CMD list | tail -n +3 | sort`; do
unset IFS
DOMU_NAME=`echo "$DOMU" | awk '{ print $1 }'`
echo -n "Shutting down $DOMU_NAME:"
$CMD shutdown $DOMU_NAME $XENDOMAINS_SHUTDOWN 2>&1 1>/dev/null &
PID=$!
watchdog
done
fi
rm -f $LOCKFILE
return 0
}
status() {
## We've already passed the test to see if Xen Dom0 is running, so here
## we check the number of lines returned from a list command and minus
## 2 from it (Header + Domain-0) and also some memory stats.
NUM_DOMU=$((`$CMD list | wc -l`-2))
MEM_FREE=`$CMD info | grep free_memory | cut -f2 -d":"`
MEM_TOTAL=`$CMD info | grep total_memory | cut -f2 -d":"`
echo "Xen running with $NUM_DOMU DomUs.$MEM_FREE MB of$MEM_TOTAL MB free."
return 0
}
case "$1" in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
status)
status
RETVAL=$?
;;
restart)
stop
start
;;
esac
More information about the kernel-xen
mailing list