#!/bin/sh
#
# vrootdevices:	setup vroot devices
#
# chkconfig:	345 97 03
# description:	Setup vroot devices for use inside vservers
#
# $Id: vrootdevices.init,v 1.5 2008/04/29 20:43:37 glen Exp $

# Source function library
. /etc/rc.d/init.d/functions

assign_vroot()
{
	cat /etc/sysconfig/vrootdevices | egrep -v '^ *#' | while read VROOT BLOCK; do
	if [ -n "$VROOT" -a -n "$BLOCK" ]; then
		if [ -d /dev/vroot ]; then
			VROOT="/dev/vroot/$VROOT"
		else
			VROOT="/dev/vroot$VROOT"
		fi
		show "$(nls "Assigning device: %s --> %s" "$VROOT" "$BLOCK")"
		busy
		vrsetup $VROOT $BLOCK > /dev/null
		ok
	fi
	done
}

remove_vroot()
{
	cat /etc/sysconfig/vrootdevices | egrep -v '^ *#' | while read VROOT BLOCK; do
	if [ -n "$VROOT" -a -n "$BLOCK" ]; then
		if [ -d /dev/vroot ]; then
			VROOT="/dev/vroot/$VROOT"
		else
			VROOT="/dev/vroot$VROOT"
		fi
		show "$(nls "Removing assignment: %s --> %s" "$VROOT" "$BLOCK")"
		busy
		vrsetup -d $VROOT > /dev/null
		ok
	fi
	done
}

start() {
	if [ ! -f /var/lock/subsys/vrootdevices ]; then
		msg_starting vrootdevices
		ok
		assign_vroot
		touch /var/lock/subsys/vrootdevices
	else
		msg_already_running vrootdevices
	fi
}

stop() {
	if [ -f /var/lock/subsys/vrootdevices ]; then
		msg_stopping vrootdevices
		ok
		remove_vroot
		rm -f /var/lock/subsys/vrootdevices >/dev/null 2>&1
	else
		msg_not_running vrootdevices
	fi
}

case "$1" in
  start|reload)
  	start
	;;
  stop)
  	stop
	;;
  restart|force-reload)
	stop
	start
	;;
  status)
	echo "There is no way to tell"
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
	;;
esac

exit $RETVAL
