2
# Copyright (C) 2000-2009, Parallels, Inc. All rights reserved.
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# OpenVZ startup script, used for redhat and debian related distributions.
22
# chkconfig: 2345 96 20
23
# description: OpenVZ startup script.
28
# Required-start: $network $remote_fs $syslog
29
# Required-stop: $network $remote_fs $syslog
30
# Should-Start: sshd vzeventd
31
# Should-Stop: sshd vzeventd
32
# Default-Start: 2 3 4 5
34
# Short-Description: OpenVZ startup script
35
# Description: OpenVZ startup script.
39
# This line is needed to cheat /etc/init.d/rc who expects action word
41
. /usr/lib/vzctl/vzctl/scripts/initd-functions
43
VZQUOTA=/usr/sbin/vzquota
44
CONFIG_DIR=/etc/vz/conf
45
VZREBOOTDIR=/var/lib/vzctl/vzreboot
46
LOCKFILE=$VARLOCK/vz_lock
48
VESTAT=/proc/vz/vestat
56
if [ "${MODULES_DISABLED}" != "yes" ]; then
57
PRELOAD_MODULES="af_packet"
58
MODULES="vzmon vzdquota vzdev"
59
CPT_MODULES="vzcpt vzrst"
60
MODULES_OTHER="vzcompat vziolimit ${CPT_MODULES}"
61
VNET_MODULES="vznetdev vznet"
62
VETH_MODULES="vzethdev"
63
NET_MODULES="${VNET_MODULES} ${VETH_MODULES}"
64
if [ "${VZWDOG}" = "yes" ]; then
65
MODULES="${MODULES} vzwdog"
67
if [ -z "$IPTABLES_MODULES" ]; then
68
if [ -n "$IPTABLES" ]; then
69
echo "WARNING: IPTABLES in vz.conf is obsoleted, " 1>&2
70
echo "please use IPTABLES_MODULES instead" 1>&2
71
IPTABLES_MODULES="$IPTABLES"
74
IPT_MODULES="ip_tables ${IPTABLES_MODULES} xt_tcpudp"
75
if [ "${IPV6}" = "yes" ]; then
76
IPT_MODULES="${IPT_MODULES} ${IP6TABLES}"
79
PLOOP_MODULES="ploop pfmt_ploop1 pfmt_raw pio_direct pio_nfs"
89
[ ! -z "$KERNEL_MAJOR" ] && return
92
local kernel=$(echo $ver | sed s/[-+].*//)
93
KERNEL_MAJOR=$(echo $kernel | awk -F . '{print $1}')
94
KERNEL_MINOR=$(echo $kernel | awk -F . '{print $2}')
95
KERNEL_PATCHLEVEL=$(echo $kernel | awk -F . '{print $3}')
100
test -r /proc/config.gz || return 0
103
local opt_must="SIM_FS VE VE_CALLS VZ_GENCALLS"
105
# For kernels >= 2.6.9 VZ_DEV must be set.
106
test "${KERNEL_MINOR}" -ge 6 &&
107
test "${KERNEL_PATCHLEVEL}" -gt 9 &&
108
opt_must="${opt_must} VZ_DEV"
109
# local opt_rec="SCHED_VCPU FAIRSCHED VZ_QUOTA VZ_QUOTA_UGID VE_NETDEV VE_ETHDEV
110
# VE_IPTABLES VZ_CHECKPOINT VZ_WDOG"
112
conf="`zcat /proc/config.gz 2>/dev/null | grep -E -v '^#|^$'`"
114
for opt in $opt_must; do
115
if ! echo "$conf" 2>/dev/null | grep -q "$opt="; then
116
echo "ERROR: Missing kernel config option: CONFIG_$opt"
120
if [ $err != 0 ]; then
121
print_failure "Please recompile your kernel."
129
for g in beancounter container fairsched ; do
130
if [ -d /proc/vz/$g ]; then
131
mount -t cgroup $g /proc/vz/$g -o name=$g 2>/dev/null
139
for g in beancounter container fairsched ; do
140
umount /proc/vz/$g 2>/dev/null
146
[ -n "${VE_PARALLEL}" -a "${VE_PARALLEL}" != "0" ] && return
149
$1 == "processor" { num++; }
150
END { print num * 4; }' /proc/cpuinfo`
155
if [ -f /proc/vz/veinfo ]; then
156
VEINFO=/proc/vz/veinfo
157
elif [ ! -f $VESTAT ]; then
165
get_veinfo || return 1
166
[ -f $SUBSYS_VZ ] || return 1
169
# Add /vz to PRUNEPATHS in /etc/updatedb.conf so updatedb won't:
170
# 1 clog its database with lots of file entries
171
# 2 interfere with vzctl umount
173
local file="/etc/updatedb.conf"
174
local var="PRUNEPATHS"
177
test -r $file || return
178
add=$(printf "$VE_ROOT\n$VE_PRIVATE" | \
179
sed -e 'N;s/^\(.*\).*\n\1.*$/\1/' -e 's@/[^/]*$@@')
180
test "$add" = "/" && return
181
test -d "$add" || return
182
grep -qw "^${var}" $file || return
183
grep -qE "^${var}.* ${add}[\" ]" $file && return
184
__echo "Adding ${add} to ${var} in ${file}:"
186
"s@\(^${var}[^\"]*\"[^\"]*\)\(\".*\)\$@\1 ${add}\2@" $file
195
echo "OpenVZ is running..."
198
echo "OpenVZ is stopped."
207
# load all kernel modules needed for containers networking
208
for mod in ${NET_MODULES}; do
209
modprobe ${mod} 2>/dev/null
212
if ip addr list | grep -q "venet0:.*UP" 2>/dev/null; then
217
if [ -z "$VEINFO" ]; then
220
__echo "Bringing up interface $VZDEV: "
221
ip link set $VZDEV up
223
ip addr add 0.0.0.0/0 dev $VZDEV
224
if [ "${IPV6}" = "yes" ]; then
225
ip -6 addr add fe80::1/128 dev $VZDEV
227
sysctl -q -w net.ipv4.conf.$VZDEV.send_redirects=0
228
if [ "$(sysctl -n -e net.ipv4.ip_forward)" != "1" ]; then
229
print_warning "IP forwarding is not enabled"
237
if ip addr list | grep -q "venet0:.*UP" 2>/dev/null; then
238
__echo "Bringing down interface $VZDEV: "
239
ip link set $VZDEV down
242
for mod in ${NET_MODULES}; do
243
/sbin/modprobe -r ${mod} > /dev/null 2>&1
251
if test -z "${VE0CPUUNITS}"; then
252
echo "Warning: VE0CPUUNITS is not set in ${VZCONF}; using value of 1000"
255
msg=`${VZCTL} set 0 --cpuunits ${VE0CPUUNITS} 2>&1`
256
if [ $? -ne 0 ]; then
257
print_failure "vzctl set 0 --cpuunits ${VE0CPUUNITS} failed: $msg"
260
if ! test -f "${CONFIG_DIR}/0.conf"; then
263
if ! grep -q '^ONBOOT=yes\|^ONBOOT=\"yes\"' ${CONFIG_DIR}/0.conf;
267
__echo "Configure node UB resources: "
268
msg=`$VZCTL set 0 --reset_ub 2>&1`
283
# CTs that were running before a reboot
284
velist=$(ls $VZREBOOTDIR)
286
# ... and not have ONBOOT=no
287
test -n "$velist" && velist=$(vzlist -aH -octid,onboot $velist |
288
awk '$2 != "no" {print $1}')
289
# ... plus ones with ONBOOT=yes
290
velist=$(echo "$velist"; vzlist -aH -octid,onboot |
291
awk '$2 == "yes" {print $1}')
292
# Then sort by bootorder
293
test -n "$velist" && velist=$(vzlist -aH -octid -s-bootorder $velist)
294
sysctl -q -w net.ipv4.route.src_check=0
296
for veid in $velist; do
297
[ "${veid}" = "0" ] && continue
298
if [ "x${VZFASTBOOT}" = "xyes" -a "x${DISK_QUOTA}" = "xyes" ];
300
$VZQUOTA stat ${veid} >/dev/null 2>&1
301
if [ $? -eq 6 ]; then
302
if $VZQUOTA show ${veid} 2>&1 | grep "vzquota : (warning) Quota is running" >/dev/null 2>&1; then
303
$VZQUOTA on ${veid} --nocheck >/dev/null 2>&1
304
need_restart="${need_restart} ${veid}"
308
if [ -z "${header}" ]; then
309
__echo "Starting CT:"
314
rm -f $VZREBOOTDIR/$veid
315
$VZCTL start ${veid} >/dev/null 2>&1 &
317
eval VE_${pid}=${veid}
320
if [ ${iter} -ge ${VE_PARALLEL} ]; then
322
for pid in ${pids}; do
323
veid=`eval echo \\$VE_${pid}`
325
__echo "Container ${veid} start: "
335
[ -n "${pids}" ] && echo
336
for pid in ${pids}; do
337
veid=`eval echo \\$VE_${pid}`
339
__echo "Container ${veid} start: "
344
for veid in ${need_restart}; do
345
__echo "Stopping CT ${veid}: "
346
$VZCTL stop ${veid} 2>&1 >/dev/null 2>&1
348
__echo "Starting CT ${veid}: "
349
msg=`$VZCTL start ${veid} 2>&1`
356
local veid velist i iter pid pids msg stage stages ret
358
if ! get_veinfo; then
364
velist=$(vzlist -1 2>/dev/null)
365
for veid in $velist; do
366
# Equalize cpuunits for all CTs
367
$VZCTL set $veid --cpuunits 2000 >/dev/null 2>&1
368
# Save to vzreboot list
369
touch $VZREBOOTDIR/$veid
374
if [ -z "${VE_STOP_MODE}" -o "$VE_STOP_MODE" = "suspend" ]; then
375
stages="suspend stop"
377
for stage in $stages; do
383
msg='Shutting down CT'
390
velist=$(vzlist -H -o ctid -sbootorder 2>/dev/null)
391
for veid in $velist; do
392
if [ "$stage" = "stop" ]; then
393
# Unset limits for CT to stop fast
394
$VZCTL set $veid --cpulimit 0 --iolimit 0 --iopslimit 0 >/dev/null 2>&1
396
if [ -z "${header}" ]; then
401
$VZCTL --skiplock $stage $veid >/dev/null 2>&1 &
403
eval VE_${pid}=${veid}
406
if [ ${iter} -ge ${VE_PARALLEL} ]; then
408
for pid in ${pids}; do
409
veid=`eval echo \\$VE_${pid}`
411
__echo "Container ${veid} $stage: "
420
[ -n "${pids}" ] && echo
422
veid=`eval echo \\$VE_${pid}`
424
__echo "Container ${veid} $stage: "
436
local m mounts msg quota
438
# umount all simfs mounts
439
while test $iter -lt 5 -a $fail -ne 0; do
441
mounts=`awk '{if ($3=="simfs") print $2}' /proc/mounts`
443
__echo "Unmounting CT area "
446
if [ $? -eq 0 ]; then
451
fuser -k -m ${m} > /dev/null 2>&1
457
quota=`awk -F: '/^[0-9]+:/{print $1}' /proc/vz/vzquota 2>/dev/null`
458
for m in ${quota}; do
459
__echo "Turn quota off for CT "
461
msg=`vzquota off ${m} 2>&1`
468
local TEMPFILE="${1}.$$"
469
local LOCKFILE="${1}"
471
trap -- "rm -f ${LOCKFILE} ${TEMPFILE}" EXIT
473
echo $$ > ${TEMPFILE} 2> /dev/null || {
474
echo "Can't write to ${TEMPFILE}"
476
ln ${TEMPFILE} ${LOCKFILE} >/dev/null 2>&1 && {
480
kill -0 `cat $LOCKFILE` >/dev/null 2>&1 && {
484
ln ${TEMPFILE} ${LOCKFILE} >/dev/null 2>&1 && {
489
echo $$ > ${LOCKFILE}
503
if ! lockfile $LOCKFILE; then
504
__echo "OpenVZ is locked"
508
if [ -f ${SUBSYS_VZ} ]; then
509
__echo "OpenVZ already running"
514
# One-time sysctl.conf setup
515
if test -z "$SKIP_SYSCTL_SETUP"; then
516
/usr/lib/vzctl/vzctl/scripts/vz-postinstall sysctl
517
echo "SKIP_SYSCTL_SETUP=yes" >> ${VZCONF}
520
__echo "Starting OpenVZ: "
521
load_modules "${IPT_MODULES}"
522
for mod in $PRELOAD_MODULES; do
523
/sbin/modprobe -r $mod >/dev/null 2>&1
524
/sbin/modprobe $mod >/dev/null 2>&1
526
for mod in $MODULES; do
527
/sbin/modprobe $mod >/dev/null 2>&1
529
if [ $RETVAL -ne 0 ]; then
530
print_failure "failed to load module ${mod}"
534
load_modules "${MODULES_OTHER} ${VZFS_MODULES} ${PLOOP_MODULES}"
535
print_success "loading OpenVZ modules"
537
if [ ! -e /dev/vzctl ]; then
538
# On most modern distros udev will create a device for you,
539
# while on the old distros /dev/vzctl comes with vzctl rpm.
540
# So the below mknod call is probably not needed at all.
541
/bin/mknod -m 600 /dev/vzctl c 126 0 > /dev/null 2>&1
543
if [ $RETVAL -ne 0 ]; then
544
print_failure "creating /dev/vzctl"
549
if [ -f /proc/vz/oom_score_adj ]; then
550
__echo "Applying OOM adjustments: "
551
cat /etc/vz/oom-groups.conf > /proc/vz/oom_score_adj
560
# Try to run vzstats to report new kernel
561
vzstats >/dev/null 2>&1
571
# Avoid stop action inside a CT, check we are in CT0
572
if test -r /proc/user_beancounters; then
573
if ! egrep -q '^[[:space:]]*0:[[:space:]]' \
574
/proc/user_beancounters; then
575
print_failure "Looks like we are inside a container!"
581
if ! lockfile $LOCKFILE; then
582
__echo "OpenVZ is locked"
592
__echo "Stopping OpenVZ: "
593
for mod in ${MODULES_OTHER} ${MODULES} ${PRELOAD_MODULES} \
594
${IPT_MODULES} ${VZFS_MODULES} ${PLOOP_MODULES}; do
595
/sbin/modprobe -r ${mod} > /dev/null 2>&1
607
for mod in ${modules}; do
608
if /sbin/lsmod | grep -qw ${mod}; then
611
/sbin/modprobe ${mod} >/dev/null 2>&1
615
# See how we were called.
623
restart|force-reload)
632
echo "Usage: $0 {start|stop|status|restart|force-reload}"