mentioned the '-m yum' buildmethod
[util-vserver.git] / util-vserver / scripts / functions
index eb8e863..c8428b4 100644 (file)
@@ -24,23 +24,23 @@ declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1}
 
 function findObject
 {
-    local mod=$1
-    local var=$2
-    local file=
-    local i=X
+    local _fo_mod=$1
+    local _fo_var=$2
+    local _fo_file=
+    local _fo_i=X
     shift 2
     
-    for i; do
-       test "$i"        || continue
-       test ! $mod "$i" || { file=$i; break; }
+    for _fo_i; do
+       test "$_fo_i"            || continue
+       test ! $_fo_mod "$_fo_i" || { _fo_file=$_fo_i; break; }
     done
 
-    test -z "$i" -o "$file" || {
-       echo "Can not find file for '$var'; aborting"
+    test -z "$_fo_i" -o "$_fo_file" || {
+       echo "Can not find file for '$_fo_var'; aborting"
        exit 1
     } >&2
 
-    eval "$var=\"$file\""
+    eval "$_fo_var=\"$_fo_file\""
 }
 
 function findFile
@@ -63,7 +63,7 @@ function findAndCopy
     findFile tmp "$@"
 
     test "$tmp" -a -s "$tmp" || return 0
-    cp -af "$tmp" "$dst"
+    $_CP -af "$tmp" "$dst"
 }
 
 function getPhysicalDir
@@ -71,6 +71,7 @@ function getPhysicalDir
     ( set -P && cd "$1" && pwd )
 }
 
+## Usage: logging <message>
 function logging
 {
     if test "$_VS_LOGFILE"; then
@@ -80,6 +81,7 @@ function logging
     fi
 }
 
+## Usage: warning <message>
 function warning
 {
     if test "$_VS_ERRFILE"; then
@@ -89,6 +91,7 @@ function warning
     fi
 }
 
+## Usage: panic <message>
 function panic
 {
     if test "$_VS_ERRFILE"; then
@@ -100,6 +103,7 @@ function panic
     exit 1
 }
 
+## Usage: execute <message>
 function execute
 {
     test -z "${DEBUG_EXEC:-}"       || echo "$@"
@@ -107,12 +111,85 @@ function execute
     exit 0
 }
 
+## Usage: isNumber <arg>
+function isNumber
+{
+    local tmp
+    let tmp=$1+0 2>/dev/null || test -z "${1//0/}" -a "$1" || return 1
+    return 0
+}
+
+## Usage: colorize <style> <command>
+function colorize
+{
+    local      style=$1
+    shift
+    
+    if ! $_TTY -s; then
+       "$@"
+    else
+       local   cfile
+       findFile cfile "$CONFDIR"/.defaults/styles/"$style" ''
+       if test "$cfile"; then
+         $_CAT "$cfile"
+       else
+         case "$style" in
+           (bold)      echo -ne "\e[1m";;
+           (emph)      echo -ne "\e[34m";;
+           (info)      echo -ne "\e[0;34m";;
+           (warn*)     echo -ne "\e[1;31m";;
+           (error)     echo -ne "\e[1;33;41m";;
+           (*)         ;;
+         esac
+       fi
+           
+       "$@"
+       echo -ne "\e[m"
+    fi
+}
+
+## Usage: xtermTitle <title>
+function xtermTitle
+{
+    $_TTY -s || return 0
+    echo -ne "\e]0;$@\007"
+}
+
+_VS_LOCKS=''
+## Usage: lock <lockfile> [<timeout>]
+function lock
+{
+    local tmp=$($_MKTEMP /tmp/vserver-lock.XXXXXX)
+    $_RM -f $tmp
+    $_MKFIFO -m600 $tmp
+
+    $_LOCKFILE "$1" $tmp $2 &
+    $_GREP -q true $tmp 2>/dev/null || return 1
+    
+    _VS_LOCKS="$! $_VS_LOCKS"
+}
+
+## Usage: unlock [<num>]
+function unlock
+{
+    local num=$1
+    local i
+
+    set -- $_VS_LOCKS
+    while test "$#" -gt 0; do
+       kill -HUP "$1" >/dev/null || :
+       shift
+       test "$num" != 1 || break
+       test -z "$num"   || let --num
+    done
+    _VS_LOCKS="$@"
+}
 
 function _pkgMountBindDir()
 {
     test "$1" != "$2" || return 0
 
-    mount -n --bind "$1" "$2"
+    $_MOUNT -n --bind "$1" "$2"
 }
 
 function _pkgSetVarsBase
@@ -214,11 +291,15 @@ function _pkgMountRPM
     _pkgMountBindDir "$RPMETCDIR" /etc/rpm
     test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm
 
