#!/bin/sh
#
# xen-qemu-dom0	qemu for xen dom0 disk backend
#
# chkconfig	- 71 29
# description:	It is used if dom0 needs to be able to access a virtual disk in a
#   non-trivial form, i.e. qcow or vhd. In this case a vbd is attached to
#   dom0 which is backed by qdisk in this qemu process. For trivial disk
#   formats like raw files or block partitions dom0 can just access them
#   directly.
# processname:	qemu-system-i386
### BEGIN INIT INFO
# Provides:          qemu-system-i386
# Required-Start:    $syslog $remote_fs
# Should-Start:
# Required-Stop:     $syslog $remote_fs
# Should-Stop:
# Default-Start:
# Default-Stop:      0 1 2 3 4 5 6
# Short-Description: qemu for xen dom0 disk backend
# Description:       It is used if dom0 needs to be able to access a virtual disk in a
#   non-trivial form, i.e. qcow or vhd. In this case a vbd is attached to
#   dom0 which is backed by qdisk in this qemu process. For trivial disk
#   formats like raw files or block partitions dom0 can just access them
#   directly.
### END INIT INFO

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

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

WITHOUT_RC_COMPAT=1
# Source function library.
. /etc/init.d/functions

PIDFILE=/var/run/xen/qemu-dom0.pid
QEMU_XEN=/usr/lib/xen/bin/qemu-system-i386

RETVAL=0

start()
{
	echo -n "Starting Qemu as disk backend for dom0: "
	start_daemon --no-announce --lockfile "$LOCKFILE" --pidfile "$PIDFILE" --expect-user root \
		$QEMU_XEN -xen-domid 0 -xen-attach -name dom0 -nographic -M xenpv -daemonize \
		-monitor /dev/null -serial /dev/null -parallel /dev/null -pidfile $PIDFILE \
		-nodefaults -no-user-config

	RETVAL=$?
	return $RETVAL
}

stop()
{
	echo -n "Stopping Qemu (disk backend for dom0): "
	stop_daemon --no-announce --lockfile "$LOCKFILE" --pidfile "$PIDFILE" --expect-user root -- "$QEMU_XEN"
	RETVAL=$?
	return $RETVAL
}

restart()
{
	echo -n "Restarting Qemu (disk backend for dom0) is unsafe: "
	passed
	echo
}

forcerestart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		restart
		;;
	forcerestart)
		forcerestart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart|condreload)
		;;
	status)
		status --lockfile "$LOCKFILE" --pidfile "$PIDFILE" --expect-user root qemu-system-i386
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|forcerestart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
