Enter namespaces before context setup.
[util-vserver.git] / scripts / vserver.functions
index 2e8897e..d979401 100644 (file)
@@ -49,6 +49,8 @@ declare -a OPTS_VTAG_CREATE=()
 declare -a OPTS_VTAG_ENTER=()
 declare -a OPTS_VMEMCTRL=()
 declare -a OPTS_VSPACE=( --default )
+declare -a OPTS_VSPACE_SHARED=()
+declare -a VSPACE_SHARED_CMD=()
 
 declare -a STOPCMD_PREPARE=()
 
@@ -344,7 +346,8 @@ function _generateInitOptions
                panic "init-vserver.sh not found; aborting"
            fi
 
-           INITCMD_START=( env TERM=$TERM $RC_WRAP "$RUNLEVEL_START" )
+           OPTS_ENV=( "${OPTS_ENV[@]}" TERM=$TERM )
+           INITCMD_START=( $RC_WRAP "$RUNLEVEL_START" )
            INITCMD_STOP=( env -i PATH=$RC_PATH TERM=$TERM RUNLEVEL=0 /sbin/rc shutdown )
            INITCMD_PREPARE=( $_FAKE_RUNLEVEL 3 /var/run/utmp )
            ;;
@@ -786,16 +789,30 @@ function _generateSpaceOptions
 {
     local vdir="$1"
     local d="$vdir"/spaces
+    local shared
 
-    ( test ! -e "$d"/pid ) || \
-       OPTS_VSPACE=( "${OPTS_VSPACE[@]}" --pid )
+    if test -e "$d"/pid; then
+       getFileValue shared "$d"/pid
+       if test -z "$shared"; then
+           OPTS_VSPACE=( "${OPTS_VSPACE[@]}" --pid )
+       else
+           OPTS_VSPACE_SHARED=( "${OPTS_VSPACE_SHARED[@]}" --pid )
+           VSPACE_SHARED_CMD=( "${VSPACE_SHARED_CMD[@]}" $_VSPACE --enter "$shared" --pid -- )
+       fi
+    fi
 
-    test ! -e "$d"/net || {
-       OPTS_VSPACE=( "${OPTS_VSPACE[@]}" --net )
+    if test -e "$d"/net; then
+       getFileValue shared "$d"/net
        # network context and namespace don't make much sense
        _HAVE_CHBIND_OPTIONS=1
        CHBIND_CMD=()
-    }
+       if test -z "$shared"; then
+           OPTS_VSPACE=( "${OPTS_VSPACE[@]}" --net )
+       else
+           OPTS_VSPACE_SHARED=( "${OPTS_VSPACE_SHARED[@]}" --net )
+           VSPACE_SHARED_CMD=( "${VSPACE_SHARED_CMD[@]}" $_VSPACE --enter "$shared" --net -- )
+       fi
+    fi
 
     local mask
     getFileValue mask "$d"/mask || \
@@ -808,8 +825,8 @@ 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
+           { $_FIND var/run  ! -type d -print0; \
+             $_FIND var/lock ! -type d -print0; } | xargs -0r $_CHROOT_SH rm
            ;;
        plain)
            $_CHROOT_SH rm .autofsck forcefsck 2>/dev/null || :
@@ -918,6 +935,10 @@ function _mountVserverInternal
     # magic regarding the mtab file; when etc/mtab can not be touched,
     # add the '-n' flag to mount
     test -w etc -o -w etc/mtab || xflag=-n
+    if test -h etc/mtab; then
+       local l=$($_READLINK etc/mtab)
+       test "${l##/proc/}" = "$l" || xflag=-n
+    fi
     "$@" $_SECURE_MOUNT -a $xflag --chroot --fstab "$fstab" --rootfs no
     popd >/dev/null
 }
@@ -1213,7 +1234,7 @@ WARNING: There is no cachedirectory configured for this vserver;
   ln -s ../.defaults/cachebase/$VSERVER_NAME $cfgdir/cache
 "
 
-    find "$cfgdir" -type f -exec "$_CHECK_UNIXFILE" '{}' ';'
+    $_FIND "$cfgdir" -type f -exec "$_CHECK_UNIXFILE" '{}' ';'
 
     vshelper.doSanityCheck
 
@@ -1378,7 +1399,8 @@ function _namespaceCleanup
     while read -r dev path opts; do
        test -n "$path" || continue
        for i in "$root" /dev /proc; do
-           test "${path#$i}" != "$path" && continue 2
+           path_dir="${path}/"
+           test "${path_dir#${i}/}" != "${path_dir}" && continue 2
        done
        for i in "${list[@]}" /; do
            test "$path" = "$i" && continue 2
@@ -1438,6 +1460,10 @@ function _generateCgroupOptions
     findFile file "$__CONFDIR/.defaults/cgroup/subsys" ""
     if test -n "$file"; then
        read CGROUP_SUBSYS < "$file"
+    elif $_GREP -q '^ns[[:space:]]' /proc/cgroups; then
+       # Hack for the ns subsystem, with which we are incompatible
+       CGROUP_SUBSYS=$($_SED '/^#/d;/^ns[[:space:]]/d;s/[[:space:]].*//' /proc/cgroups | \
+                       (s=""; while read x; do test -n "$s" && s="$s,"; s="$s$x"; done; echo "$s"))
     fi
     findFile file "$__CONFDIR/.defaults/cgroup/inherit" ""
     if test -n "$file"; then
@@ -1487,7 +1513,10 @@ function _handleCgroup
 
            shopt -s nullglob
            for i in "$dir"/*; do
-               cat "$i" > "$CGROUP_MNT/$name/${i##*/}"
+               f="${i##*/}"
+               test "$f" != mnt -a "$f" != subsys -a \
+                   "$f" != inherit -a "$f" != name || continue
+               cat "$i" > "$CGROUP_MNT/$name/$f"
            done
        fi
        echo "$$" > "$CGROUP_MNT/$name/tasks"