-    "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
+    pushd "$VDIR" >/dev/null
+
+    $_SECURE_MOUNT --chroot -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
     test -z "$WORKAROUND_106057" || mount -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
 
     test -e "$VDIR"/proc/self/status || \
-       "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure -t proc none /proc
+       $_SECURE_MOUNT --chroot -n -t proc none /proc
+
+    popd >/dev/null
 }
 
 function _pkgSetEnvBase
@@ -293,21 +374,39 @@ function isAvoidNamespace
 
 function getAllVservers
 {
-    local i
-    declare -a _tmp=()
+    local _ga_i
+    declare -a _ga_tmp=()
 
-    for i in $CONFDIR/*; do
-       test   -d "$i"          || continue
-       test ! -e "$i"/disabled || continue
-       test   -d "$i"/vdir     || continue
-       case "$i" in
+    for _ga_i in $CONFDIR/*; do
+       test   -d "$_ga_i"          || continue
+       test ! -e "$_ga_i"/disabled || continue
+       test   -d "$_ga_i"/vdir     || continue
+       case "$_ga_i" in
            *.~*~) continue;;
        esac
 
-       _tmp=( "${_tmp[@]}" "${i##$CONFDIR/}")
+       _ga_tmp=( "${_ga_tmp[@]}" "${_ga_i##$CONFDIR/}")
     done
 
-    eval $1='( "${_tmp[@]}" )'
+    eval $1='( "${_ga_tmp[@]}" )'
+}
+
+## Usage: _getProcNumberCount <ctx> <var>
+function _getProcNumberCount
+{
+    local _gp_var=$2
+    local _gp_procnr_cnt=0
+
+    # Use /proc/virtual from kernel 2.6 when possible
+    if test -d "/proc/virtual"; then
+       set -- $($_GREP '^PROC:' "/proc/virtual/$1/limit" 2>/dev/null)
+       _gp_procnr_cnt=$2
+    else
+       _gp_procnr_cnt=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$1" | $_WC -l )
+    fi
+
+    let _gp_procnr_cnt=_gp_procnr_cnt+0
+    eval $_gp_var=\$_gp_procnr_cnt
 }
 
 ## Usage: getVserverCtx <vdir> <result-varname> [<procnumber-varname> [<do-cleanup>]]
@@ -316,18 +415,18 @@ function getVserverStatus
 {
     test -r "$1"/run || return 1
 
-    local _ctx
-    read _ctx <"$1"/run
-    eval "$2"=\$_ctx
+    local _gvs_ctx
+    read _gvs_ctx <"$1"/run
+    eval "$2"=\$_gvs_ctx
 
     test "$3"        || return 0
-    local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$_ctx" | $_WC -l )
-    let _tmp=_tmp+0
-    eval "$3"=\$_tmp
+    local _gvs_tmp
+    _getProcNumberCount "$_gvs_ctx" _gvs_tmp
+    eval "$3"=\$_gvs_tmp
 
-    if test "$_tmp" = 0; then
+    if test "$_gvs_tmp" = 0; then
        local runfile=$($_READLINK "$1/run")
-       test -z "$4" || rm -f "$runfile"
+       test -z "$4" || $_RM -f "$runfile"
        return 1
     fi
 
@@ -337,26 +436,32 @@ function getVserverStatus
 ## Usage: isCtxRunning <ctx>
 function isCtxRunning
 {
-    local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$1" | $_WC -l )
+    local _tmp
+    _getProcNumberCount "$1" _tmp
     test $_tmp -gt 0
 }
 
 ## Usage: isVserverRunning <vdir> [<ctx-varname>]
 function isVserverRunning
 {
-    local ctx procnum
+    local _ivr_ctx _ivr_procnum
 
-    getVserverStatus "$1" ctx procnum 1 || return 1
-    test $procnum != 0                  || return 1
-    test -z "$2" || eval "$2"=\$ctx
+    getVserverStatus "$1" _ivr_ctx _ivr_procnum 1 || return 1
+    test $_ivr_procnum != 0                       || return 1
+    test -z "$2" || eval "$2"=\$_ivr_ctx
     return 0
 }
 
-## Called as 'getFileValue <varname> <filename>'
+## Called as 'getFileValue <varname> <filename>+'
 function getFileValue
 {
-    test -r "$2" || return 0
-    eval read "$1" <"$2"
+    local _gfv_var=$1
+    local _gfv_file
+    shift
+
+    findFile _gfv_file "$@" ''
+    test "$_gfv_file" -a -r "$_gfv_file" || return 0
+    eval read "$_gfv_var" <"$_gfv_file"
 }
 
 ## Called as 'getFileArray <varname> <filename>'
@@ -379,11 +484,11 @@ function checkComponents
     for i; do
        local failed=
        case "$i" in
-           core)       test -x "$_CHBIND"           || failed=1;;
-           build)      test -x "$_VSERVER_BUILD"    || failed=1;;
-           sysv)       test -x "$INITRDDIR/vserver" || failed=1;;
-           devel)      test -d "$INCLUDEDIR/vserver.h" || failed=1;;
-           *)          echo "Unknown component '$i'" >&2
+           (core)      test -x "$_CHBIND"           || failed=1;;
+           (build)     test -x "$_VSERVER_BUILD"    || failed=1;;
+           (sysv)      test -x "$INITRDDIR/vserver" || failed=1;;
+           (devel)     test -d "$INCLUDEDIR/vserver.h" || failed=1;;
+           (*)         echo "Unknown component '$i'" >&2
                        return false
                        ;;
        esac
@@ -410,7 +515,7 @@ function callInNamespace
     local ctx=
     
     isAvoidNamespace "$1" || \
-    ctx=$( $_VSERVER_INFO "$1" CONTEXT t ) || ctx=
+    ctx=$( $_VSERVER_INFO "$1" CONTEXT f ) || ctx=
 
     shift
     if test "$ctx"; then
@@ -420,24 +525,39 @@ function callInNamespace
     fi
 }
 
+## Usage: setDefaultTTY <vdir> [<fallback-tty>]
+function setDefaultTTY
+{
+    local cfgdir ttyname
+
+    cfgdir=$($_VSERVER_INFO "$1" APPDIR init) || cfgdir=
+    findObject -e ttyname \
+       ${cfgdir:+"$cfgdir"/tty} \
+       "$CONFDIR/.defaults/apps/init/tty" \
+       $2 /dev/null
+
+    exec   <$ttyname
+    exec  &>$ttyname
+}
+
 ## Usage: pkgmgmt.guessStyle <vserver> <resultvar>
 function pkgmgmt.guessStyle()
 {
-    local vdir=$($_VSERVER_INFO "$1" VDIR) || {
+    local _pgs_vdir=$($_VSERVER_INFO "$1" VDIR) || {
        echo $"Can not determine vserver-root" >&2
        return 1
     }
-    local cfgdir=$($_VSERVER_INFO "$1" APPDIR pkgmgmt) || :
+    local _pgs_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
+    if test "$_pgs_cfgdir" -a -e "$_pgs_cfgdir"/style; then
+       read style <"$_pgs_cfgdir"/style
+    elif test -e "$_pgs_vdir"/etc/redhat-release -o -e "$_pgs_vdir"/etc/fedora-release; then
        style=redhat
-    elif test -e "$vdir"/etc/mandrake-release; then
+    elif test -e "$_pgs_vdir"/etc/mandrake-release; then
        style=mandrake
-    elif test -e "$vdir"/etc/debian_version; then
+    elif test -e "$_pgs_vdir"/etc/debian_version; then
        style=debian
-    elif test -e "$vdir"/etc/SuSE-release; then
+    elif test -e "$_pgs_vdir"/etc/SuSE-release; then
        style=suse
     else
        echo $"Can not determine packagemanagement style" >&2
@@ -506,7 +626,7 @@ function vshelper.doSanityCheck
     else
        vshelper=$(cat "$proc_file")
 
-       cmp -s "$vshelper" "$_VSHELPER" || {
+       $_CMP -s "$vshelper" "$_VSHELPER" || {
            warnings=( "${warnings[@]}"
                       $"The configured vshelper '$vshelper' does not match the 'vshelper'
   script of the util-vserver package"
@@ -519,9 +639,9 @@ function vshelper.doSanityCheck
     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" )
+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
     }
 
@@ -574,26 +694,26 @@ function vshelper.isEnabled
 
 function vshelper._getHandlerInternal
 {
-    local var=$1
-    local tmp
+    local _vghi_var=$1
+    local _vghi_tmp
     shift
     shift      ## HACK: see below the note about the 'set -u' mode
     
     while test "$#" -ge 2; do
-       local mod=$1
-       local obj=$2
+       local _vghi_mod=$1
+       local _vghi_obj=$2
        shift 2
 
-       test "$mod" "$obj" || continue
-       case "$mod" in
+       test "$_vghi_mod" "$_vghi_obj" || continue
+       case "$_vghi_mod" in
            (-x)
-               eval $var=\$obj
+               eval $_vghi_var=\$_vghi_obj
                ;;
            (-e)
-               read tmp <"$obj"
-               eval $var=:\$tmp
+               read _vghi_tmp <"$_vghi_obj"
+               eval $_vghi_var=:\$_vghi_tmp
                ;;
-           (*) panic $"Internal error, unexpected modifier '$mod'"
+           (*) panic $"Internal error, unexpected modifier '$_vghi_mod'"
        esac
        return 0
     done
@@ -604,17 +724,16 @@ function vshelper._getHandlerInternal
 ## Usage: vshelper.getHandler <result-var> <vserver> <action>
 function vshelper.getHandler
 {
-    local appdir=$($_VSERVER_INFO "$2" APPDIR vshelper) || appdir=
-    local script
-    declare -a search_list=( X )
+    local _vgh_appdir=$($_VSERVER_INFO "$2" APPDIR vshelper) || _vgh_appdir=
+    declare -a _vgh_search_list=( X )
     ## HACK: when we are in 'set -u' mode, empty lists are causing errors
 
-    test -z "$appdir" || search_list=( "${search_list[@]}" -x "$appdir/$3" )
-    test -z "$appdir" || search_list=( "${search_list[@]}" -e "$appdir/action" )
-    search_list=( "${search_list[@]}" -x "$CONFDIR"/.defaults/apps/vshelper/"$3" )
-    search_list=( "${search_list[@]}" -e "$CONFDIR"/.defaults/apps/vshelper/action )
+    test -z "$_vgh_appdir" || _vgh_search_list=( "${_vgh_search_list[@]}" -x "$_vgh_appdir/$3" )
+    test -z "$_vgh_appdir" || _vgh_search_list=( "${_vgh_search_list[@]}" -e "$_vgh_appdir/action" )
+    _vgh_search_list=( "${_vgh_search_list[@]}" -x "$CONFDIR"/.defaults/apps/vshelper/"$3" )
+    _vgh_search_list=( "${_vgh_search_list[@]}" -e "$CONFDIR"/.defaults/apps/vshelper/action )
 
-    ! vshelper._getHandlerInternal "$1" "${search_list[@]}" || return 0
+    ! vshelper._getHandlerInternal "$1" "${_vgh_search_list[@]}" || return 0
     eval $1=':restart'
 }
 
@@ -623,15 +742,15 @@ function vshelper.doInit
 {
     vshelper.isEnabled || return 0
     
-    local xid=$($_VSERVER_INFO "$1" CONTEXT false) || {
-       warning $"vshelper.init: can not determine xid of vserver '$vserver'"
+    local xid=$($_VSERVER_INFO "$1" CONTEXT false) && test "$xid" || {
+       warning $"vshelper.init: can not determine xid of vserver '$vserver'; returned value was '$xid'"
        return 1
     }
 
     local f="$VSHELPERSTATEDIR/$xid"
     
     set -C
-    rm -f "$f"
+    $_RM -f "$f"
     echo "$1" >"$f"
     set +C
     
@@ -653,31 +772,31 @@ function vshelper.doDestroy
 {
     vshelper.isEnabled || return 0
 
-    rm -f "$VSHELPERSTATEDIR/$2"
+    $_RM -f "$VSHELPERSTATEDIR/$2"
 }
 
 ## Usage: vshelper.initSync <vserver> <pipe-varname>
 function vshelper.initSync
 {
-    local tmpdir=$(mktemp -d /tmp/vserver-stop.XXXXXX) || {
+    local _vis_tmpdir=$($_MKTEMPDIR /tmp/vserver-stop.XXXXXX) || {
        warning $"Failed to generate temporary directory for vshelper sync"
        return 1
     }
 
-    local fifo="$tmpdir"/pipe
-    mkfifo -m700 "$fifo"
-    vshelper.doInit "$VSERVER_DIR" sync "$fifo"
-    eval $2=\$fifo
+    local _vis_fifo="$_vis_tmpdir"/pipe
+    $_MKFIFO -m700 "$_vis_fifo"
+    vshelper.doInit "$1" sync "$_vis_fifo"
+    eval $2=\$_vis_fifo
 }
 
 ## Usage: vshelper.getSyncTimeout <vserver> <varname>
 function vshelper.getSyncTimeout
 {
-    local appdir=$($_VSERVER_INFO "$1" APPDIR vshelper) || appdir=
-    local file tmp
+    local _vgst_appdir=$($_VSERVER_INFO "$1" APPDIR vshelper) || _vgst_appdir=
+    local _vgst_file _vgst_tmp
 
-    findFile file ${appdir:+"$appdir"/sync-timeout} "$CONFDIR"/.defaults/apps/vshelper/sync-timeout ''
-    test "$file" || return 1
-    read tmp <"$file"
-    eval $2=\$tmp
+    findFile _vgst_file ${_vgst_appdir:+"$_vgst_appdir"/sync-timeout} "$CONFDIR"/.defaults/apps/vshelper/sync-timeout ''
+    test "$_vgst_file" || return 1
+    read _vgst_tmp <"$_vgst_file"
+    eval $2=\$_vgst_tmp
 }