#!/bin/bash # $Header: /root/magellan-cvs/src/vzctl/vz.rc,v 1.2 2007-07-21 23:27:49 niro Exp $ #%rlevels: 3:s 4:s 5:s 0:k 1:k 2:k 6:k #%start: 96 #%stop: 30 #deps #%needs: #%before: #%after: source /etc/sysconfig/rc source $rc_functions check_config() { source /etc/vz/vz.conf # if we don't want openvz running, say so [ "${VIRTUOZZO}" = "yes" ] || return 1 # set default values : ${VZCTL:=/usr/sbin/vzctl} : ${VZQUOTA:=/usr/sbin/vzquota} : ${VZVEINFO:=/proc/vz/veinfo} : ${VESTAT:=/proc/vz/vestat} : ${VPSCONFDIR:=/etc/vz/conf} : ${VZDEV:=venet0} : ${SRC_CRONSCRIPT_DIR:=/etc/vz/cron} : ${DST_CRONSCRIPT_DIR:=/etc/cron.d} 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 ${FAILURE} "vzctl missing (${VZCTL})" return 1 fi if [ -z "${VE0CPUUNITS}" ] then ${WARNING} "VE0CPUUNITS is not set in /etc/conf.d/vz; using default value of 1000" VE0CPUUNITS=1000 fi return 0 } # 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. setup_cron() { [ -z "${SRC_CRONSCRIPT_DIR}" ] && return [ -d "${SRC_CRONSCRIPT_DIR}" ] || return install -m644 -o root -g root ${SRC_CRONSCRIPT_DIR}/vz ${DST_CRONSCRIPT_DIR} } remove_cron() { [ -z "${SRC_CRONSCRIPT_DIR}" ] && return [ -d "${SRC_CRONSCRIPT_DIR}" ] || return cat > ${DST_CRONSCRIPT_DIR}/vz <&1) [ $? -ne 0 ] && ${WARNING} "vzctl set 0 --cpuunits ${VE0CPUUNITS} failed: ${msg}" test -f ${ve0conf} || return egrep -q '^ONBOOT=yes\|^ONBOOT=\"yes\"' ${ve0conf} || return echo -en ${COLOREDSTAR}"Configuring hardware node UB resources: " msg=$(${VZCTL} set 0 --reset_ub 2>&1) retval=$? echo "${msg}" evaluate_retval ${retval} } start_net() { local mod # we don't operate on a running interface if /sbin/ip addr list | 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 echo -e ${COLOREDSTAR}"Bringing up interface ${VZDEV}" /sbin/ip link set ${VZDEV} up evaluate_retval /sbin/ip addr add 0.0.0.0/0 dev ${VZDEV} echo -e ${COLOREDSTAR}"Configuring interface ${VZDEV}" /sbin/sysctl -q -w net.ipv4.conf.${VZDEV}.send_redirects=0 evaluate_retval } stop_net() { local mod if /sbin/ip addr list | grep -q "venet0:.*UP" 2>/dev/null then echo -e ${COLOREDSTAR}"Bringing down interface ${VZDEV}" /sbin/ip link set ${VZDEV} down 2>/dev/null evaluate_retval 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="" retval # get all VEs we should start on boot if ! cd ${VPSCONFDIR} then ${FAILURE} "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 echo -en ${COLOREDSTAR}"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) retval=$? echo "${mesg}" evaluate_retval ${retval} done for veid in ${need_restart} do echo -en ${COLOREDSTAR}"Stopping VE ${veid}" msg=$(${VZCTL} stop ${veid}) retval=$? echo "${mesg}" evaluate_retval ${retval} echo -e ${COLOREDSTAR}"Starting VE ${veid}" msg=$($VZCTL start ${veid} 2>&1) retval=$? echo "${mesg}" evaluate_retval ${retval} 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 retval 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 echo -e ${COLOREDSTAR}"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 echo -en ${COLOREDSTAR}"Unmounting VE area ${m}" msg=$(umount ${m} 2>&1) retval=$? echo "${mesg}" evaluate_retval ${retval} 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 echo -e ${COLOREDSTAR}"Turning quota off for VE ${m}" msg=$(vzquota off ${m} 2>&1) retval=$? echo "${mesg}" evaluate_retval ${retval} done } status_ve() { local velist veid check_config if [ -f ${VESTAT} ] then velist=$(awk '$1 != "VEID" && $1 != "Version:" {print $1}' ${VESTAT}) for veid in ${velist} do ${VZCTL} status ${veid} done fi } case "$1" in start) check_config || exit echo -e ${COLOREDSTAR}"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 retval=$? if [ ${retval} -ne 0 ] then ${FAILURE} "failed to load module ${mod}" exit ${retval} fi done for mod in ${MIGRATE_MODULES} do /sbin/modprobe ${mod} >/dev/null 2>&1 done evaluate_retval if [ ! -e /dev/vzctl ] then ${FAILURE} "Missing device node /dev/vzctl" echo echo "Please create the vzctl device node using the following command:" echo " /bin/mknod /dev/vzctl c 126 0" echo exit 1 fi start_net setup_ve0 setup_cron start_ve update_svcstatus $1 splash svc_started "$(basename $0)" 0 ;; stop) check_config || exit stop_ve remove_cron stop_net echo -e ${COLOREDSTAR}"Unloading OpenVZ modules" for mod in ${MIGRATE_MODULES} do /sbin/modprobe -r ${mod} > /dev/null 2>&1 done 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 success for the service to be marked as stopped. print_status success update_svcstatus $1 splash svc_stopped "$(basename $0)" 0 ;; restart) $0 stop sleep 1 $0 start ;; status) status_ve ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 ;; esac