X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Fscripts%2Ffunctions;h=eb8e863e2619213e8a30d92bff09c3c5b7b2d2cd;hb=eea7e81e254309e3cf64029456c1c8e316bb2062;hp=6c33047b93d6ecf917e639abbde7ee05a1469cc6;hpb=40b31875ebb4841e5c9f3fa4f3297b41352eeeca;p=util-vserver.git diff --git a/util-vserver/scripts/functions b/util-vserver/scripts/functions index 6c33047..eb8e863 100644 --- a/util-vserver/scripts/functions +++ b/util-vserver/scripts/functions @@ -15,6 +15,9 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +_VS_LOGFILE= +_VS_ERRFILE= + _VS_NEWLINE=' ' declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1} @@ -68,6 +71,43 @@ function getPhysicalDir ( set -P && cd "$1" && pwd ) } +function logging +{ + if test "$_VS_LOGFILE"; then + echo "$@" >>"$_VS_LOGFILE" + else + echo "$@" + fi +} + +function warning +{ + if test "$_VS_ERRFILE"; then + echo "$@" >>"$_VS_ERRFILE" + else + echo "$@" >&2 + fi +} + +function panic +{ + if test "$_VS_ERRFILE"; then + echo "$@" >>"$_VS_ERRFILE" + else + echo "$@" >&2 + fi + + exit 1 +} + +function execute +{ + test -z "${DEBUG_EXEC:-}" || echo "$@" + test "${DEBUG_EXEC:-}" = noexec || exec "$@" + exit 0 +} + + function _pkgMountBindDir() { test "$1" != "$2" || return 0 @@ -271,6 +311,7 @@ function getAllVservers } ## Usage: getVserverCtx [ []] +## Returns: 0 iff vserver is running function getVserverStatus { test -r "$1"/run || return 1 @@ -281,12 +322,15 @@ function getVserverStatus test "$3" || return 0 local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$_ctx" | $_WC -l ) + let _tmp=_tmp+0 eval "$3"=\$_tmp - test "$4" -o $_tmp != 0 || return 1 - _tmp=$($_READLINK "$1/run") - test "$_tmp" || return 1 - test -z "$4" || rm -f "$_tmp" + if test "$_tmp" = 0; then + local runfile=$($_READLINK "$1/run") + test -z "$4" || rm -f "$runfile" + return 1 + fi + return 0 } @@ -318,7 +362,7 @@ function getFileValue ## Called as 'getFileArray ' function getFileArray { - test -r "$2" || return 0 + test -r "$2" || return 1 local IFS=$_VS_NEWLINE eval "$1"='( $(< "$2") )' @@ -368,9 +412,272 @@ function callInNamespace isAvoidNamespace "$1" || \ ctx=$( $_VSERVER_INFO "$1" CONTEXT t ) || ctx= + shift if test "$ctx"; then $_VNAMESPACE --enter "$ctx" -- "$@" else "$@" fi } + +## Usage: pkgmgmt.guessStyle +function pkgmgmt.guessStyle() +{ + local vdir=$($_VSERVER_INFO "$1" VDIR) || { + echo $"Can not determine vserver-root" >&2 + return 1 + } + local cfgdir=$($_VSERVER_INFO "$1" APPDIR pkgmgmt) || : + + if test "$cfgdir" -a -e "$cfgdir"/style; then + read style <"$cfgdir"/style + elif test -e "$vdir"/etc/redhat-release -o -e "$vdir"/etc/fedora-release; then + style=redhat + elif test -e "$vdir"/etc/mandrake-release; then + style=mandrake + elif test -e "$vdir"/etc/debian_version; then + style=debian + elif test -e "$vdir"/etc/SuSE-release; then + style=suse + else + echo $"Can not determine packagemanagement style" >&2 + return 1 + fi + + eval $2=\$style + return 0 +} + +## Usage: pkgmgmt.isInternal +## returns true iff is configured for internal packagemanagement +## A typical application is +## | is_external= +## | pkgmgmt.isInternal "$vserver" || is_external=1 +function pkgmgmt.isInternal +{ + local cfgdir=$($_VSERVER_INFO "$1" APPDIR pkgmgmt) || : + + test -z "$cfgdir" -o ! -d "$cfgdir" -o -e "$cfgdir"/internal +} + +## Usage: pkgmgmt.isAptAvailable [] +function pkgmgmt.isAptAvailable +{ + local cfgdir="$1" + local vdir="$2" + local is_internal="$3" + + local have_apt i + if test "$is_internal"; then + have_apt=1 + test -d "$cfgdir"/base/apt -o -d "$cfgdir"/aptetc || have_apt= + else + have_apt= + for i in /bin /usr/bin /usr/local/bin; do + test ! -x "$vdir$i"/apt-get || { have_apt=1; break; } + done + fi + + test "$have_apt" && return 0 || return 1 +} + +function vshelper.doSanityCheck +{ + local vshelper this_xid i + declare -a warnings=() + local solution_disable= + local solution_sysctl= + + vshelper.isEnabled && vshelper.isEnabled warning || return 0 + + this_xid=$($_VSERVER_INFO - XID) || + panic $"Failed to determine current context; aborting..." + + ## Do nothing in other xid's; the helper will be executed in xid 0 only + test "$this_xid" = 0 || return 0 + + local proc_file=/proc/sys/kernel/vshelper + + if ! test -r "$proc_file"; then + vshelper= + warnings=( "${warnings[@]}" + $"File '$proc_file' does not exists but is required for vshelper setup" ) + solution_disable=1 + else + vshelper=$(cat "$proc_file") + + cmp -s "$vshelper" "$_VSHELPER" || { + warnings=( "${warnings[@]}" + $"The configured vshelper '$vshelper' does not match the 'vshelper' + script of the util-vserver package" + ) + solution_disable=1 + solution_sysctl=1 + } + fi + + test -d "$VSHELPERSTATEDIR" || { + warnings=( "${warnings[@]}" + $"\ +The vshelper state-directory '$VSHELPERSTATEDIR' does not exist; + since it is created by 'make install', this indicates a serious + problem with your util-vserver installation" ) + solution_disable=1 + } + + test "${#warnings[@]}" -eq 0 || { + warning $"\ +The following problem(s) were encountered while verifying vshelper +functionality:" + + for i in "${warnings[@]}"; do + warning "* $i" + done + + warning $"\ + +To fix this, you can:" + + test -z "$solution_disable" || warning $"\ +* disable vshelper entirely by executing + | touch \"$CONFDIR/.defaults/apps/vshelper/disabled\" +* disable only this message by executing + | touch \"$CONFDIR/.defaults/apps/vshelper/warning-disabled\"" + + test -x "$solution_sysctl" || warning $"\ +* configure the util-vserver vshelper script, e.g. by adding + | kernel.vshelper = \"$_VSHELPER\" + to /etc/sysctl.conf and rebooting the machine, or by executing + | echo \"$_VSHELPER\" >$proc_file" + + warning "" + + return 1 + } + + return 0 +} + +## Usage: vshelper.isEnabled [