applied http://savannah.nongnu.org/patch/?func=detailitem&item_id=4814
[util-vserver.git] / util-vserver / scripts / functions
index f5987d8..8255487 100644 (file)
@@ -1,6 +1,6 @@
 # $Id$ --*- sh -*--
 
-# Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+# Copyright (C) 2003,2004,2005,2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 #  
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -21,6 +21,7 @@ _VS_ERRFILE=
 _VS_NEWLINE='
 '
 declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1}
+declare -r VS_ALLVSERVERS_ARGS=all,marked,unmarked,stopped,running
 
 function findObject
 {
@@ -66,12 +67,12 @@ function findAndCopy
     $_CP -af "$tmp" "$dst"
 }
 
-## Usage: isRegularFile <filename>
+## Usage: isRegularFile <filename> [<mod>]
 function isRegularFile
 {
-    test -f "$1" || return 1
+    test ${2:--f} "$1" || return 1
 
-    case $file in
+    case $1 in
        (*.rpmsave|*.rpmnew|*.rpmorig|*.cfsaved*|*.~*~) return 1;;
     esac
 
@@ -83,6 +84,29 @@ function getPhysicalDir
     ( set -P && cd "$1" && pwd )
 }
 
+## Usage:: isDirectoryEmpty <dir> [<ignore-lost+found>]
+function isDirectoryEmpty
+{
+    (
+       shopt -s dotglob  || *
+       shopt -s nullglob || *
+
+       ignore_lostfound=1
+       test -z "$2" -o "$2" = 0 || ignore_lostfound=
+       
+       for i in "$1"/*; do
+           case "$i" in
+               ($1/lost+found)
+                   test "$ignore_lostfound" || exit 1
+                   ;;
+               (*)     echo "$i"; exit 1
+           esac
+        done
+
+       exit 0
+    )
+}
+
 ## Usage: logging <message>
 function logging
 {
@@ -139,6 +163,22 @@ function isNumber
     return 0
 }
 
+## Usage: hasSubstring <haystack> <needle>+
+function hasSubstring
+{
+    local pat=$1
+    local i
+    
+    shift
+
+    for i; do
+       test x"${pat/*$i*/$i}" = x"$i" || continue
+       return 0
+    done
+
+    return 1
+}
+
 ## Usage: colorize <style> <command>
 function colorize
 {
@@ -163,11 +203,34 @@ function colorize
          esac
        fi
            
-       "$@"
+       ( "$@" )
        echo -ne "\e[m"
     fi
 }
 
+function colpanic
+{
+    if test -n "$_VS_ERRFILE"; then
+       echo "$@" >>"$_VS_ERRFILE"
+    else
+       colorize error echo -n "$@" >&2
+    fi
+    echo
+
+    exit 1
+}
+
+function colwarning
+{
+    colorize warning warning "$@"
+}
+
+function colinfo
+{
+    colorize info    echo "$@"
+}
+
+
 ## Usage: xtermTitle <title>
 function xtermTitle
 {
@@ -417,25 +480,53 @@ function isAvoidNamespace
          -e "$cfgdir"/nonamespace
 }
 
+## Usage: getAllVservers <var> [<KIND>*]
 function getAllVservers
 {
     local _ga_i
     declare -a _ga_tmp=()
 
     for _ga_i in $__CONFDIR/*; do
-       test   -d "$_ga_i"          || continue
+       isRegularFile "$_ga_i" -d   || continue
+
        test ! -e "$_ga_i"/disabled || continue
        test   -d "$_ga_i"/vdir     || continue
-       case "$_ga_i" in
-           *.~*~) continue;;
+
+       local _ga_doadd=1
+       local _ga_markfile=$_ga_i/apps/init/mark
+       
+       case ${2:-ALL} in
+           (MARKED)    test   -s "$_ga_markfile" || _ga_doadd=;;
+           (UNMARKED)  test ! -s "$_ga_markfile" || _ga_doadd=;;
+           (STOPPED)   ! $_VSERVER "$_ga_i" running &>/dev/null || _ga_doadd=;;
+           (RUNNING)     $_VSERVER "$_ga_i" running &>/dev/null || _ga_doadd=;;
+           (ALL)       ;;
+           (*)         panic $"Unknown vserver tagging '$2'";;
        esac
 
-       _ga_tmp=( "${_ga_tmp[@]}" "${_ga_i##$__CONFDIR/}")
+       test -z "$_ga_doadd" || _ga_tmp=( "${_ga_tmp[@]}" "${_ga_i##$__CONFDIR/}")
     done
 
     eval $1='( "${_ga_tmp[@]}" )'
 }
 
+## Usage: getAllVserversByArg <var> <arg>
+function getAllVserversByArg
+{
+    local _gav_mark=
+    
+    case $2 in
+       (--all)         _gav_mark=ALL;;
+       (--marked)      _gav_mark=MARKED;;
+       (--unmarked)    _gav_mark=UNMARKED;;
+       (--stopped)     _gav_mark=STOPPED;;
+       (--running)     _gav_mark=RUNNING;;
+       (*)             return 1;;
+    esac
+
+    getAllVservers "$1" "$_gav_mark"
+}
+
 ## Usage: _getProcNumberCount <ctx> <var>
 function _getProcNumberCount
 {
@@ -747,7 +838,7 @@ To fix this, you can:"
 
        test -x "$solution_sysctl" || warning $"\
 * configure the util-vserver vshelper script, e.g. by adding
-  | kernel.vshelper = \"$_VSHELPER\"
+  | kernel.vshelper = $_VSHELPER
   to /etc/sysctl.conf and rebooting the machine, or by executing
   | echo \"$_VSHELPER\" >$proc_file"
 
@@ -885,7 +976,7 @@ function vshelper.initSync
 
     local _vis_fifo="$_vis_tmpdir"/pipe
     $_MKFIFO -m700 "$_vis_fifo"
-    vshelper.doInit "$1" "${method:-sync}" "$_vis_fifo"
+    vshelper.doInit "$1" "${3:-sync}" "$_vis_fifo"
     eval $2=\$_vis_fifo
 }