~ubuntu-branches/ubuntu/lucid/vzctl/lucid

« back to all changes in this revision

Viewing changes to etc/init.d/vz

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2007-04-10 18:08:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070410180816-0uuzj9fnna7gmzxv
Tags: 3.0.16-4
Etch has been released which means that this version can be uploaded
to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
#  Copyright (C) 2000-2006 SWsoft. All rights reserved.
3
 
#
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.
8
 
#
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.
13
 
#
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
17
 
#
18
 
#
19
 
# OpenVZ startup script
20
 
 
21
 
###
22
 
# chkconfig: 2345 96 88
23
 
# description: OpenVZ startup script.
24
 
###
25
 
 
26
 
### BEGIN INIT INFO
27
 
# Provides: vz
28
 
# required-start: $network $remote_fs $local_fs sshd
29
 
# required-stop:
30
 
# Default-Start: 2 3 5
31
 
# Default-Stop:
32
 
# Description: OpenVZ startup script.
33
 
# Short-Description: OpenVZ startup script
34
 
### END INIT INFO
35
 
 
36
 
 
37
 
# This line is needed to cheat /etc/init.d/rc who expects action word
38
 
 
39
 
VZCONF=/etc/vz/vz.conf
40
 
 
41
 
[ -f ${VZCONF}   ] || exit 0
42
 
. ${VZCONF}
43
 
[ "${VIRTUOZZO}" = "no" ] && exit 0
44
 
 
45
 
VZCTL=/usr/sbin/vzctl
46
 
VZQUOTA=/usr/sbin/vzquota
47
 
LOCKFILE=/var/lock/vz_lock
48
 
LICVIEW=/usr/sbin/vzlicview
49
 
PROC_VZVEINFO=/proc/vz/veinfo
50
 
 
51
 
[ -x ${VZCTL} ] || exit 0
52
 
 
53
 
VZDEV=venet0
54
 
PRELOAD_MODULES="af_packet"
55
 
MODULES="vzmon vzdquota vzdev"
56
 
CPT_MODULES="vzcpt vzrst"
57
 
VETH_MODULES="vzethdev"
58
 
MODULES_OTHER="vzcompat ${CPT_MODULES} ${VETH_MODULES}"
59
 
NET_MODULES="vznetdev vznet"
60
 
IPT_MODULES="ip_tables ${IPTABLES}"
61
 
if [ "${VZWDOG}" = "yes" ]; then
62
 
        MODULES="${MODULES} vzwdog"
63
 
fi
64
 
CONFIG_DIR=/etc/vz/conf
65
 
 
66
 
rc_done='..done'
67
 
rc_failed='..failed'
68
 
# Source function library.
69
 
if [ -r /etc/init.d/functions ]; then
70
 
        source /etc/init.d/functions
71
 
        if [ -r /etc/redhat-release ] || [ -r /etc/centos-release ]; then
72
 
                DISTR=redhat
73
 
        fi
74
 
elif [ -r /etc/rc.status ]; then
75
 
        source /etc/rc.status
76
 
        if [ -r /etc/SuSE-release ]; then
77
 
                DISTR=suse
78
 
        fi
79
 
fi
80
 
 
81
 
VEINFO=""
82
 
RETVAL=0
83
 
PARALLEL=4
84
 
cd /
85
 
 
86
 
check_is_root()
87
 
{
88
 
        ID=`id -u`
89
 
        if [ ! $ID -eq 0 ]; then 
90
 
                echo "You need to be root to run this command."
91
 
                exit 1
92
 
        fi
93
 
}
94
 
 
95
 
get_veinfo()
96
 
{
97
 
        if [ -f /proc/vz/veinfo ]; then
98
 
                VEINFO=/proc/vz/veinfo
99
 
        elif [ -f /proc/veinfo ]; then
100
 
                VEINFO=/proc/veinfo
101
 
        else
102
 
                return 1
103
 
        fi
104
 
        return 0
105
 
}
106
 
 
107
 
print_success()
108
 
{
109
 
        if [ "$DISTR" = "redhat" ]; then
110
 
                echo_success
111
 
        else
112
 
                echo -n "$rc_done"
113
 
        fi
114
 
        echo
115
 
}
116
 
 
117
 
print_failure()
118
 
