#!/sbin/runscript
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/www/viewcvs.gentoo.org/raw_cvs/gentoo-x86/sys-cluster/vzctl/files/vz-3.0.10.initd,v 1.2 2006/07/09 10:29:24 phreak Exp $

depend() {
	after net
}

check_config() {
	# if we don't want openvz running, say so
	[ "${VIRTUOZZO}" = "yes" ] || return 1

	# set default values
	: ${VZCTL:=@SBINDIR@/vzctl}
	: ${VZQUOTA:=@SBINDIR@/vzquota}
	: ${VZVEINFO:=/proc/vz/veinfo}
	: ${VESTAT:=/proc/vz/vestat}
	: ${VPSCONFDIR:=@VPSCONFDIR@}
	: ${VZDEV:=venet0}

	if [ "${MODULES_DISABLED}" != "yes" ]; then
		: ${PRELOAD_MODULES:="af_packet"}
		: ${MODULES:="vzmon vzdquota vzdev"}
		: ${MIGRATE_MODULES:="vzcpt vzrst"}
		: ${NET_MODULES="vznetdev vznet"}

		# check if you should load vzwdog module
		[ "${VZWDOG}" = "yes" ] && MODULES="${MODULES} vzwdog"
	fi

	# we need a working vzctl
	if [ ! -x "${VZCTL}" ]; then
		eerror "vzctl missing (${VZCTL})"
		return 1
	fi

	if [ -z "${VE0CPUUNITS}" ]; then
		ewarn "VE0CPUUNITS is not set in /etc/conf.d/vz; using default value of 1000"
		VE0CPUUNITS=1000
	fi
	return 0
}

setup_ve0() {
	local msg ve0conf="${VPSCONFDIR}/0.conf"

	msg=$(${VZCTL} set 0 --cpuunits ${VE0CPUUNITS} 2>&1)
	[ $? -ne 0 ] && ewarn "vzctl set 0 --cpuunits ${VE0CPUUNITS} failed: ${msg}"

	test -f ${ve0conf} || return
	egrep -q '^ONBOOT=yes\|^ONBOOT=\"yes\"' ${ve0conf} || return

	ebegin "Configuring hardware node UB resources: "
	msg=$(${VZCTL} set 0 --reset_ub 2>&1)
	eend $? ${msg}
}

start_net() {
	local mod

	# we don't operate on a running interface
	if /sbin/ip addr ls | grep -q "venet0:.*UP" 2>/dev/null; then
		return 0
	fi

	# load necessary modules
	for mod in ${NET_MODULES}; do
		/sbin/modprobe ${mod} 2>/dev/null
	done

	if [ ! -f ${VZVEINFO} ]; then
		return 0
	fi

	# configure the device
	ebegin "Bringing up interface ${VZDEV}"
		/sbin/ip link set ${VZDEV} up
	eend $?

	/sbin/ip addr add 0.0.0.0/0 dev ${VZDEV}

	ebegin "Configuring interface ${VZDEV}"
		/sbin/sysctl -q -w net.ipv4.conf.${VZDEV}.send_redirects=0
	eend $?
}

stop_net() {
	local mod

	if /sbin/ip a l | grep -q "venet0:.*UP" 2>/dev/null; then
		ebegin "Bringing down interface ${VZDEV}"
			/sbin/ip link set ${VZDEV} down 2>/dev/null
		eend $?
	fi

	# remove all modules we probably loaded on start_net
	for mod in ${NET_MODULES}; do
		/sbin/modprobe -r ${mod} > /dev/null 2>&1
	done
}

start_ve() {
	local veid velist msg need_restart=""

	# get all VEs we should start on boot
	if ! cd ${VPSCONFDIR}; then
		eerror $? "Unable to cd to ${VPSCONFDIR}"
		return 1
	fi
	velist=$(grep -l '^ONBOOT=yes\|^ONBOOT=\"yes\"' [0-9]*.conf 2>/dev/null | sed -e 's/.conf//g' | sort -n)
	cd - >/dev/null

	/sbin/sysctl -q -w net.ipv4.route.src_check=0

	for veid in ${velist}; do
		ebegin "Starting VE ${veid}"
		if [ "${VZFASTBOOT}" = "yes" -a "${DISK_QUOTA}" = "yes" ]; then
			${VZQUOTA} stat ${veid} >/dev/null 2>&1
			if [ $? -eq 6 ]; then
				if ${VZQUOTA} show ${veid} 2>&1 | grep "vzquota : (warning) Quota is running" >/dev/null 2>&1; then
					${VZQUOTA} on ${veid} --nocheck >/dev/null 2>&1
					need_restart="${need_restart} ${veid}"
				fi
			fi
		fi
		msg=$(${VZCTL} start ${veid} 2>&1)
		eend $? "${msg}"
	done

	for veid in ${need_restart}; do
		ebegin "Stopping VE ${veid}"
		msg=$(${VZCTL} stop ${veid})
		eend $? "${msg}"

		ebegin "Starting VE ${veid}"
		msg=$($VZCTL start ${veid} 2>&1)
		eend $? "${msg}"
	done

	# we're ok even if some VEs failed to start
	return 0
}

