X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Fscripts%2Fvserver.functions;h=6bb9058defb7435a19774c1ebad6536c80dca516;hb=75385100bedf6d457e88bc2bb942a6f0771b0e06;hp=52558b719a161fc01a9acd3caf7e5e5571438f2e;hpb=77b0befbf7341df0e83ab1d3fbaa4a3a0436750d;p=util-vserver.git diff --git a/util-vserver/scripts/vserver.functions b/util-vserver/scripts/vserver.functions index 52558b7..6bb9058 100644 --- a/util-vserver/scripts/vserver.functions +++ b/util-vserver/scripts/vserver.functions @@ -28,6 +28,7 @@ declare -a CHCONTEXT_OPTS=() declare -a CAPCHROOT_OPTS=() declare -a INTERFACES=() +declare -a INITCMD_RESCUE=( /bin/sleep 900 ) declare -a INITCMD_START=() declare -a INITCMD_START_SYNC=() declare -a INITCMD_STOP=() @@ -38,14 +39,20 @@ declare -a ENTER_SHELL=() declare -a OPTS_VCONTEXT_CREATE=() declare -a OPTS_VCONTEXT_MIGRATE=() +declare -a OPTS_VCONTEXT_ENTER=() declare -a OPTS_VATTRIBUTE=( --flag fakeinit ) declare -a OPTS_VSCHED=() +declare -a STOPCMD_PREPARE=() + declare -a VSERVER_EXTRA_CMDS=() +INIT_RESCUE= VSHELPER_SYNC_TIMEOUT=30 USE_VNAMESPACE= INTERFACE_CMDS_IDX=0 +RUNLEVEL_START= +RUNLEVEL_STOP= _HAVE_INTERFACE_OPTIONS= _HAVE_CHBIND_OPTIONS= _NEED_VSHELPER_SYNC= @@ -59,7 +66,7 @@ SILENT_OPT= : ${VSERVER_NAME:=$(basename "$VSERVER_DIR")} -if test -e "$VSERVER_DIR"/noisy -o "$OPTION_VERBOSE"; then +if test -e "$VSERVER_DIR"/noisy -o -n "$OPTION_VERBOSE"; then SILENT_OPT= else SILENT_OPT='--silent' @@ -70,13 +77,16 @@ function _generateChbindOptions local vdir="$1" local i local bcast= + local nid= - test "$_HAVE_INTERFACE_OPTIONS" || _generateInterfaceOptions "$vdir" + test -n "$_HAVE_INTERFACE_OPTIONS" || _generateInterfaceOptions "$vdir" local f=$vdir/interfaces/bcast getFileValue bcast "$f" + + getFileValue nid "$vdir/ncontext" "$vdir/context" - CHBIND_OPTS=( $SILENT_OPT ${bcast:+--bcast "$bcast"} ) + CHBIND_OPTS=( $SILENT_OPT ${nid:+--nid "$nid"} ${bcast:+--bcast "$bcast"} ) for i in "${INTERFACES[@]}"; do CHBIND_OPTS=( "${CHBIND_OPTS[@]}" --ip "$i" ) @@ -96,6 +106,34 @@ function _generateNiceCommand NICE_CMD=( $_NICE -$nice ) } + +function _generatePersonalityOptions +{ + local vdir="$1" + local f="$vdir"/personality + local type flags + + test -s "$f" || return 0 + + { + local delim tmp + + read type + while read tmp; do + case x$tmp in + (x\#*|x) ;; + (*) flags=$flags$delim$tmp + delim=, + ;; + esac + done + } <"$f" + + OPTS_VCONTEXT_ENTER=( "${OPTS_VCONTEXT_ENTER[@]}" + --personality-type "$type" + ${flags:+--personality-flags "$flags"} ) +} + function _generateCCapabilityOptions { local vdir=$1 @@ -105,7 +143,7 @@ function _generateCCapabilityOptions test -e "$f" || return 0 while read cap; do case x"$cap" in - (x|x\#) ;; + (x|x\#*) ;; (*) OPTS_VATTRIBUTE=( "${OPTS_VATTRIBUTE[@]}" --ccap "$cap" );; esac done <"$f" @@ -120,7 +158,7 @@ function _generateBCapabilityOptions test -e "$f" || return 0 while read cap; do case x"$cap" in - (x|x\#) ;; + (x|x\#*) ;; (*) OPTS_VATTRIBUTE=( "${OPTS_VATTRIBUTE[@]}" --bcap "$cap" );; esac done <"$f" @@ -141,7 +179,7 @@ function _generateCapabilityOptions while read cap; do case x"$cap" in - (x|x\#) ;; + (x|x\#*) ;; (!CAP_SYSCHROOT) CAP_OPTS=( "${CAP_OPTS[@]}" --cap "$cap" ) CAPCHROOT_OPTS=( "${CAPCHROOT_OPTS[@]}" --nochroot ) @@ -156,12 +194,13 @@ function _generateCapabilityOptions function getEnterShell { local vdir=$1 + local shell_file ENTER_SHELL=() + + getFileValue ENTER_SHELL "$vdir"/shell "$__CONFDIR"/.defaults/shell - getFileValue ENTER_SHELL "$vdir"/shell - - test "$ENTER_SHELL" || { + test -n "$ENTER_SHELL" || { local i for i in "/bin/bash -login" "/bin/sh -l" /bin/csh; do set -- $i @@ -181,9 +220,9 @@ function sendKillSequence while isCtxRunning "$ctx"; do test -z "$wait" || sleep "$wait" - - $_VKILL -c "$ctx" ${1:+-s "$1"} 2>/dev/null || : - test "$2" || break + + killContext "$ctx" "$1" + test -n "$2" || break wait="$2" shift 2 done @@ -193,8 +232,6 @@ function _generateInitOptions { local vdir=$1 local cfgdir=$vdir/apps/init - local runlevel_start - local runlevel_stop local i f INITCMD_START=() @@ -202,28 +239,36 @@ function _generateInitOptions INITCMD_START_SYNC=() INITCMD_STOP_SYNC=() INITCMD_PREPARE=() + STOPCMD_PREPARE=() INITKILL_SEQ=( 15 5 9 ) CHCONTEXT_INIT_OPTS=() getFileValue INITSTYLE "$cfgdir"/style - getFileValue runlevel_start "$cfgdir"/runlevel - getFileValue runlevel_start "$cfgdir"/runlevel.start - getFileValue runlevel_stop "$cfgdir"/runlevel.stop + getFileValue RUNLEVEL_START "$cfgdir"/runlevel + getFileValue RUNLEVEL_START "$cfgdir"/runlevel.start + getFileValue RUNLEVEL_STOP "$cfgdir"/runlevel.stop getFileArray INITKILL_SEQ "$cfgdir"/killseq || : case x"$INITSTYLE" in + (xrescue) + INITCMD_START=( "${INITCMD_RESCUE[@]}" ) + INITCMD_STOP=( /sbin/killall5 ) + _IS_FAKEINIT=1 + _NEED_VSHELPER_SYNC= + ;; + (xsysv) - test "$runlevel_start" || runlevel_start=3 - test "$runlevel_stop" || runlevel_stop=6 + test -n "$RUNLEVEL_START" || RUNLEVEL_START=3 + test -n "$RUNLEVEL_STOP" || RUNLEVEL_STOP=6 for i in /etc/init.d/rc /etc/rc.d/rc; do test -x "$vdir/vdir/$i" || continue - INITCMD_START=( "$i" "$runlevel_start" ) - INITCMD_STOP=( "$i" "$runlevel_stop" ) + INITCMD_START=( "$i" "$RUNLEVEL_START" ) + INITCMD_STOP=( "$i" "$RUNLEVEL_STOP" ) done - INITCMD_PREPARE=( $_FAKE_RUNLEVEL "$runlevel_start" /var/run/utmp ) + INITCMD_PREPARE=( $_FAKE_RUNLEVEL "$RUNLEVEL_START" /var/run/utmp ) ;; (xplain) @@ -231,8 +276,8 @@ function _generateInitOptions INITCMD_STOP=( /sbin/init ) _IS_FAKEINIT=1 _NEED_VSHELPER_SYNC=1 - test -z "$runlevel_start" || INITCMD_START=( "${INITCMD_START[@]}" "$runlevel_start" ) - test -z "$runlevel_stop" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$runlevel_stop" ) + test -z "$RUNLEVEL_START" || INITCMD_START=( "${INITCMD_START[@]}" "$RUNLEVEL_START" ) + test -z "$RUNLEVEL_STOP" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$RUNLEVEL_STOP" ) ;; (xminit) @@ -241,8 +286,9 @@ function _generateInitOptions _IS_FAKEINIT=1 INITCMD_START_SYNC=( "$_INITSYNC_MINIT_START" "$vdir" ) _NEED_VSHELPER_SYNC=1 - test -z "$runlevel_start" || INITCMD_START=( "${INITCMD_START[@]}" "$runlevel_start" ) - test -z "$runlevel_stop" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$runlevel_stop" ) + test -z "$RUNLEVEL_START" || INITCMD_START=( "${INITCMD_START[@]}" "$RUNLEVEL_START" ) + test -z "$RUNLEVEL_STOP" || INITCMD_STOP=( "${INITCMD_STOP[@]}" "$RUNLEVEL_STOP" ) + ! isNumber "${RUNLEVEL_START:-3}" || INITCMD_PREPARE=( $_FAKE_RUNLEVEL "${RUNLEVEL_START:-3}" /var/run/utmp ) ;; (xgentoo) @@ -251,18 +297,18 @@ function _generateInitOptions ;; (x) ;; - (*) echo "Unknown init-style '$INITSTYLE'; aborting" >&2; - exit 1;; + (*) panic "Unknown init-style '$INITSTYLE'; aborting";; esac - getFileArray INITCMD_START "$cfgdir"/cmd.start || : - getFileArray INITCMD_STOP "$cfgdir"/cmd.stop || : - getFileArray INITCMD_START_SYNC "$cfgdir"/cmd.start-sync || : - getFileArray INITCMD_STOP_SYNC "$cfgdir"/cmd.stop-sync || : - getFileArray INITCMD_PREPARE "$cfgdir"/cmd.prepare || : - + if test x"$INITSTYLE" != xrescue; then + getFileArray INITCMD_START "$cfgdir"/cmd.start || : + getFileArray INITCMD_STOP "$cfgdir"/cmd.stop || : + getFileArray INITCMD_START_SYNC "$cfgdir"/cmd.start-sync || : + getFileArray INITCMD_STOP_SYNC "$cfgdir"/cmd.stop-sync || : + getFileArray INITCMD_PREPARE "$cfgdir"/cmd.prepare || : + fi - test "$OPTION_FORCE_SYNC" -o -e "$cfgdir"/sync || { + test -n "$OPTION_FORCE_SYNC" -o -e "$cfgdir"/sync || { INITCMD_START_SYNC=() INITCMD_STOP_SYNC=() _NEED_VSHELPER_SYNC= @@ -284,7 +330,7 @@ function _generateFlagOptions test ! -e "$vdir"/flags || \ while read flag; do case x"$flag" in - (x|x\#) ;; + (x|x\#*) ;; (xnamespace) ;; (xfakeinit) _IS_FAKEINIT=1 @@ -317,17 +363,6 @@ function _generateChcontextOptions read domainname <"$vdir"/uts/domainname || read domainname <"$vdir"/domainname || : } 2>/dev/null - ## LEGACY ALERT - test \( ! -e "$vdir"/hostname -o -e "$vdir"/uts/nodename \) -a \ - \( ! -e "$vdir"/domainname -o -e "$vdir"/uts/domainname \) || { - echo $"\ -The hostname is now configured in 'uts/nodename' but not in 'hostname' anymore; -the old way is not supported when using 2.6 kernel patches so it is strongly -recommended to move these files. - -The same applies to 'domainname' vs. 'uts/domainname'." >&2 - } - test -z "$S_CONTEXT" || ctx=$S_CONTEXT _generateCapabilityOptions "$vdir" @@ -344,7 +379,7 @@ The same applies to 'domainname' vs. 'uts/domainname'." >&2 OPTS_VCONTEXT_CREATE=( $SILENT_OPT \ ${ctx:+--xid "$ctx"} ) ## put '--secure' at front so that it can be overridden - OPTS_VATTRIBUTE=( --secure "${OPTS_VATTRIBUTE[@]}" ) + OPTS_VATTRIBUTE=( --secure --flag default "${OPTS_VATTRIBUTE[@]}" ) } function _generateScheduleOptions @@ -353,7 +388,7 @@ function _generateScheduleOptions local f="$vdir"/schedule test -e "$f" || return 0 - local fill_rate interval tokens tokens_min tokens_max cpu_mask + local fill_rate interval tokens tokens_min tokens_max prio_bias { { read fill_rate && \ @@ -361,47 +396,47 @@ function _generateScheduleOptions read tokens && \ read tokens_min && \ read tokens_max && \ - read cpu_mask || cpu_mask= + read prio_bias || prio_bias= } <"$f" } 2>/dev/null - test "$cpu_mask" || { + test -n "$prio_bias" || { echo $"Bad content in '$f'; aborting..." >&2 false } OPTS_VSCHED=( --fill-rate "$fill_rate" --interval "$interval" \ --tokens "$tokens" --tokens_min "$tokens_min" \ - --tokens_max "$tokens_max" --cpu_mask "$cpu_mask" ) + --tokens_max "$tokens_max" --priority-bias "$prio_bias" ) } function _getInterfaceValue { - local val=$1 - local dflt=$2 + local _giv_val=$1 + local _giv_dflt=$2 shift 2 - local i - local tmp + local _giv_i + local _giv_tmp - for i; do - read tmp <"$i/$val" && break || : + for _giv_i; do + read _giv_tmp <"$_giv_i/$_giv_val" && break || : done 2>/dev/null - : ${tmp:=$dflt} - eval $val=\$tmp + : ${_giv_tmp:=$_giv_dflt} + eval $_giv_val=\$_giv_tmp } ## Usage: _transformMask2Prefix function _transformMask2Prefix { - local tmp=$2 + local _tm2p_tmp=$2 - test "$tmp" || { - $_MASK2PREFIX "$3" || tmp=$? + test -n "$_tm2p_tmp" || { + $_MASK2PREFIX "$3" || _tm2p_tmp=$? } - eval $1=\$tmp + eval $1=\$_tm2p_tmp return 0 } @@ -422,6 +457,29 @@ function _generateMac eval $1=$(printf "f0:ff:%02x:%02x:%02x:%02x" $[ (~($2>>8)) & 0xff ] $[ ($2 & 0xff) ] $[ ($3>>8) & 0xff ] $[ $3 & 0xff ]) } +function _getVLANInfo +{ + case "$1" in + (vlan????) + panic "\ +creation of VLAN_PLUS_VID devices is not supported; please create them +before starting the vserver and use the 'nodev' flag then" + echo "$1 vlan ${1##vlan} VLAN_PLUS_VID" + ;; + (vlan*) + panic "\ +creation of VLAN_PLUS_VID_NO_PAD devices is not supported; please +create them before starting the vserver and use the 'nodev' flag then" + echo "$1 vlan ${1##vlan} VLAN_PLUS_VID_N0_PAD" + ;; + (*.????) echo "$1 ${1%%.*} ${1##*.} DEV_PLUS_VID";; + (*.*) echo "$1 ${1%%.*} ${1##*.} DEV_PLUS_VID_NO_PAD";; + (*) return 1 + esac + + return 0 +} + ## Usage: _processSingleInterface function _processSingleInterface { @@ -448,44 +506,42 @@ function _processSingleInterface _getInterfaceValue scope '' "$iface" "$iface/.." _getInterfaceValue mac '' "$iface" - test "$ip" || { echo $"Can not read ip for '$iface'" >&2; return 1; } + test -n "$ip" || { echo $"Can not read ip for '$iface'" >&2; return 1; } + test -n "$dev" -o -e "$iface"/nodev || { + echo $"No device specified for '$iface'" >&2 + return 1; + } - test ! -e "$iface"/down || up= - - while test "$dev"; do + test ! -e "$iface"/down || up= + + while true; do _transformMask2Prefix prefix "$prefix" "$mask" INTERFACES=( "${INTERFACES[@]}" "$ip${prefix:+/$prefix}" ) - test ! -e "$iface"/nodev || break - test ! -e "$iface"/only_ip || { - ## LEGACY ALERT - echo $"The 'only_ip' flag for interface '$iface' is deprecated; use 'nodev' instead of" >&2 - break - } - - case "$dev" in - (*.*) - test -d /proc/net/vlan || { - echo -e $"VLAN device-name used, but vlan subsystem not enabled.\nTry to execute 'modprobe 8021q' before starting the vservers" >&2 - return 1 - } - test -f /proc/net/vlan || { - _addInterfaceCmd VCONFIG "${dev/./ }" - _addInterfaceCmd IP_ADDR 127.0.0.1/8 broadcast 127.255.255.255 dev "$dev" - _addInterfaceCmd IP_LINK "$dev" $up - } - ;; - esac + test ! -e "$iface"/nodev || break + ## LEGACY ALERT + test ! -e "$iface"/only_ip || break + + local vlan_info + if vlan_info=$(_getVLANInfo "$dev"); then + test -d /proc/net/vlan || { + echo -e $"VLAN device-name used, but vlan subsystem not enabled.\nTry to execute 'modprobe 8021q' before starting the vservers" >&2 + return 1 + } + test -f /proc/net/vlan -o -e "$iface"/novlandev || { + _addInterfaceCmd VCONFIG $vlan_info + } + fi if ! test -e "$iface"/indirect; then _addInterfaceCmd IP_ADDR "$ip${prefix:+/$prefix}" broadcast ${bcast:-+} ${name:+label "$dev:$name"} dev "$dev" #_addInterfaceCmd IP_ROUTE "$ip${prefix:+/$prefix}" dev "$dev" _addInterfaceCmd IP_LINK "$dev" $up - elif ! test "$ctx"; then + elif ! test -n "$ctx"; then echo $"Using 'dummy' (indirect) for interface '$dev' requires a fixed context number; dynamic ctx are not supported" >&2 return 1 else - test "$mac" || _generateMac mac "$(basename $iface)" "$ctx" || return 1 + test -z "$mac" || _generateMac mac "$(basename $iface)" "$ctx" || return 1 _addInterfaceCmd MODPROBE dummy "$dev" _addInterfaceCmd IP_LINK dev dummy0 address "$mac" _addInterfaceCmd NAMEIF "$dev" "$mac" @@ -519,7 +575,7 @@ function enableInterfaces local i=0 declare -a var - lock "$LOCKDIR"/vserver.interfaces + lock "$__LOCKDIR"/vserver.interfaces while test $i -lt $INTERFACE_CMDS_IDX; do eval var='( "${INTERFACE_CMDS_'$i'[@]}" )' @@ -535,8 +591,9 @@ function enableInterfaces shift 2 $_MODPROBE ${name:+-o "$name"} "$mod" "$@" ;; - NAMEIF) $_NAMEIF "$@";; - VCONFIG) $_VCONFIG add "$@";; + NAMEIF) $_NAMEIF "$@";; + VCONFIG) $_VCONFIG set_name_type "$4" >/dev/null + $_VCONFIG add "$2" "$3" >/dev/null;; IP_ADDR) $_IP addr add "$@";; IP_ADDR_FLUSH) $_IP addr flush "$@";; IP_LINK) $_IP link set "$@";; @@ -552,12 +609,12 @@ function enableInterfaces function disableInterfaces { - test "$_HAVE_INTERFACE_OPTIONS" || _generateInterfaceOptions "$1" + test -n "$_HAVE_INTERFACE_OPTIONS" || _generateInterfaceOptions "$1" local i=$INTERFACE_CMDS_IDX declare -a var - lock "$LOCKDIR"/vserver.interfaces + lock "$__LOCKDIR"/vserver.interfaces while test $i -gt 0; do let --i || : @@ -571,7 +628,7 @@ function disableInterfaces IPTABLES) ;; ## TODO MODPROBE) $_RMMOD "${2:-$1}";; NAMEIF) ;; - VCONFIG) $_VCONFIG rem "$@";; + VCONFIG) $_VCONFIG rem "$2.$3" >/dev/null;; IP_ADDR) $_IP addr del "$@";; IP_ADDR_FLUSH) ;; IP_LINK) ;; ## Ignore the link-down command for now @@ -590,11 +647,11 @@ function prepareInit case "$INITSTYLE" in sysv) { find var/run ! -type d -print0; \ - find var/lock ! -type d -print0; } | xargs -0r $_CHROOT_RM + find var/lock ! -type d -print0; } | xargs -0r $_CHROOT_SH rm ;; plain) - $_CHROOT_RM .autofsck forcefsck - : | $_CHROOT_CAT fastboot + $_CHROOT_SH rm .autofsck forcefsck 2>/dev/null || : + : | $_CHROOT_SH truncate fastboot 2>/dev/null || : ;; minit) ;; @@ -603,16 +660,31 @@ function prepareInit popd >/dev/null } -function generateOptions +## Usage: prepareInit +function prepareStop { - _generateInterfaceOptions "$1" - test "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$1" - _generateNiceCommand "$1" - _generateInitOptions "$1" - _generateChcontextOptions "$1" - _generateScheduleOptions "$1" + pushd "$1/vdir" >/dev/null + case "$INITSTYLE" in + (sysv) + export PREVLEVEL=$RUNLEVEL_START # required by Debian's initscripts + ;; + esac + "${STOPCMD_PREPARE[@]}" + popd >/dev/null +} - if test "$_IS_FAKEINIT"; then + +function generateOptions +{ + _generateInterfaceOptions "$1" + test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$1" + _generateNiceCommand "$1" + _generateInitOptions "$1" + _generateChcontextOptions "$1" + _generateScheduleOptions "$1" + _generatePersonalityOptions "$1" + + if test -n "$_IS_FAKEINIT"; then CHCONTEXT_INIT_OPTS=( --disconnect --flag fakeinit ) OPTS_VCONTEXT_MIGRATE=( "${OPTS_VCONTEXT_MIGRATE[@]}" --initpid --disconnect ) fi @@ -621,10 +693,35 @@ function generateOptions function _mountVserverInternal { local fstab="$1" + local xflag= + test -e "$fstab" || return 0 shift - - "$@" $_SECURE_MOUNT -a --chroot "$vdir" --fstab "$fstab" + + pushd "$vdir" >/dev/null + # check whether / is mounted readonly or whether there is special + # magic regarding the mtab file; when etc/mtab can not be touched, + # add the '-n' flag to mount + test -w etc -o -w etc/mtab || xflag=-n + "$@" $_SECURE_MOUNT -a $xflag --chroot --fstab "$fstab" --rootfs no + popd >/dev/null +} + +function mountRootFS +{ + local cfgdir=$1 + local vdir=$1/vdir + local fstab="$cfgdir"/fstab + local xflag= + + test -e "$fstab" || return 0 + pushd "$vdir" >/dev/null + # check whether / is mounted readonly or whether there is special + # magic regarding the mtab file; when etc/mtab can not be touched, + # add the '-n' flag to mount + test -w etc -o -w etc/mtab || xflag=-n + $_SECURE_MOUNT -a $xflag --chroot --fstab "$fstab" --rootfs only -n + popd >/dev/null } function mountVserver @@ -635,21 +732,23 @@ function mountVserver local mtab_src test -e "$cfgdir"/fstab -o \ - -e "$cfgdir"/fstab.local || return 0 + -e "$cfgdir"/fstab.local -o \ + -e "$cfgdir"/fstab.remote || return 0 - findObject -r mtab_src "$vdir"/apps/init/mtab "$CONFDIR"/.defaults/init/mtab "$PKGLIBDEFAULTDIR"/mtab /dev/null + findObject -r mtab_src "$cfgdir"/apps/init/mtab "$__CONFDIR"/.defaults/init/mtab "$__PKGLIBDEFAULTDIR"/mtab /dev/null pushd "$vdir" >/dev/null - $_CHROOT_CAT /etc/mtab <"$mtab_src" + $_CHROOT_SH truncate /etc/mtab <"$mtab_src" popd >/dev/null - test "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$cfgdir" + test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$cfgdir" test -z "$NAMESPACE_CLEANUP" || isAvoidNamespace "$cfgdir" || \ $_VNAMESPACE --cleanup - _mountVserverInternal "$cfgdir"/fstab $_CHBIND "${CHBIND_OPTS[@]}" + _mountVserverInternal "$cfgdir"/fstab _mountVserverInternal "$cfgdir"/fstab.local + _mountVserverInternal "$cfgdir"/fstab.remote $_CHBIND "${CHBIND_OPTS[@]}" isAvoidNamespace "$cfgdir" || \ $_SECURE_MOUNT --rbind -n "$vdir" "/" @@ -661,10 +760,10 @@ function _umountVserverInternal test -e "$fstab" || return 0 shift - tac "$fstab" | { + $_TAC "$fstab" | { is_ok=1 while read src dst tmp; do - test "$tmp" || continue + test -n "$tmp" || continue case x"$src" in (x\#*) continue;; esac @@ -672,7 +771,7 @@ function _umountVserverInternal "$@" $_EXEC_CD "$dst" $_UMOUNT -lfn . || is_ok= done - test "$is_ok" + test -n "$is_ok" } } @@ -685,29 +784,106 @@ function umountVserver isAvoidNamespace "$cfgdir" || return 0 test -e "$cfgdir"/fstab -o \ -e "$cfgdir"/fstab.local || return 0 - test "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$cfgdir" + test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$cfgdir" pushd "$vdir/" >/dev/null || return 1 _umountVserverInternal "$cfgdir"/fstab.local || is_ok= _umountVserverInternal "$cfgdir"/fstab $_CHBIND "${CHBIND_OPTS[@]}" || is_ok= popd >/dev/null || return 1 - test "$is_ok" + test -n "$is_ok" } -## Usage: waitForSync +## Usage: waitForSync function initSync { - test -z "$_NEED_VSHELPER_SYNC" || vshelper.initSync "$1" "$2" + local _is_meth=sync + test -n "$_NEED_VSHELPER_SYNC" && \ + ! $_VSERVER_INFO - FEATURE vwait || _is_meth=async + + vshelper.initSync "$1" "$3" "$_is_meth" +} + +## Usage: initWait +function initWait +{ + if $_VSERVER_INFO - FEATURE vwait; then + local _is_tmpdir + _is_tmpdir=$($_MKTEMPDIR /tmp/vwaitstat.XXXXXX) + + ( + $_VWAIT --timeout "$VSHELPER_SYNC_TIMEOUT" \ + --status-fd 3 "$2" \ + >>$_is_tmpdir/out 2>$_is_tmpdir/err 3>$_is_tmpdir/fifo + rc=$? + + if test "$rc" -ne 0 -a "$rc" -ne 1; then + $_VPS axf | $_EGREP -e "^[^ \t]+[ \t]+$S_CONTEXT[ \t]+" >&4 + killContext "$S_CONTEXT" 9 + fi + + exit $rc + ) 4>$_is_tmpdir/procs & + + echo "$!" >$_is_tmpdir/pid + eval "$3"=$_is_tmpdir + fi +function _waitForVWait +{ + wait "$3" || : + + declare -a status + declare -r procs=$(cat $4) + + getFileArray status "$2" + set -- ${status[0]} + + case "$1" in + (ERROR) warning $"\ +'vwait' exited with error '$2' which indicates that vserver could not +be stopped properly" + ;; + (FINISHED) ;; + (KILLED) warning $"\ +A timeout occured while waiting for the vserver to finish and it was +killed by sending a SIGKILL signal. Please investigate the reasons +and/or increase the timeout in apps/vshelper/sync-timeout." + ;; + + (TIMEOUT) warning $"\ +A timeout occured while waiting for the vserver to finish and it will +be killed by sending a SIGKILL signal. The following process list +might be useful for finding out the reason of this behavior: + +---------------------------------------------------------------------- +${procs:+$procs +}----------------------------------------------------------------------" + ;; + + (\?\?\?|*) warning $"\ +internal error: 'vwait' exited with an unexpected status '$1'; I will +try to continue but be prepared for unexpected events." + ;; + esac + + return 0 } -## Usage: waitForSync [] +## Usage: waitForSync [] [] function waitForSync { local cfgdir=$1 local fifo=$2 - - if test "$_NEED_VSHELPER_SYNC"; then + local vwait_statdir=$3 + local vwait_pid=$4 + + if test -d "$vwait_statdir"; then + _waitForVWait "$cfgdir" "$vwait_statdir/fifo" "$( <$vwait_statdir/pid )" "$vwait_statdir/procs" + elif test -n "$_NEED_VSHELPER_SYNC"; then $_VSHELPER_SYNC "$fifo" "$VSHELPER_SYNC_TIMEOUT" || \ warning $"\ A timeout or other error occured while waiting for the synchronization @@ -738,18 +914,158 @@ function execScriptlets declare -r flavor=$3 local base i - for base in "$vdir"/scripts "$CONFDIR"/.defaults/scripts; do + for base in "$vdir"/scripts "$__CONFDIR"/.defaults/scripts; do local DONT_SKIP_DEFAULTS= local already_handled= for i in "$base/$flavor" "$base/$flavor.d"/*; do - test -f "$i" -a -r "$i" || continue + isRegularFile "$i" || continue + test -r "$i" || continue + already_handled=1 local start= test -x "$i" || start=_sourceWrap $start "$i" "$flavor" "$name" done - test -z "$already_handled" -o "$DONT_SKIP_DEFAULTS" || break + test -z "$already_handled" -o -n "$DONT_SKIP_DEFAULTS" || break + done +} + + +function sanityCheck +{ + declare -r cfgdir=$1 + + ! test -e "$cfgdir"/fstab.local || + warning $"\ +WARNING: 'fstab' will *not* be executed in the network context of the + vserver anymore. Therefore, 'fstab.local' has the same functionality + and is obsoleted. When you need the old behaviour, put the mounts + into 'fstab.remote'" + + ! test -e "$cfgdir"/hostname -a ! -L "$cfgdir"/hostname || + warning $"\ +WARNING: The hostname is now configured in 'uts/nodename' but not in + 'hostname'." + + ! test -e "$cfgdir"/domainname -a ! -L "$cfgdir"/domainname || + warning $"\ +WARNING: The domainname is now configured in 'uts/domainname' but not + in 'domainname'." >&2 + + + local i + for i in "$cfgdir"/interfaces/*/only_ip; do + if test -e "$i"; then + local iface + iface=${i##$cfgdir/interfaces/} + iface=${iface%%/only_ip} + warning $"\ +WARNING: The 'only_ip' flag for interface '$iface' is deprecated; use + 'nodev' instead of" + fi + done + + find "$cfgdir" -type f -exec "$_CHECK_UNIXFILE" '{}' ';' + + vshelper.doSanityCheck + + $_VSERVER_INFO - VERIFYCAP || + panic $"capabilities are not enabled in kernel-setup" + + $_VSERVER_INFO - VERIFYPROC || + panic $"\ +/proc/uptime can not be accessed. Usually, this is caused by +procfs-security. Please read the FAQ for more details +http://www.linux-vserver.org/index.php?page=Linux-Vserver+FAQ" +} + + +function _setSingleDiskLimit +{ + local vdir=$1 + local dlimit=$2 + local space_used= + local space_total= + local inodes_used= + local inodes_total= + local reserved= + local directory= + local ctx= + + getFileValue ctx "$vdir/context" + getFileValue directory "$dlimit/directory" || return 0 + getFileValue space_total "$dlimit/space_total" || return 0 + getFileValue inodes_total "$dlimit/inodes_total" || return 0 + getFileValue reserved "$dlimit/reserved" || return 0 + + local cachename=`echo "$ctx$directory" | $_SED 's!/!_!g'` + + test -e "$vdir/cache/$cachename" && . "$vdir/cache/$cachename" + # Remove the cache so if the machine goes down unexpectedly, we won't have a stale cache + $_RM -f "$vdir/cache/$cachename" + + if test -z "$inodes_used" -o -z "$space_used"; then + space_used=` $_VDU --xid $ctx --space --script "$directory"` + inodes_used=`$_VDU --xid $ctx --inodes --script "$directory"` + fi + + $_VDLIMIT --xid $ctx \ + --set space_used=$space_used \ + --set space_total=$space_total \ + --set inodes_used=$inodes_used \ + --set inodes_total=$inodes_total \ + --set reserved=$reserved \ + "$directory" +} + + +function setDiskLimits +{ + local vdir=$1 + local dlimit + + # Disk Limits without a static context are useless + test -e "$vdir"/context || return 0 + + for dlimit in "$vdir/dlimits/"*; do + test -d "$dlimit" || continue + test ! -e "$dlimit/disabled" || continue + + _setSingleDiskLimit "$vdir" "$dlimit" + done +} + + +function _saveSingleDiskLimit +{ + local vdir=$1 + local dlimit=$2 + local ctx= + local directory= + + getFileValue ctx "$vdir/context" + getFileValue directory "$dlimit/directory" || return 0 + + local cachename=`echo "$ctx$directory" | $_SED 's!/!_!g'` + + $_VDLIMIT --xid $ctx "$directory" | \ + $_GREP '_used=' > "$vdir/cache/$cachename" +} + + +function saveDiskLimits +{ + local vdir=$1 + local dlimit + + test -e "$vdir"/context || return 0 + + for dlimit in "$vdir/dlimits/"*; do + test -d "$dlimit" || continue + test ! -e "$dlimit/disabled" || continue + + _saveSingleDiskLimit "$vdir" "$dlimit" done }