#!/bin/sh
#  Copyright (C) 2000-2007 SWsoft. All rights reserved.
#  Copyright (C) 2006-2007 Dmitry V. Levin <ldv@altlinux.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
# chkconfig: - 96 88
# description: OpenVZ startup script.
#
### BEGIN INIT INFO
# Provides: vz
# required-start: $network $remote_fs $local_fs sshd
# required-stop:
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: OpenVZ startup script
# Description: OpenVZ startup script.
### END INIT INFO

WITHOUT_RC_COMPAT=1

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

VZCONF="/etc/vz/vz.conf"
CONFIG_DIR="/etc/vz/conf"
LOCKFILE=/var/lock/subsys/vz
PROC_VZ_VEINFO=/proc/vz/veinfo
PROC_VZ_VESTAT=/proc/vz/vestat
VZDEV=venet0
MODULES=
MODULES_OTHER=
NET_MODULES=
PRELOAD_MODULES=
IPT_MODULES=

SourceIfNotEmpty "$VZCONF" || exit 0
is_no "$VIRTUOZZO" && exit 0

if ! is_yes "$MODULES_DISABLED"; then
	PRELOAD_MODULES=
	MODULES="af_packet vzmon vzdquota vzdev"
	CPT_MODULES="vzcpt vzrst"
	MODULES_OTHER="vzcompat ${CPT_MODULES}"
	VNET_MODULES="vznetdev vznet"
	VETH_MODULES="vzethdev"
	NET_MODULES="${VNET_MODULES} ${VETH_MODULES}"
	IPT_MODULES="ip_tables ${IPTABLES} xt_tcpudp"
	is_yes "$IPV6" && IPT_MODULES="$IPT_MODULES $IP6TABLES"
	is_yes "$VZWDOG" && MODULES="${MODULES} vzwdog"
fi

VEINFO=
RETVAL=0
# Number of the parallel VEs on stop.
# In case empty value the number of parallel VEs calculated as 'num_cpu * 4'
PARALLEL=
cd /

get_veinfo()
{
	if [ -f /proc/vz/veinfo ]; then
		VEINFO=/proc/vz/veinfo
	elif [ -f /proc/veinfo ]; then
		VEINFO=/proc/veinfo
	elif [ ! -f $PROC_VZ_VESTAT ]; then
		return 1
	fi
	return 0
}

is_running()
{
	[ -f "$LOCKFILE" ] && get_veinfo
}

check_kernel()
{
	[ -d /proc/vz ]
}

get_parallel()
{
	[ -z "$PARALLEL" ] || return 0
	local NPROCS
	NPROCS=`egrep -cs ^cpu[0-9]+ /proc/stat`
	[ "$NPROCS" -gt 0 ] 2>/dev/null || NPROCS=1
	PARALLEL="$(($NPROCS*4))"
}

# We used to install OpenVZ cron job when the vzctl package was
# installed, irrespective of whether OpenVZ was actually being
# run. Although the cron jobs didn't create any problems if someone
# wasn't running OpenVZ some users complained about the cron log file
# filling up, resource usage, and power consumption since systems
# wouldn't really idle. It really only makes sense to run the OpenVZ
# cron job if the vz service is turned on and not just merely
# having the package installed. This init.d script is an obvious place
# to install or remove the cron jobs based on the service
# being enabled or not.
SRC_CRONSCRIPT_DIR=/etc/vz/cron
DST_CRONSCRIPT_DIR=/etc/cron.d

check_old_cron_files()
{
	# avoid duplicated OpenVZ cron settings
	local f
	for f in vpsreboot vpsnetclean; do
		[ -f "$DST_CRONSCRIPT_DIR/$f" ] || continue
		rm -f "$DST_CRONSCRIPT_DIR/$f"
	done
}

setup_cron()
{
	check_old_cron_files
	[ -d "$SRC_CRONSCRIPT_DIR" ] || return
	cat "$SRC_CRONSCRIPT_DIR"/vz* > "$DST_CRONSCRIPT_DIR"/.vz.$$ &&
		mv "$DST_CRONSCRIPT_DIR"/.vz.$$ "$DST_CRONSCRIPT_DIR"/vz &&
		chmod 644 "$DST_CRONSCRIPT_DIR"/vz
}

