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=()
46 _NEWLINE=${_NEWLINE:0-1}
48 : ${VSERVER_NAME:=$(basename "$VSERVER_DIR")}
50 if test -e "$VSERVER_DIR"/noisy; then
56 ## Called as '_getFileValue <varname> <filename>'
57 function _getFileValue
59 test -r "$2" || return 0
63 ## Called as '_getFileArray <varname> <filename>'
64 function _getFileArray
66 test -r "$2" || return 0
69 eval "$1"='( $(< "$2") )'
72 function _generateChbindOptions
78 local f=$vdir/interfaces/bcast
79 _getFileValue bcast "$f"
81 CHBIND_OPTS=( $SILENT_OPT ${bcast:+--bcast "$bcast"} )
83 for i in "${INTERFACES[@]}"; do
84 CHBIND_OPTS=( "${CHBIND_OPTS[@]}" --ip "$i" )
88 function _generateNiceCommand
93 test -r "$vdir/nice" || return 0;
94 read nice <"$vdir"/nice
96 NICE_CMD=( $_NICE -$nice )
99 function _generateCapabilityOptions
104 test -e "$vdir"/capabilities || return 0
113 CAP_OPTS=( "${CAP_OPTS[@]}" --cap "$cap" )
114 CAPCHROOT_OPTS=( "${CAPCHROOT_OPTS[@]}" --nochroot )
117 CAP_OPTS=( "${CAP_OPTS[@]}" --cap "$cap" );;
119 done <"$vdir"/capabilities
123 function _generateInitOptions
126 local cfgdir=$vdir/apps/init
127 local runlevel_start=3
128 local runlevel_stop=6
133 INITCMD_START_SYNC=()
137 CHCONTEXT_INIT_OPTS=()
139 _getFileValue INITSTYLE "$cfgdir"/style
140 _getFileValue runlevel_start "$cfgdir"/runlevel
141 _getFileValue runlevel_start "$cfgdir"/runlevel.start
142 _getFileValue runlevel_stop "$cfgdir"/runlevel.stop
144 case x"$INITSTYLE" in
146 for i in /etc/init.d/rc /etc/rc.d/rc; do
147 test -x "$vdir/vdir/$i" || continue
148 INITCMD_START=( "$i" "$runlevel_start" )
149 INITCMD_STOP=( "$i" "$runlevel_stop" )
151 INITCMD_PREPARE=( $_FAKE_RUNLEVEL "$runlevel_start" /var/run/utmp )
155 INITCMD_START=( /sbin/init )
156 INITCMD_STOP=( /sbin/init )
157 CHCONTEXT_INIT_OPTS=( --disconnect --flag fakeinit )
158 test -z "$runlevel_start" || INITCMD_START=( "${INITCMD_START[@]}" "$runlevel_start" )
159 test -z "$runlevel_stop" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$runlevel_stop" )
163 INITCMD_START=( /sbin/minit-start )
164 INITCMD_STOP=( /sbin/minit-stop )
165 CHCONTEXT_INIT_OPTS=( --disconnect --flag fakeinit )
166 INITCMD_START_SYNC=( "$_INITSYNC_MINIT_START" "$vdir" )
167 INITCMD_STOP_SYNC=( "$_INITSYNC_MINIT_STOP" "$vdir" )
168 test -z "$runlevel_start" || INITCMD_START=( "${INITCMD_START[@]}" "$runlevel_start" )
169 test -z "$runlevel_stop" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$runlevel_stop" )
173 *) echo "Unknown init-style '$INITSTYLE'; aborting" >&2;
177 _getFileArray INITCMD_START "$cfgdir"/cmd.start
178 _getFileArray INITCMD_STOP "$cfgdir"/cmd.stop
179 _getFileArray INITCMD_START_SYNC "$cfgdir"/cmd.start-sync
180 _getFileArray INITCMD_STOP_SYNC "$cfgdir"/cmd.stop-sync
181 _getFileArray INITCMD_PREPARE "$cfgdir"/cmd.prepare
183 test -e "$cfgdir"/sync -o "$OPTION_FORCE_SYNC" || {
184 INITCMD_START_SYNC=()
189 function _generateFlagOptions
193 CHCONTEXT_FLAG_OPTS=()
195 test -e "$vdir"/flags || return 0
202 CHCONTEXT_FLAG_OPTS=( "${CHCONTEXT_FLAG_OPTS[@]}"
203 --disconnect --flag --fakeinit )
206 CHCONTEXT_FLAG_OPTS=( "${CHCONTEXT_FLAG_OPTS[@]}"
213 function _generateChcontextOptions
216 local ctx hostname domainname
221 read ctx <"$vdir"/context
222 read hostname <"$vdir"/hostname
223 read domainname <"$vdir"/domainname
226 test -z "$S_CONTEXT" || ctx=$S_CONTEXT
228 _generateCapabilityOptions "$vdir"
229 _generateFlagOptions "$vdir"
231 CHCONTEXT_OPTS=( $SILENT_OPT \
232 "${CHCONTEXT_FLAG_OPTS[@]}" \
233 "${CHCONTEXT_INIT_OPTS[@]}" \
236 ${ctx:+--ctx "$ctx"} \
237 ${hostname:+--hostname "$hostname"} \
238 ${domainname:+--domainname "$domainname"} )
241 function _getInterfaceValue
251 read tmp <"$i/$val" && break || :
258 ## Usage: _transformMask2Prefix <result-varname> <prefix> <mask>
259 function _transformMask2Prefix
264 $_MASK2PREFIX "$3" || tmp=$?
271 function _addInterfaceCmd
273 eval INTERFACE_CMDS_${INTERFACE_CMDS_IDX}='( "$@" )'
274 let ++INTERFACE_CMDS_IDX
277 ## Usage: _processSingleInterface <interface-directory>
278 function _processSingleInterface
290 _getInterfaceValue ip '' "$iface"
291 _getInterfaceValue dev '' "$iface" "$iface/.."
292 _getInterfaceValue prefix '' "$iface" "$iface/.."
293 _getInterfaceValue mask '' "$iface" "$iface/.."
294 _getInterfaceValue bcast '' "$iface" "$iface/.."
295 _getInterfaceValue name '' "$iface"
296 _getInterfaceValue scope '' "$iface" "$iface/.."
298 test "$ip" || { echo $"Can not read ip for '$iface'"; return 1; }
300 test ! -e "$iface"/only_ip || unset dev
303 _transformMask2Prefix prefix "$prefix" "$mask"
307 test -d /proc/net/vlan || {
308 echo -e $"VLAN device-name used, but vlan subsystem not enabled.\nTry to execute 'modprobe 8021q' before starting the vservers"
311 test -f /proc/net/vlan || {
312 _addInterfaceCmd VCONFIG "${dev/./ }"
313 _addInterfaceCmd IP_ADDR 127.0.0.1/8 broadcast 127.255.255.255 dev "$dev"
314 _addInterfaceCmd IP_LINK "$dev"
319 INTERFACES=( "${INTERFACES[@]}" "$ip${prefix:+/$prefix}" )
320 _addInterfaceCmd IP_ADDR "$ip${prefix:+/$prefix}" broadcast ${bcast:-+} ${name:+label "$dev:$name"} dev "$dev"
321 #_addInterfaceCmd IP_ROUTE "$ip${prefix:+/$prefix}" dev "$dev"
322 _addInterfaceCmd IP_LINK "$dev"
326 ## Usage: _generateInterfaceOptions <vserver-directory>
327 function _generateInterfaceOptions
331 for iface in "$1/interfaces/"*; do
332 test -d "$iface" || continue
333 test ! -e "$iface"/disabled || continue
335 _processSingleInterface "$iface"
339 function enableInterfaces
344 while test $i -lt $INTERFACE_CMDS_IDX; do
345 eval var='( "${INTERFACE_CMDS_'$i'[@]}" )'
350 VCONFIG) $_VCONFIG add "${var[@]}";;
351 IP_ADDR) $_IP addr add "${var[@]}";;
352 IP_LINK) $_IP link set "${var[@]}" up;;
353 IP_ROUTE) $_IP route add "${var[@]}";;
354 *) echo "Unknown interface-command type '$type'" >&2; false;;
361 function disableInterfaces
363 local i=$INTERFACE_CMDS_IDX
366 while test $i -gt 0; do
369 eval var='( "${INTERFACE_CMDS_'$i'[@]}" )'
374 VCONFIG) $_VCONFIG rem "${var[@]}";;
375 IP_ADDR) $_IP addr del "${var[@]}";;
376 IP_LINK) ;; ## Ignore the link-down command for now
377 IP_ROUTE) $_IP route del "${var[@]}";;
378 *) echo "Unknown interface-command type '$type'" >&2; false;;
383 ## Usage: prepareInit <vserver-directory>
386 pushd "$1/vdir" >/dev/null
389 { find var/run ! -type d -print0; \
390 find var/lock ! -type d -print0; } | xargs -0r $_CHROOT_RM
395 "${INITCMD_PREPARE[@]}"
399 function generateOptions
401 _generateInterfaceOptions "$1"
402 _generateChbindOptions "$1"
403 _generateNiceCommand "$1"
404 _generateInitOptions "$1"
405 _generateChcontextOptions "$1"
408 function mountVserver
413 test -e "$cfgdir"/fstab || return 0
414 $_CHBIND "${CHBIND_OPTS[@]}" \
415 $_SECURE_MOUNT -a --chroot "$vdir" --fstab "$cfgdir"/fstab
418 function umountVserver
425 test -e "$cfgdir"/fstab || return 0
427 pushd "$vdir/" >/dev/null
428 tac "$cfgdir"/fstab | while read src dst tmp; do
429 test "$tmp" || continue
434 $_CHBIND "${CHBIND_OPTS[@]}" \
435 $_SECURE_UMOUNT "$dst" || is_ok=
444 local vdir name flavor start i
448 ## Usage: execScriptlets <vserver-cfgdir> <vserver-name> <script-flavor>
449 function execScriptlets
457 for i in "$vdir/scripts/$flavor" "$vdir/scripts/$flavor.d"/*; do
458 test -f "$i" -a -r "$i" || continue
459 test -x "$i" || start=_sourceWrap
460 $start "$i" "$flavor" "$name"