Support a base cgroup.
[util-vserver.git] / scripts / functions
index fe7521b..7d6278b 100644 (file)
@@ -21,7 +21,6 @@ _VS_ERRFILE=
 _VS_NEWLINE='
 '
 declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1}
-declare -r VS_ALLVSERVERS_ARGS=all,marked,unmarked,stopped,running
 
 function findObject
 {
@@ -294,11 +293,7 @@ function _setVserverDir
 
 function _setVserverName
 {
-    if test -e "$VSERVER_DIR"/name; then
-       read VSERVER_NAME <"$VSERVER_DIR"/name
-    else
-       VSERVER_NAME=$(basename "$VSERVER_DIR")
-    fi
+    VSERVER_NAME=$(basename "$VSERVER_DIR")
 }
 
 function _pkgMountBindDir()
@@ -531,6 +526,10 @@ function getAllVservers
 {
     local _ga_i
     declare -a _ga_tmp=()
+    local _ga_IFS="$IFS"
+    IFS=,
+    local -a _ga_marks=( $3 )
+    IFS="$_ga_IFS"
 
     for _ga_i in $__CONFDIR/*; do
        isRegularFile "$_ga_i" -d   || continue
@@ -547,6 +546,19 @@ function getAllVservers
            (STOPPED)   ! $_VSERVER "$_ga_i" running &>/dev/null || _ga_doadd=;;
            (RUNNING)     $_VSERVER "$_ga_i" running &>/dev/null || _ga_doadd=;;
            (ALL)       ;;
+           (MARK)      local _ga_j
+                       local _ga_mark
+                       local _ga_f=false
+                       local _ga_invert=false
+                       for _ga_j in "${_ga_marks[@]}"; do
+                           _ga_mark="$(echo "$_ga_j" | $_SED 's/^[!~]//')"
+                           test "$_ga_j" = "$_ga_mark" || _ga_invert=true
+                           test -s "$_ga_markfile" && $_GREP -qx "$_ga_mark" "$_ga_markfile" && \
+                               _ga_f=true || :
+                       done
+                       test $_ga_f = $_ga_invert && \
+                           _ga_doadd=
+                       ;;
            (*)         panic $"Unknown vserver tagging '$2'";;
        esac
 
@@ -556,21 +568,62 @@ function getAllVservers
     eval $1='( "${_ga_tmp[@]}" )'
 }
 
+declare -r VS_ALLVSERVERS_ARGS=all,marked,unmarked,stopped,running,mark:
 ## 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
+    local _gav_v="$1"
+    local _gav_first=1
+    local _gav_res=()
+    shift
+
+    while test "$#" -ge 1; do
+       local _gav_extra=
+        local _gav_tmp=()
+
+       case "$1" in
+           (--all)             _gav_mark=ALL;;
+           (--marked)          _gav_mark=MARKED;;
+           (--unmarked)        _gav_mark=UNMARKED;;
+           (--stopped)         _gav_mark=STOPPED;;
+           (--running)         _gav_mark=RUNNING;;
+           (--mark*)           _gav_mark=MARK
+                               _gav_extra=${_gav_i##*=}
+                               test -n "$_gav_extra" -a "$_gav_extra" != "--mark" || \
+                                   _gav_extra="$2"
+                               shift
+                               ;;
+           (--)                break;;
+           (*)                 set -- "$_gav_v" "$@"
+                               return 1;;
+       esac
 
-    getAllVservers "$1" "$_gav_mark"
+       getAllVservers _gav_tmp "$_gav_mark" $_gav_extra
+       if test -n "$_gav_first"; then
+           _gav_res=( "${_gav_tmp[@]}" )
+       else
+           local _gav_i=0
+           local _gav_n="${#_gav_res[@]}"
+           while test $_gav_i -lt $_gav_n; do
+               local _gav_found=0
+               for _gav_j in "${_gav_tmp[@]}"; do
+                   if test "$_gav_j" = "${_gav_res[$_gav_i]}"; then
+                       _gav_found=1
+                       break
+                   fi
+               done
+               if test "$_gav_found" = 0; then
+                   unset _gav_res[$_gav_i]
+               fi
+               let ++_gav_i
+           done
+           _gav_res=( "${_gav_res[@]}" )
+       fi
+       shift
+       _gav_first=
+    done
+    eval $_gav_v='( "${_gav_res[@]}" )'
 }
 
 ## Usage: _getProcNumberCount <ctx> <var>
@@ -601,10 +654,16 @@ function getVserverStatus
     read _gvs_ctx <"$1"/run
     eval "$2"=\$_gvs_ctx
 
-    test -n "$3"     || return 0
-    local _gvs_tmp
-    _getProcNumberCount "$_gvs_ctx" _gvs_tmp
-    eval "$3"=\$_gvs_tmp
+    if test "$1" = "$($_VUNAME -g --xid "$_gvs_ctx" context)"; then
+       test -n "$3" || return 0
+       local _gvs_tmp
+       _getProcNumberCount "$_gvs_ctx" _gvs_tmp
+       eval "$3"=\$_gvs_tmp
+    else
+       test -n "$3" || return 1
+       eval "$3"=0
+       _gvs_tmp=0
+    fi
 
     if test "$_gvs_tmp" = 0; then
        local runfile=$($_READLINK "$1/run")
@@ -994,7 +1053,11 @@ function vshelper.doInit
     
     local xid
     xid=$($_VSERVER_INFO "$1" CONTEXT false) && test -n "$xid" || {
-       warning $"vshelper.init: can not determine xid of vserver '$vserver'; returned value was '$xid'"
+       warning $"vshelper.init: can not determine xid of vserver '$vserver'; returned value was '$xid'
+
+This usually means that you're using an init-less init-style, but the
+guest isn't configured to start any service. Try enabling a service,
+changing the init-style, or making the contexts persistent."
        return 1
     }