remove_cron()
{
	check_old_cron_files
	[ -d "$SRC_CRONSCRIPT_DIR" ] || return
	cat > "$DST_CRONSCRIPT_DIR"/vz <<__EOF__
# DO NOT EDIT THIS FILE!
#
# Contents of this file managed by /etc/init.d/vz script
# Master copy is in $SRC_CRONSCRIPT_DIR/vz* file(s).
# Consult these files for documentation.
EOF
__EOF__
}

# Actualize OpenVZ cron entry:
# if OpenVZ is running, add it, otherwise remove.
update_cron()
{
	if is_running; then
		printf %s $'Adding OpenVZ cron entries'
		setup_cron
	else
		printf %s $'Removing OpenVZ cron entries'
		remove_cron
	fi && echo_success || echo_failure
}

status()
{
	if ! check_kernel; then
		echo "Running kernel is not OpenVZ capable."
		return 1
	fi
	if is_running; then
		echo "OpenVZ is running."
		return 0
	else
		echo "OpenVZ is stopped."
		return 3
	fi
}

load_modules()
{
	local mod rc=0
	for mod; do
		! lsmod |grep -qs "^$mod[[:space:]]" || continue
		modinfo "$mod" >/dev/null 2>&1 || continue
		action "Loading module $mod:" modprobe "$mod"
		[ "$rc" = 0 ] || rc=$?
	done
	return $rc
}

unload_modules()
{
	local mod rc=0
	for mod; do
		lsmod |grep -qs "^$mod[[:space:]]" || continue
		printf %s "Unloading module $mod: "
		modprobe -r "$mod" >/dev/null 2>&1 &&
			success "$STRING" || passed "$STRING"
		[ "$rc" = 0 ] || rc=$?
		echo
	done
	return $rc
}

setup_net()
{
	local mod

	load_modules ${NET_MODULES} || return
	if ip addr list | grep -qs "venet0:.*UP"; then
		return 0
	fi
	get_veinfo
	[ -n "$VEINFO" ] || return
	action "Bringing up interface $VZDEV:" \
		ip link set ${VZDEV} up || return
	action "Assigning address to interface $VZDEV:" \
		ip addr add 0.0.0.0/0 dev ${VZDEV} || return
	if [ "$(sysctl -n net.ipv4.conf.$VZDEV.forwarding)" = 0 ]; then
		action "Enabling IPv4 packet forwarding:" \
			sysctl -w net.ipv4.ip_forward=1
	fi
	action "Configuring interface $VZDEV:" \
		sysctl -w net.ipv4.conf.${VZDEV}.send_redirects=0
	if is_yes "$IPV6"; then
		action "Configuring IPv6 for interface $VZDEV:" \
			ip -6 addr add fe80::1/128 dev ${VZDEV} || return
	fi
}

setup_ve0()
{
	if test -z "${VE0CPUUNITS}"; then
		printf "VE0CPUUNITS is not set in %s; using value of 1000" ${VZCONF}
		passed VE0CPUUNITS
		VE0CPUUNITS=1000
	fi
	action "Configuring cpuunits limit for VE0 to $VE0CPUUNITS:" \
		vzctl set 0 --cpuunits ${VE0CPUUNITS} || return
	test -s "${CONFIG_DIR}/0.conf" || return
	grep -qs '^ONBOOT=yes\|^ONBOOT=\"yes\"' ${CONFIG_DIR}/0.conf || return 0
	action "Configuring node UB resources:" \
		vzctl set 0 --reset_ub
}

start_ves()
{
	local veid
	local velist
	local need_restart=

	cd ${CONFIG_DIR} || return
	velist=`grep -l '^ONBOOT=yes\|^ONBOOT=\"yes\"' [0-9]*.conf 2>/dev/null |
		sed -e 's/\.conf$//g' | sort -n`
	cd - >/dev/null
	sysctl -w net.ipv4.route.src_check=0
	for veid in ${velist}; do
		[ "$veid" != "0" ] || continue
		if is_yes "$VZFASTBOOT" && is_yes "$DISK_QUOTA"; then
			printf "Preparing VE %s quota:" ${veid}
			vzquota stat ${veid} >/dev/null 2>&1
			if [ $? -eq 6 ]; then
				if vzquota show ${veid} 2>&1 |
				   grep -qs "vzquota : (warning) Quota is running"; then
					vzquota on ${veid} --nocheck >/dev/null 2>&1
					need_restart="${need_restart} ${veid}"
				fi
			fi
			success "Preparing VE %s quota"
		fi
		action "Starting VE $veid:" \
			vzctl start ${veid}
	done
	for veid in ${need_restart}; do
		action "Stopping VE $veid:" \
			vzctl stop ${veid}
		action "Starting VE $veid:" \
			vzctl start ${veid}
	done
}

