slitaz-vz view base-scripts/vz @ rev 6

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 26 08:27:32 2019 +0100 (2019-02-26)
parents
children
line source
1 #!/bin/sh
2 # Copyright (C) 2000-2008, Parallels, Inc. 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, used for redhat and debian related distributions.
21 ###
22 # description: OpenVZ startup script.
23 ###
25 VZCONF=/etc/vz/vz.conf
27 [ -f ${VZCONF} ] || exit 0
28 . ${VZCONF}
29 [ "${VIRTUOZZO}" = "no" ] && exit 0
31 VZCTL=/usr/sbin/vzctl
32 [ -x ${VZCTL} ] || exit 0
34 VZQUOTA=/usr/sbin/vzquota
35 CONFIG_DIR=/etc/vz/conf
36 LOCKFILE=/var/lock/subsys/vz_lock
37 SUBSYS_VZ=/var/lock/subsys/vz
38 VESTAT=/proc/vz/vestat
39 VZDEV=venet0
40 PRELOAD_MODULES=
41 MODULES=
42 MODULES_OTHER=
43 NET_MODULES=
44 IPT_MODULES=
46 if [ "${MODULES_DISABLED}" != "yes" ]; then
47 PRELOAD_MODULES="af_packet"
48 MODULES="vzmon vzdquota vzdev"
49 CPT_MODULES="vzcpt vzrst"
50 MODULES_OTHER="vzcompat ${CPT_MODULES}"
51 VNET_MODULES="vznetdev vznet"
52 VETH_MODULES="vzethdev"
53 NET_MODULES="${VNET_MODULES} ${VETH_MODULES}"
54 if [ "${VZWDOG}" = "yes" ]; then
55 MODULES="${MODULES} vzwdog"
56 fi
57 IPT_MODULES="ip_tables ${IPTABLES} xt_tcpudp"
58 if [ "${IPV6}" = "yes" ]; then
59 IPT_MODULES="${IPT_MODULES} ${IP6TABLES}"
60 fi
61 VZFS_MODULES="simfs"
62 fi
65 rc_done='..done'
66 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 elif [ -r /etc/debian_version ]; then
80 DISTR=debian
81 LOCKFILE=/var/lock/vz_lock
82 SUBSYS_VZ=/var/lock/vz
83 elif [ -r /etc/slitaz-release ]; then
84 DISTR=slitaz
85 LOCKFILE=/var/lock/vz_lock
86 SUBSYS_VZ=/var/lock/vz
87 fi
89 VEINFO=""
90 RETVAL=0
91 # Number of the containers to stop in parallel.
92 # In case of empty value the number of CTs is calculated as 'num_cpu * 4'
93 PARALLEL=
94 cd /
96 # We used to install OpenVZ cron job when the vzctl package was
97 # installed, irrespective of whether OpenVZ was actually being
98 # run. Although the cron jobs didn't create any problems if someone
99 # wasn't running OpenVZ some users complained about the cron log file
100 # filling up, resource usage, and power consumption since systems
101 # wouldn't really idle. It really only makes sense to run the OpenVZ
102 # cron job if the vz service is turned on and not just merely
103 # having the package installed. This init.d script is an obvious place
104 # to install or remove the cron jobs based on the service
105 # being enabled or not.
106 SRC_CRONSCRIPT_DIR=/etc/vz/cron
107 DST_CRONSCRIPT_DIR=/etc/cron.d
109 check_old_cron_files()
110 {
111 # avoid double OpenVZ cron settings
112 local f
113 for f in vpsreboot vpsnetclean; do
114 [ -f $DST_CRONSCRIPT_DIR/$f ] && rm -f $DST_CRONSCRIPT_DIR/$f
115 done
116 }
118 setup_cron()
119 {
120 check_old_cron_files
121 [ -z "$SRC_CRONSCRIPT_DIR" ] && return
122 [ -d "$SRC_CRONSCRIPT_DIR" ] || return
123 cat $SRC_CRONSCRIPT_DIR/vz* > $DST_CRONSCRIPT_DIR/vz &&
124 chmod 644 $DST_CRONSCRIPT_DIR/vz
125 }
127 remove_cron()
128 {
129 check_old_cron_files
130 [ -z "$SRC_CRONSCRIPT_DIR" ] && return
131 [ -d "$SRC_CRONSCRIPT_DIR" ] || return
132 cat > $DST_CRONSCRIPT_DIR/vz <<EOF
133 # DO NOT EDIT THIS FILE!
134 #
135 # Contents of this file managed by /etc/init.d/vz script
136 # Master copy is in $SRC_CRONSCRIPT_DIR/vz* file(s).
137 # Consult $SRC_CRONSCRIPT_DIR/vz* for documentation.
138 EOF
139 }
141 # Actualize OpenVZ cron entry:
142 # if OpenVZ is running, add it, otherwise remove.
143 update_cron()
144 {
145 if is_running; then
146 __echo "Adding OpenVZ cron entries"
147 setup_cron
148 else
149 __echo "Removing OpenVZ cron entries"
150 remove_cron
151 fi
152 print_result
153 }
155 check_kernel()
156 {
157 if ! test -d /proc/vz ; then
158 print_failure "Running kernel is not OpenVZ kernel."
159 exit 1
160 fi
161 }
163 get_kernel_version()
164 {
165 [ ! -z "$KERNEL_MAJOR" ] && return
167 local ver=$(uname -r)
168 local kernel=$(echo $ver | sed s/[-+].*//)
169 KERNEL_MAJOR=$(echo $kernel | awk -F . '{print $1}')
170 KERNEL_MINOR=$(echo $kernel | awk -F . '{print $2}')
171 KERNEL_PATCHLEVEL=$(echo $kernel | awk -F . '{print $3}')
172 }
174 check_kernel_config()
175 {
176 test -r /proc/config.gz || return 0
178 local conf opt err=0
179 local opt_must="SIM_FS VE VE_CALLS VZ_GENCALLS"
180 get_kernel_version
181 # For kernels >= 2.6.9 VZ_DEV must be set.
182 test "${KERNEL_MINOR}" -ge 6 &&
183 test "${KERNEL_PATCHLEVEL}" -gt 9 &&
184 opt_must="${opt_must} VZ_DEV"
185 # local opt_rec="SCHED_VCPU FAIRSCHED VZ_QUOTA VZ_QUOTA_UGID VE_NETDEV VE_ETHDEV
186 # VE_IPTABLES VZ_CHECKPOINT VZ_WDOG"
188 conf="`zcat /proc/config.gz 2>/dev/null | grep -E -v '^#|^$'`"
190 for opt in $opt_must; do
191 if ! echo "$conf" 2>/dev/null | grep -q "$opt="; then
192 echo "ERROR: Missing kernel config option: CONFIG_$opt"
193 err=1
194 fi
195 done
196 if [ $err != 0 ]; then
197 print_failure "Please recompile your kernel."
198 exit 1
199 fi
200 }
202 get_parallel()
203 {
204 [ -n "${PARALLEL}" ] && return
205 PARALLEL=`awk '
206 BEGIN { num=0; }
207 $1 == "processor" { num++; }
208 END { print num * 4; }' /proc/cpuinfo`
209 }
211 get_veinfo()
212 {
213 if [ -f /proc/vz/veinfo ]; then
214 VEINFO=/proc/vz/veinfo
215 elif [ -f /proc/veinfo ]; then
216 VEINFO=/proc/veinfo
217 elif [ ! -f $VESTAT ]; then
218 return 1
219 fi
220 return 0
221 }
223 print_success()
224 {
225 if [ "$DISTR" = "redhat" ]; then
226 echo_success
227 else
228 echo -n "$rc_done"
229 fi
230 echo
231 }
233 print_failure()
234 {
235 echo -n "$1"
236 if [ "$DISTR" = "redhat" ]; then
237 failure $"$1"
238 else
239 echo -n "$rc_failed"
240 fi
241 echo
242 }
244 # Calls either print_success or print_failure, depending on $?
245 # Optional argument $1 -- an error string passed to print_failure.
246 print_result()
247 {
248 if [ $? -eq 0 ] ; then
249 print_success
250 else
251 print_failure "$1"
252 fi
253 }
255 __echo()
256 {
257 if [ "$DISTR" = "redhat" ]; then
258 echo -n $"$1"
259 else
260 echo -n "$1"
261 fi
262 }
264 is_running()
265 {
266 get_veinfo || return 1
267 [ -f $SUBSYS_VZ ] || return 1
268 }
270 status()
271 {
272 check_kernel
274 if is_running; then
275 echo "OpenVZ is running..."
276 return 0
277 else
278 echo "OpenVZ is stopped."
279 return 3
280 fi
281 }
283 start_net()
284 {
285 local mod
287 # load all kernel modules needed for containers networking
288 for mod in ${NET_MODULES}; do
289 modprobe ${mod} 2>/dev/null
290 done
292 if ip addr list | grep -q "venet0:.*UP" 2>/dev/null; then
293 return 0
294 fi
296 get_veinfo
297 if [ -z "$VEINFO" ]; then
298 return 0
299 fi
300 __echo "Bringing up interface $VZDEV: "
301 ip link set $VZDEV up
302 print_result
303 ip addr add 0.0.0.0/0 dev $VZDEV
304 __echo "Configuring interface $VZDEV: "
305 sysctl -w net.ipv4.conf.$VZDEV.send_redirects=0
306 print_result
307 if [ "${IPV6}" = "yes" ]; then
308 __echo "Configuring ipv6 $VZDEV: "
309 # Fix me: ip addres should be generated
310 ip -6 addr add fe80::1/128 dev $VZDEV
311 print_result
312 fi
313 }
315 stop_net()
316 {
317 local mod
319 if ip addr list | grep -q "venet0:.*UP" 2>/dev/null; then
320 __echo "Bringing down interface $VZDEV: "
321 ip link set $VZDEV down
322 print_result
323 fi
324 for mod in ${NET_MODULES}; do
325 /sbin/modprobe -r ${mod} > /dev/null 2>&1
326 done
327 }
329 setup_ve0()
330 {
331 if test -z "${VE0CPUUNITS}"; then
332 echo "Warning: VE0CPUUNITS is not set in ${VZCONF}; using value of 1000"
333 VE0CPUUNITS=1000
334 fi
335 msg=`${VZCTL} set 0 --cpuunits ${VE0CPUUNITS} 2>&1`
336 if [ $? -ne 0 ]; then
337 print_failure "vzctl set 0 --cpuunits ${VE0CPUUNITS} failed: $msg"
338 fi
340 if ! test -f "${CONFIG_DIR}/0.conf"; then
341 return
342 fi
343 if ! grep -q '^ONBOOT=yes\|^ONBOOT=\"yes\"' ${CONFIG_DIR}/0.conf;
344 then
345 return
346 fi
347 __echo "Configure node UB resources: "
348 msg=`$VZCTL set 0 --reset_ub 2>&1`
349 print_result "$msg"
350 }
352 start_ves()
353 {
354 local veid
355 local velist
356 local msg
357 local need_restart
359 need_restart=""
360 cd ${CONFIG_DIR} || return
361 velist=`grep -l '^ONBOOT=yes\|^ONBOOT=\"yes\"' [0-9]*.conf 2>/dev/null |
362 sed -e 's/.conf//g' | sort -n`
363 cd - >/dev/null
364 sysctl -w net.ipv4.route.src_check=0
365 for veid in $velist; do
366 [ "${veid}" = "0" ] && continue
367 __echo "Starting CT ${veid}: "
368 if [ "x${VZFASTBOOT}" = "xyes" -a "x${DISK_QUOTA}" = "xyes" ];
369 then
370 $VZQUOTA stat ${veid} >/dev/null 2>&1
371 if [ $? -eq 6 ]; then
372 if $VZQUOTA show ${veid} 2>&1 | grep "vzquota : (warning) Quota is running" >/dev/null 2>&1; then
373 $VZQUOTA on ${veid} --nocheck >/dev/null 2>&1
374 need_restart="${need_restart} ${veid}"
375 fi
376 fi
377 fi
378 msg=`$VZCTL start ${veid} 2>&1`
379 print_result "$msg"
380 done
381 for veid in ${need_restart}; do
382 __echo "Stopping CT ${veid}: "
383 $VZCTL stop ${veid} 2>&1 >/dev/null 2>&1
384 print_result "$msg"
385 __echo "Starting CT ${veid}: "
386 msg=`$VZCTL start ${veid} 2>&1`
387 print_result "$msg"
388 done
389 }
391 stop_ves()
392 {
393 local veid
394 local velist
395 local msg
396 local m
397 local mounts
398 local fail
399 local iter
400 local quota
401 local pids
403 if get_veinfo; then
404 get_parallel
405 for i in 0 1 2; do
406 iter=0;
407 pids=
408 velist=`awk '$1 != "VEID" && $1 != "Version:" {print $1}' ${VESTAT}`
409 for veid in $velist; do
410 echo "Shutting down CT $veid"
411 # Set fairsched parameters to maximum so
412 # CT will stop fast
413 $VZCTL set $veid --cpuunits 2000 --cpulimit 0 >/dev/null 2>&1
414 $VZCTL --skiplock stop $veid >/dev/null 2>&1 &
415 pids="$pids $!"
416 iter=$(($iter+1))
417 if [ ${iter} -gt ${PARALLEL} ]; then
418 for pid in ${pids}; do
419 wait ${pid}
420 done
421 pids=
422 iter=0
423 fi
424 done
425 for pid in $pids; do
426 wait $pid
427 done
428 done
429 fi
430 iter=0
431 fail=1
432 while test $iter -lt 5 -a $fail -ne 0; do
433 fail=0
434 mounts=`awk '{if ($3=="simfs") print $2}' /proc/mounts`
435 for m in $mounts; do
436 __echo "Unmounting CT area "
437 echo -n $m
438 msg=`umount $m 2>&1`
439 if [ $? -eq 0 ]; then
440 print_success
441 else
442 print_failure "$msg"
443 fail=$((fail+1))
444 fuser -k -m ${m} > /dev/null 2>&1
445 fi
446 done
447 iter=$(($iter+1))
448 done
449 # turn quota off
450 quota=`awk -F: '/^[0-9]+:/{print $1}' /proc/vz/vzquota 2>/dev/null`
451 for m in ${quota}; do
452 __echo "Turn quota off for CT "
453 echo -n $m
454 msg=`vzquota off ${m} 2>&1`
455 print_result "$msg"
456 done
457 }
459 lockfile()
460 {
461 local TEMPFILE="${1}.$$"
462 local LOCKFILE="${1}"
464 trap -- "rm -f ${LOCKFILE} ${TEMPFILE}" EXIT
466 echo $$ > ${TEMPFILE} 2> /dev/null || {
467 echo "Can't write to ${TEMPFILE}"
468 }
469 ln ${TEMPFILE} ${LOCKFILE} >/dev/null 2>&1 && {
470 rm -f ${TEMPFILE};
471 return 0;
472 }
473 kill -0 `cat $LOCKFILE` >/dev/null 2>&1 && {
474 return 1;
475 }
476 ln ${TEMPFILE} ${LOCKFILE} >/dev/null 2>&1 && {
477 rm -f ${TEMPFILE};
478 return 0;
479 }
480 rm -f ${LOCKFILE}
481 echo $$ > ${LOCKFILE}
482 return 0
483 }
485 start()
486 {
487 local veid
488 local velist
489 local msg
490 local mod
492 check_kernel
493 check_kernel_config
495 if ! lockfile $LOCKFILE; then
496 __echo "OpenVZ is locked"
497 print_failure
498 return 1
499 fi
500 if [ -f ${SUBSYS_VZ} ]; then
501 __echo "OpenVZ already running"
502 print_failure
503 return 1
504 fi
505 __echo "Starting OpenVZ: "
506 load_modules "${IPT_MODULES}"
507 for mod in $PRELOAD_MODULES; do
508 /sbin/modprobe -r $mod >/dev/null 2>&1
509 /sbin/modprobe $mod >/dev/null 2>&1
510 done
511 for mod in $MODULES; do
512 /sbin/modprobe $mod >/dev/null 2>&1
513 RETVAL=$?
514 if [ $RETVAL -ne 0 ]; then
515 print_failure "failed to load module ${mod}"
516 return $RETVAL
517 fi
518 done
519 load_modules "${MODULES_OTHER} ${VZFS_MODULES}"
520 print_success "loading OpenVZ modules"
522 if [ ! -e /dev/vzctl ]; then
523 # On most modern distros udev will create a device for you,
524 # while on the old distros /dev/vzctl comes with vzctl rpm.
525 # So the below mknod call is probably not needed at all.
526 /bin/mknod -m 600 /dev/vzctl c 126 0 > /dev/null 2>&1
527 RETVAL=$?
528 if [ $RETVAL -ne 0 ]; then
529 print_failure "creating /dev/vzctl"
530 return $RETVAL
531 fi
532 fi
534 start_net
535 setup_ve0
536 setup_cron
537 start_ves
539 rm -f $LOCKFILE
540 touch $SUBSYS_VZ
541 }
543 stop()
544 {
545 local mod
547 if ! lockfile $LOCKFILE; then
548 __echo "OpenVZ is locked"
549 print_failure
550 RETVAL=1
551 return 1
552 fi
554 stop_ves
555 remove_cron
556 stop_net
557 __echo "Stopping OpenVZ: "
558 for mod in ${MODULES_OTHER} ${MODULES} ${PRELOAD_MODULES} ${IPT_MODULES} ${VZFS_MODULES}; do
559 /sbin/modprobe -r ${mod} > /dev/null 2>&1
560 done
561 rm -f $LOCKFILE
562 rm -f $SUBSYS_VZ
563 print_success
564 }
566 load_modules()
567 {
568 local modules=$1
569 local mod
571 for mod in ${modules}; do
572 if /sbin/lsmod | grep -qw ${mod}; then
573 continue
574 fi
575 /sbin/modprobe ${mod} >/dev/null 2>&1
576 done
577 }
579 # See how we were called.
580 case "$1" in
581 start)
582 start
583 ;;
584 stop)
585 stop
586 ;;
587 restart|force-reload)
588 stop
589 start
590 ;;
591 status)
592 status
593 RETVAL=$?
594 ;;
595 update-cron)
596 update_cron
597 RETVAL=0
598 ;;
599 *)
600 echo "Usage: $0 {start|stop|status|restart|force-reload|update-cron}"
601 exit 1
602 esac
604 exit $RETVAL