3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
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 # $VSERVER_DIR ... path to vserver-cfg dir
20 # $VSERVER_NAME ... name of vserver
22 declare -a NICE_CMD=()
23 declare -a CHBIND_OPTS=()
24 declare -a CAP_OPTS=()
25 declare -a CHCONTEXT_INIT_OPTS=()
26 declare -a CHCONTEXT_FLAG_OPTS=()
27 declare -a CHCONTEXT_OPTS=()
28 declare -a CAPCHROOT_OPTS=()
29 declare -a INTERFACES=()
31 declare -a INITCMD_START=()
32 declare -a INITCMD_START_SYNC=()
33 declare -a INITCMD_STOP=()
34 declare -a INITCMD_STOP_SYNC=()
35 declare -a INITCMD_PREPARE=()
36 declare -a INITKILL_SEQ=()
37 declare -a ENTER_SHELL=()
39 declare -a OPTS_VCONTEXT_CREATE=()
40 declare -a OPTS_VCONTEXT_MIGRATE=()
41 declare -a OPTS_VATTRIBUTE=( --flag fakeinit )
42 declare -a OPTS_VSCHED=()
44 declare -a STOPCMD_PREPARE=()
46 declare -a VSERVER_EXTRA_CMDS=()
48 VSHELPER_SYNC_TIMEOUT=30
53 _HAVE_INTERFACE_OPTIONS=
64 : ${VSERVER_NAME:=$(basename "$VSERVER_DIR")}
66 if test -e "$VSERVER_DIR"/noisy -o "$OPTION_VERBOSE"; then
72 function _generateChbindOptions
78 test "$_HAVE_INTERFACE_OPTIONS" || _generateInterfaceOptions "$vdir"
80 local f=$vdir/interfaces/bcast
81 getFileValue bcast "$f"
83 CHBIND_OPTS=( $SILENT_OPT ${bcast:+--bcast "$bcast"} )
85 for i in "${INTERFACES[@]}"; do
86 CHBIND_OPTS=( "${CHBIND_OPTS[@]}" --ip "$i" )
89 _HAVE_CHBIND_OPTIONS=1
92 function _generateNiceCommand
97 test -r "$vdir/nice" || return 0;
98 read nice <"$vdir"/nice
100 NICE_CMD=( $_NICE -$nice )
103 function _generateCCapabilityOptions
107 local f="$vdir"/ccapabilities
109 test -e "$f" || return 0
113 (*) OPTS_VATTRIBUTE=( "${OPTS_VATTRIBUTE[@]}" --ccap "$cap" );;
118 function _generateBCapabilityOptions
122 local f="$vdir"/bcapabilities
124 test -e "$f" || return 0
128 (*) OPTS_VATTRIBUTE=( "${OPTS_VATTRIBUTE[@]}" --bcap "$cap" );;
133 function _generateCapabilityOptions
138 _generateBCapabilityOptions "$vdir"
139 _generateCCapabilityOptions "$vdir"
141 test -e "$vdir"/capabilities || return 0
150 CAP_OPTS=( "${CAP_OPTS[@]}" --cap "$cap" )
151 CAPCHROOT_OPTS=( "${CAPCHROOT_OPTS[@]}" --nochroot )
154 CAP_OPTS=( "${CAP_OPTS[@]}" --cap "$cap" )
157 done <"$vdir"/capabilities
160 function getEnterShell
166 getFileValue ENTER_SHELL "$vdir"/shell
168 test "$ENTER_SHELL" || {
170 for i in "/bin/bash -login" "/bin/sh -l" /bin/csh; do
172 test -x "$vdir/vdir/$1" || continue
179 ## Usage: sendKillSequence <ctx> <signal> [<wait> <signal>]*
180 function sendKillSequence
186 while isCtxRunning "$ctx"; do
187 test -z "$wait" || sleep "$wait"
189 $_VKILL -c "$ctx" ${1:+-s "$1"} 2>/dev/null || :
196 function _generateInitOptions
199 local cfgdir=$vdir/apps/init
204 INITCMD_START_SYNC=()
209 INITKILL_SEQ=( 15 5 9 )
210 CHCONTEXT_INIT_OPTS=()
213 getFileValue INITSTYLE "$cfgdir"/style
214 getFileValue RUNLEVEL_START "$cfgdir"/runlevel
215 getFileValue RUNLEVEL_START "$cfgdir"/runlevel.start
216 getFileValue RUNLEVEL_STOP "$cfgdir"/runlevel.stop
217 getFileArray INITKILL_SEQ "$cfgdir"/killseq || :
219 case x"$INITSTYLE" in
221 test "$RUNLEVEL_START" || RUNLEVEL_START=3
222 test "$RUNLEVEL_STOP" || RUNLEVEL_STOP=6
224 for i in /etc/init.d/rc /etc/rc.d/rc; do
225 test -x "$vdir/vdir/$i" || continue
226 INITCMD_START=( "$i" "$RUNLEVEL_START" )
227 INITCMD_STOP=( "$i" "$RUNLEVEL_STOP" )
229 INITCMD_PREPARE=( $_FAKE_RUNLEVEL "$RUNLEVEL_START" /var/run/utmp )
233 INITCMD_START=( /sbin/init )
234 INITCMD_STOP=( /sbin/init )
236 _NEED_VSHELPER_SYNC=1
237 test -z "$RUNLEVEL_START" || INITCMD_START=( "${INITCMD_START[@]}" "$RUNLEVEL_START" )
238 test -z "$RUNLEVEL_STOP" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$RUNLEVEL_STOP" )
242 INITCMD_START=( /sbin/minit-start )
243 INITCMD_STOP=( /sbin/minit-stop )
245 INITCMD_START_SYNC=( "$_INITSYNC_MINIT_START" "$vdir" )
246 _NEED_VSHELPER_SYNC=1
247 test -z "$RUNLEVEL_START" || INITCMD_START=( "${INITCMD_START[@]}" "$RUNLEVEL_START" )
248 test -z "$RUNLEVEL_STOP" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$RUNLEVEL_STOP" )
252 INITCMD_START=( /sbin/rc default )
253 INITCMD_STOP=( /sbin/rc shutdown )
257 (*) echo "Unknown init-style '$INITSTYLE'; aborting" >&2;
261 getFileArray INITCMD_START "$cfgdir"/cmd.start || :
262 getFileArray INITCMD_STOP "$cfgdir"/cmd.stop || :
263 getFileArray INITCMD_START_SYNC "$cfgdir"/cmd.start-sync || :
264 getFileArray INITCMD_STOP_SYNC "$cfgdir"/cmd.stop-sync || :
265 getFileArray INITCMD_PREPARE "$cfgdir"/cmd.prepare || :
268 test "$OPTION_FORCE_SYNC" -o -e "$cfgdir"/sync || {
269 INITCMD_START_SYNC=()
274 if vshelper.isEnabled; then
275 vshelper.getSyncTimeout "$vdir" VSHELPER_SYNC_TIMEOUT || :
281 function _generateFlagOptions
285 CHCONTEXT_FLAG_OPTS=()
287 test ! -e "$vdir"/flags || \
296 OPTS_VATTRIBUTE=( "${OPTS_VATTRIBUTE[@]}" --flag "$flag" )
297 CHCONTEXT_FLAG_OPTS=( "${CHCONTEXT_FLAG_OPTS[@]}"
303 isAvoidNamespace "$vdir" || {
305 CHCONTEXT_FLAG_OPTS=( "${CHCONTEXT_FLAG_OPTS[@]}" --flag namespace )
309 function _generateChcontextOptions
312 local ctx hostname domainname
317 read ctx <"$vdir"/context || :
319 read hostname <"$vdir"/uts/nodename || read hostname <"$vdir"/hostname || :
320 read domainname <"$vdir"/uts/domainname || read domainname <"$vdir"/domainname || :
324 test \( ! -e "$vdir"/hostname -o -e "$vdir"/uts/nodename \) -a \
325 \( ! -e "$vdir"/domainname -o -e "$vdir"/uts/domainname \) || {
327 The hostname is now configured in 'uts/nodename' but not in 'hostname' anymore;
328 the old way is not supported when using 2.6 kernel patches so it is strongly
329 recommended to move these files.
331 The same applies to 'domainname' vs. 'uts/domainname'." >&2
334 test -z "$S_CONTEXT" || ctx=$S_CONTEXT
336 _generateCapabilityOptions "$vdir"
337 _generateFlagOptions "$vdir"
339 CHCONTEXT_OPTS=( $SILENT_OPT \
340 "${CHCONTEXT_FLAG_OPTS[@]}" \
343 ${ctx:+--ctx "$ctx"} \
344 ${hostname:+--hostname "$hostname"} \
345 ${domainname:+--domainname "$domainname"} )
347 OPTS_VCONTEXT_CREATE=( $SILENT_OPT \
348 ${ctx:+--xid "$ctx"} )
349 ## put '--secure' at front so that it can be overridden
350 OPTS_VATTRIBUTE=( --secure "${OPTS_VATTRIBUTE[@]}" )
353 function _generateScheduleOptions
356 local f="$vdir"/schedule
357 test -e "$f" || return 0
359 local fill_rate interval tokens tokens_min tokens_max cpu_mask
367 read cpu_mask || cpu_mask=
371 test "$cpu_mask" || {
372 echo $"Bad content in '$f'; aborting..." >&2
376 OPTS_VSCHED=( --fill-rate "$fill_rate" --interval "$interval" \
377 --tokens "$tokens" --tokens_min "$tokens_min" \
378 --tokens_max "$tokens_max" --cpu_mask "$cpu_mask" )
381 function _getInterfaceValue
391 read tmp <"$i/$val" && break || :
398 ## Usage: _transformMask2Prefix <result-varname> <prefix> <mask>
399 function _transformMask2Prefix
404 $_MASK2PREFIX "$3" || tmp=$?
411 function _addInterfaceCmd
413 eval INTERFACE_CMDS_${INTERFACE_CMDS_IDX}='( "$@" )'
414 let ++INTERFACE_CMDS_IDX
417 ## Usage: _generateMac <var> <iface> <ctx>
418 function _generateMac
421 echo $"Interface basename '$iface' must be either a number, or the mac must be configured explicitly" >&2
425 eval $1=$(printf "f0:ff:%02x:%02x:%02x:%02x" $[ (~($2>>8)) & 0xff ] $[ ($2 & 0xff) ] $[ ($3>>8) & 0xff ] $[ $3 & 0xff ])
428 ## Usage: _processSingleInterface <interface-directory>
429 function _processSingleInterface
444 _getInterfaceValue ip '' "$iface"
445 _getInterfaceValue extip '' "$iface" "$iface/.."
446 _getInterfaceValue dev '' "$iface" "$iface/.."
447 _getInterfaceValue prefix '' "$iface" "$iface/.."
448 _getInterfaceValue mask '' "$iface" "$iface/.."
449 _getInterfaceValue bcast '' "$iface" "$iface/.."
450 _getInterfaceValue name '' "$iface"
451 _getInterfaceValue scope '' "$iface" "$iface/.."
452 _getInterfaceValue mac '' "$iface"
454 test "$ip" || { echo $"Can not read ip for '$iface'" >&2; return 1; }
456 test ! -e "$iface"/down || up=
458 while test "$dev"; do
459 _transformMask2Prefix prefix "$prefix" "$mask"
460 INTERFACES=( "${INTERFACES[@]}" "$ip${prefix:+/$prefix}" )
462 test ! -e "$iface"/nodev || break
463 test ! -e "$iface"/only_ip || {
465 echo $"The 'only_ip' flag for interface '$iface' is deprecated; use 'nodev' instead of" >&2
471 test -d /proc/net/vlan || {
472 echo -e $"VLAN device-name used, but vlan subsystem not enabled.\nTry to execute 'modprobe 8021q' before starting the vservers" >&2
475 test -f /proc/net/vlan || {
476 _addInterfaceCmd VCONFIG "${dev/./ }"
477 _addInterfaceCmd IP_ADDR 127.0.0.1/8 broadcast 127.255.255.255 dev "$dev"
478 _addInterfaceCmd IP_LINK "$dev" $up
483 if ! test -e "$iface"/indirect; then
484 _addInterfaceCmd IP_ADDR "$ip${prefix:+/$prefix}" broadcast ${bcast:-+} ${name:+label "$dev:$name"} dev "$dev"
485 #_addInterfaceCmd IP_ROUTE "$ip${prefix:+/$prefix}" dev "$dev"
486 _addInterfaceCmd IP_LINK "$dev" $up
487 elif ! test "$ctx"; then
488 echo $"Using 'dummy' (indirect) for interface '$dev' requires a fixed context number; dynamic ctx are not supported" >&2
491 test "$mac" || _generateMac mac "$(basename $iface)" "$ctx" || return 1
492 _addInterfaceCmd MODPROBE dummy "$dev"
493 _addInterfaceCmd IP_LINK dev dummy0 address "$mac"
494 _addInterfaceCmd NAMEIF "$dev" "$mac"
495 _addInterfaceCmd IP_ADDR "$ip${prefix:+/$prefix}" dev "$dev"
496 test -z "$extip" || _addInterfaceCmd IPTABLES "$ip${prefix:+/$prefix}" ${name:+label "$dev:$name"} "$ctx" "$extip"
503 ## Usage: _generateInterfaceOptions <vserver-directory>
504 function _generateInterfaceOptions
509 test ! -e "$1"/context || read ctx <"$1"/context
511 for iface in "$1/interfaces/"*; do
512 test -d "$iface" || continue
513 test ! -e "$iface"/disabled || continue
515 _processSingleInterface "$iface"
517 _HAVE_INTERFACE_OPTIONS=1
520 function enableInterfaces
525 lock "$LOCKDIR"/vserver.interfaces
527 while test $i -lt $INTERFACE_CMDS_IDX; do
528 eval var='( "${INTERFACE_CMDS_'$i'[@]}" )'
539 $_MODPROBE ${name:+-o "$name"} "$mod" "$@"
541 NAMEIF) $_NAMEIF "$@";;
542 VCONFIG) $_VCONFIG add "$@";;
543 IP_ADDR) $_IP addr add "$@";;
544 IP_ADDR_FLUSH) $_IP addr flush "$@";;
545 IP_LINK) $_IP link set "$@";;
546 IP_ROUTE) $_IP route add "$@";;
547 *) echo "Unknown interface-command type '$type'" >&2; false;;
556 function disableInterfaces
558 test "$_HAVE_INTERFACE_OPTIONS" || _generateInterfaceOptions "$1"
560 local i=$INTERFACE_CMDS_IDX
563 lock "$LOCKDIR"/vserver.interfaces
565 while test $i -gt 0; do
568 eval var='( "${INTERFACE_CMDS_'$i'[@]}" )'
575 MODPROBE) $_RMMOD "${2:-$1}";;
577 VCONFIG) $_VCONFIG rem "$@";;
578 IP_ADDR) $_IP addr del "$@";;
580 IP_LINK) ;; ## Ignore the link-down command for now
581 IP_ROUTE) $_IP route del "$@";;
582 *) echo "Unknown interface-command type '$type'" >&2; false;;
589 ## Usage: prepareInit <vserver-directory>
592 pushd "$1/vdir" >/dev/null
595 { find var/run ! -type d -print0; \
596 find var/lock ! -type d -print0; } | xargs -0r $_CHROOT_RM
599 $_CHROOT_RM .autofsck forcefsck 2>/dev/null || :
600 : | $_CHROOT_CAT fastboot 2>/dev/null || :
605 "${INITCMD_PREPARE[@]}"
609 ## Usage: prepareInit <vserver-directory>
612 pushd "$1/vdir" >/dev/null
615 export PREVLEVEL=$RUNLEVEL_START # required by Debian's initscripts
618 "${STOPCMD_PREPARE[@]}"
623 function generateOptions
625 _generateInterfaceOptions "$1"
626 test "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$1"
627 _generateNiceCommand "$1"
628 _generateInitOptions "$1"
629 _generateChcontextOptions "$1"
630 _generateScheduleOptions "$1"
632 if test "$_IS_FAKEINIT"; then
633 CHCONTEXT_INIT_OPTS=( --disconnect --flag fakeinit )
634 OPTS_VCONTEXT_MIGRATE=( "${OPTS_VCONTEXT_MIGRATE[@]}" --initpid --disconnect )
638 function _mountVserverInternal
641 test -e "$fstab" || return 0
644 pushd "$vdir" >/dev/null
645 "$@" $_SECURE_MOUNT -a --chroot --fstab "$fstab"
649 function mountVserver
656 test -e "$cfgdir"/fstab -o \
657 -e "$cfgdir"/fstab.local || return 0
659 findObject -r mtab_src "$vdir"/apps/init/mtab "$CONFDIR"/.defaults/init/mtab "$PKGLIBDEFAULTDIR"/mtab /dev/null
661 pushd "$vdir" >/dev/null
662 $_CHROOT_CAT /etc/mtab <"$mtab_src"
665 test "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$cfgdir"
667 test -z "$NAMESPACE_CLEANUP" || isAvoidNamespace "$cfgdir" || \
668 $_VNAMESPACE --cleanup
670 _mountVserverInternal "$cfgdir"/fstab $_CHBIND "${CHBIND_OPTS[@]}"
671 _mountVserverInternal "$cfgdir"/fstab.local
673 isAvoidNamespace "$cfgdir" || \
674 $_SECURE_MOUNT --rbind -n "$vdir" "/"
677 function _umountVserverInternal
680 test -e "$fstab" || return 0
685 while read src dst tmp; do
686 test "$tmp" || continue
692 "$@" $_EXEC_CD "$dst" $_UMOUNT -lfn . || is_ok=
698 function umountVserver
704 isAvoidNamespace "$cfgdir" || return 0
705 test -e "$cfgdir"/fstab -o \
706 -e "$cfgdir"/fstab.local || return 0
707 test "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$cfgdir"
709 pushd "$vdir/" >/dev/null || return 1
710 _umountVserverInternal "$cfgdir"/fstab.local || is_ok=
711 _umountVserverInternal "$cfgdir"/fstab $_CHBIND "${CHBIND_OPTS[@]}" || is_ok=
712 popd >/dev/null || return 1
717 ## Usage: waitForSync <vserver> <vshelper-fifo-varname>
720 test -z "$_NEED_VSHELPER_SYNC" || vshelper.initSync "$1" "$2"
723 ## Usage: waitForSync <vserver> [<vshelper-fifo>]
729 if test "$_NEED_VSHELPER_SYNC"; then
730 $_VSHELPER_SYNC "$fifo" "$VSHELPER_SYNC_TIMEOUT" || \
732 A timeout or other error occured while waiting for the synchronization
733 signal from vserver '$VSERVER_NAME'.
734 The vserver will be killed nevertheless..."
735 elif test "${#INITCMD_STOP_SYNC[@]}" -ne 0; then
736 "${INITCMD_STOP_SYNC[@]}" || \
738 Stop-synchronization for vserver '$VSERVER_NAME' failed. The vserver
739 will be killed nevertheless..."
742 test -z "$OPTION_FORCE_SYNC" -a ! -e "$cfgdir"/sync ||
748 local vdir name flavor start i already_handled base
752 ## Usage: execScriptlets <vserver-cfgdir> <vserver-name> <script-flavor>
753 function execScriptlets
760 for base in "$vdir"/scripts "$CONFDIR"/.defaults/scripts; do
761 local DONT_SKIP_DEFAULTS=
762 local already_handled=
764 for i in "$base/$flavor" "$base/$flavor.d"/*; do
765 test -f "$i" -a -r "$i" || continue
768 test -x "$i" || start=_sourceWrap
769 $start "$i" "$flavor" "$name"
772 test -z "$already_handled" -o "$DONT_SKIP_DEFAULTS" || break