ve_stop()
{
	local veid
	local iter
	local pids

	if get_veinfo; then
		get_parallel
		for i in 0 1 2; do
			iter=0
			pids=
			for veid in `awk '$1 != "VEID" && $1 != "Version:" {print $1}' ${PROC_VZ_VESTAT}`; do
				echo "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 $!"
				iter=$(($iter+1))
				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

	UnmountFilesystems 3 5 \
		'($3=="simfs") {print $2}' \
		"Unmounting VE area" \
		"Unmounting VE area (retry)"

	for veid in $(awk -F: '/^[0-9]+:/{print $1}' </proc/vz/vzquota 2>/dev/null); do
		action "Turning off quota for VE $veid:" \
			vzquota off ${veid}
	done
}

lockfile()
{
	local tempfile lockfile="${1}"

	tempfile="$(mktemp ${lockfile}.XXXXXX)" || return 1
	echo $$ > ${tempfile} || return 1

	if ln ${tempfile} ${lockfile} >/dev/null 2>&1; then
		rm -f ${tempfile}
		return 0
	fi
	if kill -0 `cat $lockfile` >/dev/null 2>&1; then
		rm -f ${tempfile}
		return 1
	fi
	if ln -f ${tempfile} ${lockfile}; then
		rm -f ${tempfile}
		return 0
	fi
	rm -f ${tempfile}
	return 1
}

locked_start()
{
	local veid
	local velist
	local msg

	if [ -f "$LOCKFILE" ]; then
		msg_already_running vz
		passed "OpenVZ startup"
		echo
		RETVAL=$?
		return $RETVAL
	fi

	load_modules ${IPT_MODULES}
	unload_modules ${PRELOAD_MODULES}

	load_modules ${PRELOAD_MODULES} ${MODULES}
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		return $RETVAL
	fi

	load_modules ${MODULES_OTHER}

	if [ ! -e /dev/vzctl ]; then
		# On most modern distros udev will create a device for you,
		# while on the old distros /dev/vzctl comes with vzctl rpm.
		# So the below mknod call is probably not needed at all.
		action "Creating vzctl device:" \
			mknod -m 600 /dev/vzctl c 126 0
		RETVAL=$?
		if [ $RETVAL -ne 0 ]; then
			return $RETVAL
		fi
	fi

	setup_net
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		return $RETVAL
	fi

	setup_ve0
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		return $RETVAL
	fi

	setup_cron
	start_ves

	touch "$LOCKFILE"
}

locked_stop()
{
	remove_cron
	ve_stop
	action "Shutting down interface $VZDEV:" \
		ip link set ${VZDEV} down
	unload_modules ${MODULES} ${PRELOAD_MODULES} ${IPT_MODULES2} ${NET_MODULES}
	rm -f "$LOCKFILE"
}

start()
{
	if ! check_kernel; then
		action 'Checking OpenVZ kernel' false
		RETVAL=1
		return $RETVAL
	fi
	if ! lockfile ${LOCKFILE}_lock; then
		printf %s "OpenVZ is locked"
		failure "OpenVZ startup"
		echo
		RETVAL=$?
		return $RETVAL
	fi
	locked_start
	RETVAL=$?
	rm -f ${LOCKFILE}_lock
}

stop()
{
	if ! lockfile ${LOCKFILE}_lock; then
		printf %s "OpenVZ is locked"
		failure "OpenVZ startup"
		echo
		RETVAL=$?
		return $RETVAL
	fi
	locked_stop
	RETVAL=$?
	rm -f ${LOCKFILE}_lock
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	status)
		status
		RETVAL=$?
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	update-cron)
		update_cron
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|status|restart|condstop|condrestart|update-cron}"
		RETVAL=1
esac

exit $RETVAL