{
119
 
        if [ "$DISTR" = "redhat" ]; then
120
 
                failure $"$1"
121
 
        else
122
 
                echo -n "$rc_failed"
123
 
        fi
124
 
        echo
125
 
}
126
 
 
127
 
__echo()
128
 
{
129
 
        if [ "$DISTR" = "redhat" ]; then
130
 
                echo -n $"$1"
131
 
        else
132
 
                echo -n "$1"
133
 
        fi
134
 
}
135
 
 
136
 
status()
137
 
{
138
 
        local base=${0##*/}
139
 
        if get_veinfo; then
140
 
                echo "OpenVZ is running..."
141
 
                return 0
142
 
        else
143
 
                echo "OpenVZ is stopped."
144
 
                return 3
145
 
        fi
146
 
}
147
 
 
148
 
function setup_net()
149
 
{
150
 
        local mod
151
 
 
152
 
        if ip a l | grep -q "venet0:.*UP" 2>/dev/null; then
153
 
                return 0
154
 
        fi
155
 
        for mod in ${NET_MODULES}; do
156
 
                modprobe ${mod} 2>/dev/null
157
 
        done
158
 
        __echo "Bringing up interface $VZDEV: " 
159
 
        ip link set $VZDEV up 
160
 
        if [ $? -eq 0 ] ; then
161
 
                print_success
162
 
        else
163
 
                print_failure
164
 
        fi
165
 
        ip addr add 0.0.0.0/0 dev $VZDEV
166
 
        __echo "Configuring interface $VZDEV: "
167
 
        sysctl -w net.ipv4.conf.$VZDEV.send_redirects=0
168
 
        if [ $? -eq 0 ] ; then
169
 
                print_success
170
 
        else
171
 
                print_failure
172
 
        fi
173
 
}
174
 
 
175
 
function hn_setup()
176
 
{
177
 
        if test -z "${VE0CPUUNITS}"; then
178
 
                echo "Warning: VE0CPUUNITS is not set in ${VZCONF}; using value of 1000"
179
 
                VE0CPUUNITS=1000
180
 
        fi
181
 
        msg=`${VZCTL} set 0 --cpuunits ${VE0CPUUNITS} 2>&1`
182
 
        if [ $? -ne 0 ]; then
183
 
                print_failure "vzctl set 0 --cpuunits ${VE0CPUUNITS} failed: $msg"
184
 
        fi
185
 
 
186
 
        if ! test -f "${CONFIG_DIR}/0.conf"; then
187
 
                return
188
 
        fi
189
 
        if ! grep -q '^ONBOOT=yes\|^ONBOOT=\"yes\"' ${CONFIG_DIR}/0.conf;
190
 
        then
191
 
                return
192
 
        fi
193
 
        __echo "Configure node UB resources: "
194
 
        msg=`$VZCTL set 0 --reset_ub 2>&1`
195
 
        if [ $? -eq 0 ]; then
196
 
                print_success
197
 
        else
198
 
                print_failure "$msg"
199
 
        fi
200
 
}
201
 
 
202
 
function ve_start()
203
 
{
204
 
        local veid
205
 
        local velist
206
 
        local msg
207
 
        local need_restart
208
 
 
209
 
        need_restart=""
210
 
        cd ${CONFIG_DIR} || return
211
 
        velist=`grep -l '^ONBOOT=yes\|^ONBOOT=\"yes\"' [0-9]*.conf 2>/dev/null | \
212
 
                sed -e 's/.conf//g' | sort -n`
213
 
        cd - >/dev/null
214
 
        echo "0" > /proc/sys/net/ipv4/route/src_check
215
 
        for veid in $velist; do
216
 
                [ "${veid}" = "0" ] && continue 
217
 
                __echo "Starting VPS ${veid}: "
218
 
                if [ "x${VZFASTBOOT}" = "xyes" -a "x${DISK_QUOTA}" = "xyes" ]; 
219
 
                then
220
 
                        $VZQUOTA stat ${veid} >/dev/null 2>&1
221
 
                        if [ $? -eq 6 ]; then
222
 
                                if $VZQUOTA show ${veid} 2>&1 | grep "vzquota : (warning) Quota is running" >/dev/null 2>&1; then
223
 
                                        $VZQUOTA on ${veid} --nocheck >/dev/null 2>&1
224
 
                                        need_restart="${need_restart} ${veid}"
225
 
                                fi
226
 
                        fi
227
 
                fi
228
 
                msg=`$VZCTL start ${veid} 2>&1`
229
 
                if [ $? -eq 0 ]; then
230
 
                        print_success
231
 
                else
232
 
                        print_failure "$msg"
233
 
                fi
234
 
        done
235
 
        for veid in ${need_restart}; do
236
 
                __echo "Stopping VPS ${veid}: "
237
 
                $VZCTL stop ${veid} 2>&1 >/dev/null 2>&1
238
 
                if [ $? -eq 0 ]; then
239
 
                        print_success
240
 
                else
241
 
                        print_failure "$msg"
242
 
                fi
243
 
                __echo "Starting VPS ${veid}: "
244
 
                msg=`$VZCTL start ${veid} 2>&1`
245
 
                if [ $? -eq 0 ]; then
246
 
                        print_success
247
 
                else
248
 
                        print_failure "$msg"
249
 
                fi
250
 
        done
251
 
}
252
 
 
253
 
function ve_stop()
254
 
{
255
 
        local veid
256
 
        local velist
257
 
        local msg
258
 
        local m
259
 
        local mounts
260
 
        local fail
261
 
        local iter
262
 
        local quota
263
 
        local pids
264
 
 
265
 
        if get_veinfo; then
266
 
                for ((i = 0; i <= 2; i++)); do
267
 
                        iter=0;
268
 
                        pids=
269
 
                        velist=`awk  '{print $1;}' < ${VEINFO} | grep -v "^0$"`
270
 
                        for veid in $velist; do
271
 
                                echo "Shutting down VPS $veid"
272
 
                                # Set fairsched parameters to maximum so
273
 
                                # VPS will stop fast
274
 
                                $VZCTL set $veid --cpuunits 2000 --cpulimit 0 >/dev/null 2>&1
275
 
                                $VZCTL --skiplock stop $veid >/dev/null 2>&1 &
276
 
                                pids="$pids $!"
277
 
                                let iter++
278
 
                                sleep 1
279
 
                                if [ ${iter} -gt ${PARALLEL} ]; then
280
 
                                        for pid in ${pids}; do
281
 
                                                wait ${pid}
282
 
                                        done
283
 
                                        pids=
284
 
                                        iter=0
285
 
                                fi
286
 
                        done
287
 
                        for pid in $pids; do
288
 
                                wait $pid
289
 
                        done
290
 
                done
291
 
        fi
292
 
        iter=0
293
 
        fail=1
294
 
        while test $iter -lt 5 -a $fail -ne 0; do
295
 
                fail=0
296
 
                mounts=`awk '{if ($3=="simfs") print $2}' /proc/mounts`
297
 
                for m in $mounts; do
298
 
                        __echo "Unmounting VPS area "
299
 
                        echo -n $m
300
 
                        msg=`umount $m 2>&1`
301
 
                        if [ $? -eq 0 ]; then
302
 
                                print_success
303
 
                        else
304
 
                                print_failure "$msg"
305
 
                                fail=$((fail+1))
306
 
                                fuser -k -m ${m} > /dev/null 2>&1
307
 
                        fi
308
 
                done
309
 
                iter=$((iter+1))
310
 
        done
311
 
        # turn quota off
312
 
        quota=`awk -F: '/^[0-9]+:/{print $1}' /proc/vz/vzquota 2>/dev/null`
313
 
        for m in ${quota}; do
314
 
                __echo "Turn quota off for VPS "
315
 
                echo -n $m
316
 
                msg=`vzquota off ${m} 2>&1`
317
 
                if [ $? -eq 0 ]; then
318
 
                        print_success
319
 
                else
320
 
                        print_failure "$msg"
321
 
                fi
322
 
        done
323
 
}
324
 
 
325
 
function lockfile()
326
 
{
327
 
        local TEMPFILE="${1}.$$"
328
 
        local LOCKFILE="${1}"
329
 
 
330
 
        echo $$ > ${TEMPFILE} 2> /dev/null || {
331
 
                echo "Can't write to ${TEMPFILE}"
332
 
        }
333
 
        ln ${TEMPFILE} ${LOCKFILE} >/dev/null 2>&1 && {
334
 
                rm -f ${TEMPFILE};
335
 
                return 0;
336
 
        }
337
 
        kill -0 `cat $LOCKFILE` >/dev/null 2>&1 && {
338
 
                return 1;
339
 
        }
340
 
        ln ${TEMPFILE} ${LOCKFILE} >/dev/null 2>&1 && {
341
 
                rm -f ${TEMPFILE};
342
 
                return 0;
343
 
        }
344
 
        rm -f ${LOCKFILE}
345
 
        echo $$ > ${LOCKFILE}
346
 
        return 0
347
 
}
348
 
 
349
 
function start()
350
 
{
351
 
        local veid
352
 
        local velist
353
 
        local msg
354
 
 
355
 
        if ! lockfile $LOCKFILE; then
356
 
                __echo "OpenVZ is locked"
357
 
                print_failure
358
 
                return 1
359
 
        fi
360
 
        if [ -f /var/lock/vz ]; then
361
 
                __echo "OpenVZ already running"
362
 
                print_failure
363
 
                return 1
364
 
        fi
365
 
        if [ ! -e /dev/vzctl ]; then
366
 
                /bin/mknod -m 600 /dev/vzctl c 126 0 > /dev/null 2>&1
367
 
                RETVAL=$?
368
 
                if [ $RETVAL -ne 0 ]; then
369
 
                        __echo "creating vzctl device"
370
 
                        print_failure
371
 
                        return $RETVAL
372
 
                fi
373
 
        fi
374
 
        __echo "Starting OpenVZ: "
375
 
        load_modules "${IPT_MODULES}"
376
 
        for MOD in $PRELOAD_MODULES; do
377
 
                /sbin/modprobe -r $MOD >/dev/null 2>&1
378
 
                /sbin/modprobe $MOD >/dev/null 2>&1
379
 
        done
380
 
        for MOD in $MODULES; do
381
 
                /sbin/modprobe $MOD >/dev/null 2>&1
382
 
                RETVAL=$?
383
 
                if [ $RETVAL -ne 0 ]; then
384
 
                        print_failure "Loading module ${MOD}"
385
 
                        return $RETVAL
386
 
                fi
387
 
        done
388
 
        load_modules "${MODULES_OTHER}"
389
 
        print_success "loading OpenVZ modules"
390
 
        setup_net
391
 
        hn_setup
392
 
        ve_start
393
 
        
394
 
        rm -f $LOCKFILE
395
 
        touch /var/lock/vz
396
 
}
397
 
 
398
 
function stop()
399
 
{
400
 
        if ! lockfile $LOCKFILE; then
401
 
                __echo "OpenVZ is locked"
402
 
                print_failure
403
 
                RETVAL=1
404
 
                return 1
405
 
        fi
406
 
 
407
 
        ve_stop
408
 
        __echo "Stopping OpenVZ: "
409
 
        ip link set ${VZDEV} down 2>/dev/null
410
 
        for MOD in $MODULES; do
411
 
                /sbin/modprobe -r $MOD > /dev/null 2>&1
412
 
        done
413
 
        for MOD in $PRELOAD_MODULES ${IPT_MODULES2} ${NET_MODULES}; do
414
 
                /sbin/modprobe -r $MOD > /dev/null 2>&1
415
 
        done
416
 
        print_success
417
 
        rm -f $LOCKFILE
418
 
        rm -f /var/lock/vz
419
 
}
420
 
 
421
 
function load_modules()
422
 
{
423
 
        local modules=$1
424
 
        local mod
425
 
 
426
 
        for mod in ${modules}; do
427
 
                if /sbin/lsmod | grep -qw ${mod}; then
428
 
                        continue
429
 
                fi
430
 
                /sbin/modprobe $mod >/dev/null 2>&1
431
 
        done
432
 
}
433
 
 
434
 
# See how we were called.
435
 
case "$1" in
436
 
  start)
437
 
        start
438
 
        ;;
439
 
  stop)
440
 
        stop
441
 
        ;;
442
 
  force-reload)
443
 
        stop
444
 
        start
445
 
        ;;
446
 
  restart)
447
 
        stop
448
 
        start
449
 
        ;;
450
 
  status)
451
 
        status
452
 
        RETVAL=$?
453
 
        ;;
454
 
  *)
455
 
        echo "Usage: $0 {start|stop|status|restart}"
456
 
        exit 1
457
 
esac
458
 
 
459
 
exit $RETVAL
460