gentoo: use /var/run for new /run compatibility
[util-vserver.git] / scripts / vserver.functions
index d35a4fe..77ca912 100644 (file)
@@ -42,6 +42,7 @@ declare -a OPTS_VCONTEXT_CREATE=()
 declare -a OPTS_VCONTEXT_MIGRATE=()
 declare -a OPTS_VCONTEXT_ENTER=()
 OPT_VCONTEXT_CHROOT=--chroot
+OPT_VCONTEXT_CLOSE_FD=""
 declare -a OPTS_VATTRIBUTE=( --flag fakeinit )
 declare -a OPTS_VSCHED=()
 declare -a OPTS_ENV=()
@@ -154,15 +155,21 @@ function _generateNiceCommand
 function _generateIONiceCommand
 {
     local vdir=$1
-    local ionice_class=2
-    local ionice_priority=0
+    local ionice_class=
+    local ionice_priority=
 
     test -n "$_IONICE" || return 0
 
     test -r "$vdir/ionice/class" && read ionice_class <"$vdir"/ionice/class
     test -r "$vdir/ionice/priority" && read ionice_priority <"$vdir"/ionice/priority
 
-    IONICE_CMD=( $_IONICE -c$ionice_class -n$ionice_priority )
+    test -n "$ionice_class$ionice_priority" || return 0
+
+    if test -z "$ionice_class" -a -n "$ionice_priority"; then
+       IONICE_CMD=( $_IONICE -c2 -n"$ionice_priority" )
+    else
+       IONICE_CMD=( $_IONICE ${ionice_class:+-c$ionice_class} ${ionice_priority:+-n$ionice_priority} )
+    fi
 }
 
 function _generatePersonalityOptions
@@ -206,6 +213,13 @@ function _generateBCapabilityOptions
     _readFileToArray "$vdir"/bcapabilities OPTS_VATTRIBUTE --bcap
 }
 
+function _generateUMaskOptions
+{
+    local vdir=$1
+
+    _readFileToArray "$vdir"/umask OPTS_VATTRIBUTE --umask
+}
+
 function _generateCapabilityOptions
 {
     local vdir=$1
@@ -342,7 +356,7 @@ function _generateInitOptions
            ! isNumber "${RUNLEVEL_START:-3}" || INITCMD_PREPARE=( $_FAKE_RUNLEVEL "${RUNLEVEL_START:-3}" /var/run/utmp )
            ;;
 
-       (xgentoo)
+       (xgentoo|xopenrc)
            test -n "$RUNLEVEL_START" || RUNLEVEL_START="default"
            RC_PATH=/usr/sbin:/usr/bin:/sbin:/bin
 
@@ -442,6 +456,7 @@ function _generateChcontextOptions
 
     _generateCapabilityOptions "$vdir"
     _generateFlagOptions       "$vdir"
+    _generateUMaskOptions      "$vdir"
 
     CHCONTEXT_OPTS=( $SILENT_OPT \
                      "${CHCONTEXT_FLAG_OPTS[@]}" \
@@ -453,6 +468,8 @@ function _generateChcontextOptions
 
     OPTS_VCONTEXT_CREATE=( $SILENT_OPT \
                           ${ctx:+--xid "$ctx"} )
+    findFile file "$vdir"/keepfds "$__CONFDIR/.defaults/keepfds" ""
+    test -n "$file" || OPT_VCONTEXT_CLOSE_FD="--closefd"
     ## put '--secure' at front so that it can be overridden
     OPTS_VATTRIBUTE=( --secure --flag default "${OPTS_VATTRIBUTE[@]}" )
 }
@@ -762,7 +779,11 @@ function disableInterfaces
            IP_ADDR_FLUSH)      ;;
            IP_LINK)            ;; ## Ignore the link-down command for now
            IP_ROUTE)           $_IP route del "$@";;
-           TUNCTL)             $_TUNCTL --~persist "$1";;
+           TUNCTL)
+               local dev="$1"
+               shift
+               $_TUNCTL --~persist "$@" "$dev"
+               ;;
            *)                  echo "Unknown interface-command type '$type'" >&2; false;;
        esac
     done
@@ -825,8 +846,11 @@ function prepareInit
     pushd "$1/vdir" >/dev/null
     case "$INITSTYLE" in
        sysv)
-           { $_FIND var/run  ! -type d -print0; \
-             $_FIND var/lock ! -type d -print0; } | xargs -0r $_CHROOT_SH rm
+           local -a dirs=( $($_CHROOT_SH realpath /var/run /var/lock | $_SED 's!^/*!!' || :) )
+           local dir
+           for dir in "${dirs[@]}"; do
+               $_FIND $dir ! -type d -print0 | xargs -0r $_CHROOT_SH rm
+           done
            ;;
        plain)
            $_CHROOT_SH rm .autofsck forcefsck 2>/dev/null || :
@@ -1395,11 +1419,19 @@ function _namespaceCleanup
        done
     done
 
+    # keep cgroup mount points
+    list=( "${list[@]}" "${CGROUP_MNT}" )
+    if test -n "$CGROUP_MNT_PER_SS"; then
+        for ss in "${CGROUP_SUBSYS[@]}"; do
+            list=( "${list[@]}" "${CGROUP_MNT}/${ss}" )
+        done
+    fi
+
     local -a list_umount
     while read -r dev path opts; do
        test -n "$path" || continue
-       for i in "$root" /dev /proc; do
-           path_dir="${path}/"
+       path_dir="${path}/"
+       for i in "$root" /dev /proc /sys; do
            test "${path_dir#${i}/}" != "${path_dir}" && continue 2
        done
        for i in "${list[@]}" /; do
@@ -1453,15 +1485,18 @@ function _generateCgroupOptions
 
     hasCgroup || return 0
 
-    findFile file "$__CONFDIR/.defaults/cgroup/mnt" ""
-    if test -n "$file"; then
-       read CGROUP_MNT < "$file"
-    fi
     findFile file "$__CONFDIR/.defaults/cgroup/subsys" ""
     if test -n "$file"; then
        _readFileToArray CGROUP_SUBSYS "$file"
     else
-       CGROUP_SUBSYS=( $($_SED '/^#/d;/^ns[[:space:]]/d;s/[[:space:]].*//' /proc/cgroups) )
+       CGROUP_SUBSYS=( $($_AWK '/^#/ { next; } $1 == "ns" { next; } $4 != "0" { print $1; }' /proc/cgroups) )
+    fi
+    findFile file "$__CONFDIR/.defaults/cgroup/mnt" ""
+    if test -n "$file"; then
+       read CGROUP_MNT < "$file"
+    elif test -d /sys/fs/cgroup -a -d /sys/fs/cgroup/"${CGROUP_SUBSYS[0]}"; then
+       CGROUP_MNT=/sys/fs/cgroup
+       CGROUP_MNT_PER_SS=1
     fi
     findFile file "$__CONFDIR/.defaults/cgroup/inherit" ""
     if test -n "$file"; then