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=()
40 _HAVE_INTERFACE_OPTIONS=
48 : ${VSERVER_NAME:=$(basename "$VSERVER_DIR")}
50 if test -e "$VSERVER_DIR"/noisy -o "$OPTION_VERBOSE"; then
56 function _generateChbindOptions
62 local f=$vdir/interfaces/bcast
63 getFileValue bcast "$f"
65 CHBIND_OPTS=( $SILENT_OPT ${bcast:+--bcast "$bcast"} )
67 for i in "${INTERFACES[@]}"; do
68 CHBIND_OPTS=( "${CHBIND_OPTS[@]}" --ip "$i" )
72 function _generateNiceCommand
77 test -r "$vdir/nice" || return 0;
78 read nice <"$vdir"/nice
80 NICE_CMD=( $_NICE -$nice )
83 function _generateCapabilityOptions
88 test -e "$vdir"/capabilities || return 0
97 CAP_OPTS=( "${CAP_OPTS[@]}" --cap "$cap" )
98 CAPCHROOT_OPTS=( "${CAPCHROOT_OPTS[@]}" --nochroot )
101 CAP_OPTS=( "${CAP_OPTS[@]}" --cap "$cap" );;
103 done <"$vdir"/capabilities
106 function getEnterShell
112 getFileValue ENTER_SHELL "$vdir"/shell
114 test "$ENTER_SHELL" || {
116 for i in "/bin/bash -login" "/bin/sh -l" /bin/csh; do
118 test -x "$vdir/vdir/$1" || continue
125 function _generateInitOptions
128 local cfgdir=$vdir/apps/init
135 INITCMD_START_SYNC=()
139 INITKILL_SEQ=( 15 5 9 )
140 CHCONTEXT_INIT_OPTS=()
143 getFileValue INITSTYLE "$cfgdir"/style
144 getFileValue runlevel_start "$cfgdir"/runlevel
145 getFileValue runlevel_start "$cfgdir"/runlevel.start
146 getFileValue runlevel_stop "$cfgdir"/runlevel.stop
147 getFileArray INITKILL_SEQ "$cfgdir"/killseq
149 case x"$INITSTYLE" in
151 test "$runlevel_start" || runlevel_start=3
152 test "$runlevel_stop" || runlevel_stop=6
154 for i in /etc/init.d/rc /etc/rc.d/rc; do
155 test -x "$vdir/vdir/$i" || continue
156 INITCMD_START=( "$i" "$runlevel_start" )
157 INITCMD_STOP=( "$i" "$runlevel_stop" )
159 INITCMD_PREPARE=( $_FAKE_RUNLEVEL "$runlevel_start" /var/run/utmp )
163 INITCMD_START=( /sbin/init )
164 INITCMD_STOP=( /sbin/init )
165 CHCONTEXT_INIT_OPTS=( --disconnect --flag fakeinit )
166 test -z "$runlevel_start" || INITCMD_START=( "${INITCMD_START[@]}" "$runlevel_start" )
167 test -z "$runlevel_stop" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$runlevel_stop" )
171 INITCMD_START=( /sbin/minit-start )
172 INITCMD_STOP=( /sbin/minit-stop )
173 CHCONTEXT_INIT_OPTS=( --disconnect --flag fakeinit )
174 INITCMD_START_SYNC=( "$_INITSYNC_MINIT_START" "$vdir" )
175 INITCMD_STOP_SYNC=( "$_INITSYNC_MINIT_STOP" "$vdir" )
176 test -z "$runlevel_start" || INITCMD_START=( "${INITCMD_START[@]}" "$runlevel_start" )
177 test -z "$runlevel_stop" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$runlevel_stop" )
181 *) echo "Unknown init-style '$INITSTYLE'; aborting" >&2;
185 getFileArray INITCMD_START "$cfgdir"/cmd.start
186 getFileArray INITCMD_STOP "$cfgdir"/cmd.stop
187 getFileArray INITCMD_START_SYNC "$cfgdir"/cmd.start-sync
188 getFileArray INITCMD_STOP_SYNC "$cfgdir"/cmd.stop-sync
189 getFileArray INITCMD_PREPARE "$cfgdir"/cmd.prepare
192 test -e "$cfgdir"/sync -o "$OPTION_FORCE_SYNC" || {
193 INITCMD_START_SYNC=()
198 function _generateFlagOptions
202 CHCONTEXT_FLAG_OPTS=()
204 test -e "$vdir"/flags || return 0
211 CHCONTEXT_INIT_OPTS=( --disconnect --flag fakeinit )
214 CHCONTEXT_FLAG_OPTS=( "${CHCONTEXT_FLAG_OPTS[@]}"
221 function _generateChcontextOptions
224 local ctx hostname domainname
229 read ctx <"$vdir"/context || :
230 read hostname <"$vdir"/hostname || :
231 read domainname <"$vdir"/domainname || :
234 test -z "$S_CONTEXT" || ctx=$S_CONTEXT
236 _generateCapabilityOptions "$vdir"
237 _generateFlagOptions "$vdir"
239 CHCONTEXT_OPTS=( $SILENT_OPT \
240 "${CHCONTEXT_FLAG_OPTS[@]}" \
243 ${ctx:+--ctx "$ctx"} \
244 ${hostname:+--hostname "$hostname"} \
245 ${domainname:+--domainname "$domainname"} )
248 function _getInterfaceValue
258 read tmp <"$i/$val" && break || :
265 ## Usage: _transformMask2Prefix <result-varname> <prefix> <mask>
266 function _transformMask2Prefix
271 $_MASK2PREFIX "$3" || tmp=$?
278 function _addInterfaceCmd
280 eval INTERFACE_CMDS_${INTERFACE_CMDS_IDX}='( "$@" )'
281 let ++INTERFACE_CMDS_IDX
284 ## Usage: _processSingleInterface <interface-directory>
285 function _processSingleInterface
297 _getInterfaceValue ip '' "$iface"
298 _getInterfaceValue dev '' "$iface" "$iface/.."
299 _getInterfaceValue prefix '' "$iface" "$iface/.."
300 _getInterfaceValue mask '' "$iface" "$iface/.."
301 _getInterfaceValue bcast '' "$iface" "$iface/.."
302 _getInterfaceValue name '' "$iface"
303 _getInterfaceValue scope '' "$iface" "$iface/.."
305 test "$ip" || { echo $"Can not read ip for '$iface'"; return 1; }
307 test ! -e "$iface"/only_ip || unset dev
310 _transformMask2Prefix prefix "$prefix" "$mask"
314 test -d /proc/net/vlan || {
315 echo -e $"VLAN device-name used, but vlan subsystem not enabled.\nTry to execute 'modprobe 8021q' before starting the vservers"
318 test -f /proc/net/vlan || {
319 _addInterfaceCmd VCONFIG "${dev/./ }"
320 _addInterfaceCmd IP_ADDR 127.0.0.1/8 broadcast 127.255.255.255 dev "$dev"
321 _addInterfaceCmd IP_LINK "$dev"
326 INTERFACES=( "${INTERFACES[@]}" "$ip${prefix:+/$prefix}" )
327 _addInterfaceCmd IP_ADDR "$ip${prefix:+/$prefix}" broadcast ${bcast:-+} ${name:+label "$dev:$name"} dev "$dev"
328 #_addInterfaceCmd IP_ROUTE "$ip${prefix:+/$prefix}" dev "$dev"
329 _addInterfaceCmd IP_LINK "$dev"
333 ## Usage: _generateInterfaceOptions <vserver-directory>
334 function _generateInterfaceOptions
338 for iface in "$1/interfaces/"*; do
339 test -d "$iface" || continue
340 test ! -e "$iface"/disabled || continue
342 _processSingleInterface "$iface"
344 _HAVE_INTERFACE_OPTIONS=1
347 function enableInterfaces
352 while test $i -lt $INTERFACE_CMDS_IDX; do
353 eval var='( "${INTERFACE_CMDS_'$i'[@]}" )'
358 VCONFIG) $_VCONFIG add "${var[@]}";;
359 IP_ADDR) $_IP addr add "${var[@]}";;
360 IP_LINK) $_IP link set "${var[@]}" up;;
361 IP_ROUTE) $_IP route add "${var[@]}";;
362 *) echo "Unknown interface-command type '$type'" >&2; false;;
369 function disableInterfaces
371 test "$_HAVE_INTERFACE_OPTIONS" || _generateInterfaceOptions "$1"
373 local i=$INTERFACE_CMDS_IDX
376 while test $i -gt 0; do
379 eval var='( "${INTERFACE_CMDS_'$i'[@]}" )'
384 VCONFIG) $_VCONFIG rem "${var[@]}";;
385 IP_ADDR) $_IP addr del "${var[@]}";;
386 IP_LINK) ;; ## Ignore the link-down command for now
387 IP_ROUTE) $_IP route del "${var[@]}";;
388 *) echo "Unknown interface-command type '$type'" >&2; false;;
393 ## Usage: prepareInit <vserver-directory>
396 pushd "$1/vdir" >/dev/null
399 { find var/run ! -type d -print0; \
400 find var/lock ! -type d -print0; } | xargs -0r $_CHROOT_RM
405 "${INITCMD_PREPARE[@]}"
409 function generateOptions
411 _generateInterfaceOptions "$1"
412 _generateChbindOptions "$1"
413 _generateNiceCommand "$1"
414 _generateInitOptions "$1"
415 _generateChcontextOptions "$1"
418 function mountVserver
424 test -e "$cfgdir"/fstab || return 0
426 findObject -r mtab_src "$vdir"/apps/init/mtab "$CONFDIR"/.defaults/init/mtab /dev/null
428 pushd "$vdir" >/dev/null
429 $_CHROOT_CAT /etc/mtab <"$mtab_src"
432 $_CHBIND "${CHBIND_OPTS[@]}" \
433 $_SECURE_MOUNT -a --chroot "$vdir" --fstab "$cfgdir"/fstab
436 function umountVserver
443 test -e "$cfgdir"/fstab || return 0
445 pushd "$vdir/" >/dev/null
446 tac "$cfgdir"/fstab | while read src dst tmp; do
447 test "$tmp" || continue
452 $_CHBIND "${CHBIND_OPTS[@]}" \
453 $_SECURE_UMOUNT "$dst" || is_ok=
462 local vdir name flavor start i
466 ## Usage: execScriptlets <vserver-cfgdir> <vserver-name> <script-flavor>
467 function execScriptlets
475 for i in "$vdir/scripts/$flavor" "$vdir/scripts/$flavor.d"/*; do
476 test -f "$i" -a -r "$i" || continue
477 test -x "$i" || start=_sourceWrap
478 $start "$i" "$flavor" "$name"