#!/bin/bash
#
# xencommons    Script to start and stop xenstored and xenconsoled
#
# Author:       Ian Jackson <ian.jackson@eu.citrix.com>
#
# chkconfig: 2345 70 10
# description: Starts and stops xenstored and xenconsoled
### BEGIN INIT INFO
# Provides:          xenstored xenconsoled
# Required-Start:    $syslog $remote_fs
# Should-Start:
# Required-Stop:     $syslog $remote_fs
# Should-Stop:
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop xenstored and xenconsoled
# Description:       Starts and stops the daemons neeeded for xl/xend
### END INIT INFO

if [ -d /etc/sysconfig ]; then
	xencommons_config=/etc/sysconfig
else
	xencommons_config=/etc/default
fi

test -f $xencommons_config/xencommons && . $xencommons_config/xencommons

XENCONSOLED_PIDFILE=/var/run/xenconsoled.pid
XEN_CAPABILITIES=/proc/xen/capabilities
shopt -s extglob

# not running in Xen dom0 or domU
if ! test -d /proc/xen ; then
      exit 0
fi

# mount xenfs in dom0 or domU with a pv_ops kernel
if test "x$1" = xstart && \
   ! test -f $XEN_CAPABILITIES && \
   ! grep -q '^xenfs ' /proc/mounts ;
then
      mount -t xenfs xenfs /proc/xen
fi

# run this script only in dom0:
# no capabilities file in xenlinux kernel
if ! test -f $XEN_CAPABILITIES ; then
      exit 0
fi
# empty capabilities file in pv_ops kernel
if ! grep -q "control_d" $XEN_CAPABILITIES ; then
      exit 0
fi

do_start () {
        local time=0
	local timeout=30

	modprobe xen-evtchn 2>/dev/null
	modprobe xen-gntdev 2>/dev/null
	modprobe evtchn 2>/dev/null
	modprobe gntdev 2>/dev/null

	# Load XEN backend modules
	# NB: They could be loaded later, e.g. when dom0 hotplug events occur,
	# but for now it's safest to have them loaded here.
	modprobe evtchn 2>/dev/null || true
	modprobe gntdev 2>/dev/null || true
	modprobe blktap 2>/dev/null || true
	modprobe blkbk 2>/dev/null || true
	modprobe netbk 2>/dev/null || true
	modprobe usbbk 2>/dev/null || true
	# xenblk (frontend module) is needed in dom0, allowing it to use vbds
	modprobe xenblk 2>/dev/null || true
	# support xl create pv guest with qcow/qcow2 disk image
	modprobe nbd max_part=8 2>/dev/null || true

	if ! `xenstore-read -s / >/dev/null 2>&1`
	then
		test -z "$XENSTORED_ROOTDIR" && XENSTORED_ROOTDIR="/var/lib/xenstored"
		rm -f "$XENSTORED_ROOTDIR"/tdb* &>/dev/null
		test -z "$XENSTORED_TRACE" || XENSTORED_ARGS=" -T /var/log/xen/xenstored-trace.log"

		echo -n Starting xenstored...
		xenstored --pid-file=/var/run/xenstored.pid $XENSTORED_ARGS

		# Wait for xenstored to actually come up, timing out after 30 seconds
                while [ $time -lt $timeout ] && ! `xenstore-read -s / >/dev/null 2>&1` ; do
                    echo -n .
		    time=$(($time+1))
                    sleep 1
                done
		echo

		# Exit if we timed out
		if ! [ $time -lt $timeout ] ; then
		    echo Could not start xenstored
		    exit 1
		fi

		echo Setting domain 0 name...
		xenstore-write "/local/domain/0/name" "Domain-0"
	fi

	echo Starting xenconsoled...
	test -z "$XENCONSOLED_TRACE" || XENCONSOLED_ARGS=" --log=$XENCONSOLED_TRACE"
	xenconsoled --pid-file=$XENCONSOLED_PIDFILE $XENCONSOLED_ARGS
	test -z "$XENBACKENDD_DEBUG" || XENBACKENDD_ARGS="-d"
	test "`uname`" != "NetBSD" || xenbackendd $XENBACKENDD_ARGS
}
do_stop () {
        echo Stopping xenconsoled
	if read 2>/dev/null <$XENCONSOLED_PIDFILE pid; then
		kill $pid
		while kill -9 $pid >/dev/null 2>&1; do sleep 0.1; done
		rm -f $XENCONSOLED_PIDFILE
	fi

	echo WARNING: Not stopping xenstored, as it cannot be restarted.
}

case "$1" in
  start)
	do_start
	;;
  status)
        xenstore-read -s /
	;;
  stop)
	do_stop
	;;
  reload)
	echo >&2 'Reload not available; use force-reload'; exit 1
	;;
  force-reload|restart)
        do_stop
	do_start
	;;
  *)
	# do not advertise unreasonable commands that there is no reason
	# to use with this device
	echo $"Usage: $0 {start|stop|status|restart|force-reload}"
	exit 1
esac

exit $?
