#!/sbin/runscript
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-cluster/util-vserver/files/0.30.208-r1/vservers.initd,v 1.1 2005/08/03 18:33:37 hollow Exp $

opts="${opts} vstatus"

checkconfig() {
	: ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
	if [[ ! -e ${UTIL_VSERVER_VARS} ]]; then
		eerror "Cannot find util-vserver installation"
		eerror "(the file '$UTIL_VSERVER_VARS' would be expected)"
		exit 1
	fi
	. ${UTIL_VSERVER_VARS}
	
	: ${MARKS:=default}
	: ${NUMPARALLEL:=1}
	: ${LOCKDIR:=/var/lock/vservers}
}

start() {
	checkconfig
	
	${_SETATTR} -Rx --hide /proc
	
	ebegin "Unhiding /proc entries"
	${_VPROCUNHIDE}
	eend $?
	
	if [[ -n ${STARTALL} ]]; then
		ebegin "Starting all types of vservers"
		${_START_VSERVERS} -j ${NUMPARALLEL} --all --start
		local rc=$?
		[[ $rc -eq 0 ]] && touch ${LOCKDIR}/all
		eend $rc
	else
		for MARK in ${MARKS}; do
			ebegin "Starting vservers of type '${MARK}'"
			${_START_VSERVERS} -m ${MARK} -j ${NUMPARALLEL} --all --start
			local rc=$?
			[[ $rc -eq 0 ]] && touch ${LOCKDIR}/${MARK}
			eend $rc
		done
	fi
	
	for VSERVER in ${START_VSERVERS}; do
		ebegin "Starting single vserver '${VSERVER}'"
		${_VSERVER} ${VSERVER} start
		eend $?
	done
}

stop() {
	checkconfig
	
	ebegin "Stopping all types of vservers"
	${_START_VSERVERS} -j ${NUMPARALLEL} --all --stop
	eend $?
	
	rm -f ${LOCKDIR}/*
	
	einfo "Checking for vservers still running ..."
	
	for i in $( find /proc/virtual/ -noleaf -mindepth 1 -maxdepth 1 -type d | sort ); do
		NAME=$( basename $( vuname -g --xid $( basename ${i} ) CONTEX ) )
		ebegin "  Stopping single vserver: ${NAME} )"
		/usr/sbin/vserver ${NAME} stop &> /dev/null
		eend $?
	done
	
	ebegin "Hiding /proc entries"
	${_SETATTR} -Rx --hide /proc
	eend 0
}

vstatus() {
	checkconfig

	einfo "${LOCKDIR} shows the following types of vservers running:"
	
	for i in $( ls ${LOCKDIR} | sort ); do
		einfo "  ${i}"
		local running="true"
	done
	
	if [[ "${running}" != "true" ]]; then
		einfo "  none"
	fi
	
	einfo
	einfo "/proc/virtual/ says these are running:"
	
	for i in $( find /proc/virtual/ -noleaf -mindepth 1 -maxdepth 1 -type d | sort ); do
		NAME=$( basename $( vuname -g --xid $( basename ${i} ) CONTEX ) )
		einfo "  ${NAME}"
	done
	
	einfo
	einfo "vserver-stat says these are running:"
	/usr/sbin/vserver-stat
}