get_parallel()
{
	[ -n "${PARALLEL}" ] && return
	PARALLEL=`awk '
BEGIN { num=0; }
$1 == "processor" { num++; }
END { print num * 4; }' /proc/cpuinfo`
}

stop_ve() {
	local veid velist msg m mounts fail i iter pid pids quota

	if [ -f ${VESTAT} ]; then
		get_parallel
		for ((i = 0; i <= 2; i++)); do
			iter=0
			pids=
			velist=$(awk '$1 != "VEID" && $1 != "Version:" {print $1}' ${VESTAT})
			for veid in ${velist}; do
				ebegin "Shutting down VE $veid"
				# Set fairsched parameters to maximum so
				# VE will stop fast
				${VZCTL} set ${veid} --cpuunits 2000 --cpulimit 0 >/dev/null 2>&1
				${VZCTL} --skiplock stop ${veid} >/dev/null 2>&1 &
				pids="${pids} $!"
				let iter++
				if [ ${iter} -gt ${PARALLEL} ]; then
					for pid in ${pids}; do
						wait ${pid}
					done
					pids=
					iter=0
				fi
			done

			for pid in ${pids}; do
				wait ${pid}
			done
		done
	fi

	iter=0
	fail=1

	while [ ${iter} -lt 5 -a ${fail} -ne 0 ]; do
		fail=0
		mounts=$(awk '{if ($3=="simfs") print $2}' /proc/mounts)

		for m in ${mounts}; do
			ebegin "Unmounting VE area ${m}"
			msg=$(umount ${m} 2>&1)
			eend $? "${msg}"
			if [ $? -ne 0 ]; then
				let fail++
				fuser -k -m ${m} > /dev/null 2>&1
			fi
		done

		let iter++
	done

	# turn quota off
	quota=$(awk -F: '/^[0-9]+:/{print $1}' /proc/vz/vzquota 2>/dev/null)

	for m in ${quota}; do
		ebegin "Turning quota off for VE ${m}"
		msg=$(vzquota off ${m} 2>&1)
		eend $? "${msg}"
	done
}

start() {
	check_config || return

	local mod rc

	ebegin "Loading OpenVZ modules"
	for mod in ${PRELOAD_MODULES}; do
		/sbin/modprobe -r ${mod} >/dev/null 2>&1
		/sbin/modprobe ${mod} >/dev/null 2>&1
	done

	for mod in ${MODULES}; do
		/sbin/modprobe ${mod} >/dev/null 2>&1
		rc=$?
		if [ ${rc} -ne 0 ]; then
			eend ${rc} "failed to load module ${mod}"
			return ${rc}
		fi
	done

	for mod in ${MIGRATE_MODULES}; do
		/sbin/modprobe ${mod} >/dev/null 2>&1
	done
	eend

	if [ ! -e /dev/vzctl ]; then
		eerror "Missing device node /dev/vzctl"
		einfo
		einfo "Please create the vzctl device node using the following command:"
		einfo "   /bin/mknod /dev/vzctl c 126 0"
		einfo
		return 1
	fi

	start_net

	setup_ve0

	start_ve
}

stop() {
	check_config || return

	local mod

	stop_ve
	stop_net

	for mod in ${MODULES}; do
		/sbin/modprobe -r ${mod} > /dev/null 2>&1
	done

	for mod in ${PRELOAD_MODULES}; do
		/sbin/modprobe -r ${mod} > /dev/null 2>&1
	done

	# Even if some modules failed to unload (say they were not loaded)
	# we return 0 for the service to be marked as stopped.
	return 0
}
