check whether filesystem is mounted readonly; if so, execute
[util-vserver.git] / util-vserver / scripts / vserver.functions
index 5877853..a2a56a7 100644 (file)
@@ -28,6 +28,7 @@ declare -a CHCONTEXT_OPTS=()
 declare -a CAPCHROOT_OPTS=()
 declare -a INTERFACES=()
 
+declare -a INITCMD_RESCUE=( /bin/sleep 900 )
 declare -a INITCMD_START=()
 declare -a INITCMD_START_SYNC=()
 declare -a INITCMD_STOP=()
@@ -38,6 +39,7 @@ declare -a ENTER_SHELL=()
 
 declare -a OPTS_VCONTEXT_CREATE=()
 declare -a OPTS_VCONTEXT_MIGRATE=()
+declare -a OPTS_VCONTEXT_ENTER=()
 declare -a OPTS_VATTRIBUTE=( --flag fakeinit )
 declare -a OPTS_VSCHED=()
 
@@ -45,6 +47,7 @@ declare -a STOPCMD_PREPARE=()
 
 declare -a VSERVER_EXTRA_CMDS=()
 
+INIT_RESCUE=
 VSHELPER_SYNC_TIMEOUT=30
 USE_VNAMESPACE=
 INTERFACE_CMDS_IDX=0
@@ -100,6 +103,34 @@ function _generateNiceCommand
     NICE_CMD=( $_NICE -$nice )
 }
 
+
+function _generatePersonalityOptions
+{
+    local vdir="$1"
+    local f="$vdir"/personality
+    local type flags
+
+    test -s "$f" || return 0
+
+    {
+       local delim tmp
+
+       read type
+       while read tmp; do
+           case x$tmp in
+               (x\#*|x)        ;;
+               (*)             flags=$flags$delim$tmp
+                               delim=,
+                               ;;
+           esac
+       done
+    } <"$f"
+
+    OPTS_VCONTEXT_ENTER=( "${OPTS_VCONTEXT_ENTER[@]}"
+                          --personality-type "$type"
+                         ${flags:+--personality-flags "$flags"} )
+}
+
 function _generateCCapabilityOptions
 {
     local vdir=$1
@@ -218,6 +249,13 @@ function _generateInitOptions
     getFileArray INITKILL_SEQ  "$cfgdir"/killseq || :
 
     case x"$INITSTYLE" in
+       (xrescue)
+           INITCMD_START=( "${INITCMD_RESCUE[@]}" )
+           INITCMD_STOP=( /sbin/killall5 )
+           _IS_FAKEINIT=1
+           _NEED_VSHELPER_SYNC=
+           ;;
+           
        (xsysv)
            test -n "$RUNLEVEL_START" || RUNLEVEL_START=3
            test -n "$RUNLEVEL_STOP"  || RUNLEVEL_STOP=6
@@ -260,12 +298,13 @@ function _generateInitOptions
            exit 1;;
     esac
 
-    getFileArray INITCMD_START      "$cfgdir"/cmd.start      || :
-    getFileArray INITCMD_STOP       "$cfgdir"/cmd.stop       || :
-    getFileArray INITCMD_START_SYNC "$cfgdir"/cmd.start-sync || :
-    getFileArray INITCMD_STOP_SYNC  "$cfgdir"/cmd.stop-sync  || :
-    getFileArray INITCMD_PREPARE    "$cfgdir"/cmd.prepare    || :
-
+    if test x"$INITSTYLE" != xrescue; then
+       getFileArray INITCMD_START      "$cfgdir"/cmd.start      || :
+       getFileArray INITCMD_STOP       "$cfgdir"/cmd.stop       || :
+       getFileArray INITCMD_START_SYNC "$cfgdir"/cmd.start-sync || :
+       getFileArray INITCMD_STOP_SYNC  "$cfgdir"/cmd.stop-sync  || :
+       getFileArray INITCMD_PREPARE    "$cfgdir"/cmd.prepare    || :
+    fi
     
     test -n "$OPTION_FORCE_SYNC" -o -e "$cfgdir"/sync || {
        INITCMD_START_SYNC=()
@@ -585,11 +624,11 @@ function prepareInit
     case "$INITSTYLE" in
        sysv)
            { find var/run  ! -type d -print0; \
-             find var/lock ! -type d -print0; } | xargs -0r $_CHROOT_RM
+             find var/lock ! -type d -print0; } | xargs -0r $_CHROOT_SH rm
            ;;
        plain)
-           $_CHROOT_RM .autofsck forcefsck 2>/dev/null || :
-           : | $_CHROOT_CAT fastboot       2>/dev/null || :
+           $_CHROOT_SH rm .autofsck forcefsck 2>/dev/null || :
+           : | $_CHROOT_SH truncate fastboot  2>/dev/null || :
            ;;
        minit)
            ;;
@@ -614,12 +653,13 @@ function prepareStop
 
 function generateOptions
 {
-    _generateInterfaceOptions  "$1"
+    _generateInterfaceOptions   "$1"
     test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$1" 
-    _generateNiceCommand       "$1"
-    _generateInitOptions       "$1"
-    _generateChcontextOptions  "$1"
-    _generateScheduleOptions   "$1"
+    _generateNiceCommand        "$1"
+    _generateInitOptions        "$1"
+    _generateChcontextOptions   "$1"
+    _generateScheduleOptions    "$1"
+    _generatePersonalityOptions "$1"
 
     if test -n "$_IS_FAKEINIT"; then
        CHCONTEXT_INIT_OPTS=( --disconnect --flag fakeinit )
@@ -630,11 +670,17 @@ function generateOptions
 function _mountVserverInternal
 {
     local fstab="$1"
+    local xflag=
+    
     test -e "$fstab" || return 0
     shift
 
     pushd "$vdir" >/dev/null
-    "$@" $_SECURE_MOUNT -a --chroot --fstab "$fstab" --rootfs no
+    # check whether / is mounted readonly or whether there is special
+    # 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
+    "$@" $_SECURE_MOUNT -a $xflag --chroot --fstab "$fstab" --rootfs no
     popd >/dev/null
 }
 
@@ -643,10 +689,15 @@ function mountRootFS
     local cfgdir=$1
     local vdir=$1/vdir
     local fstab="$cfgdir"/fstab
+    local xflag=
 
     test -e "$fstab" || return 0
     pushd "$vdir" >/dev/null
-    $_SECURE_MOUNT -a --chroot --fstab "$fstab" --rootfs only -n
+    # check whether / is mounted readonly or whether there is special
+    # 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
+    $_SECURE_MOUNT -a $xflag --chroot --fstab "$fstab" --rootfs only -n
     popd >/dev/null
 }
 
@@ -664,7 +715,7 @@ function mountVserver
     findObject -r mtab_src "$cfgdir"/apps/init/mtab "$__CONFDIR"/.defaults/init/mtab "$__PKGLIBDEFAULTDIR"/mtab /dev/null
     
     pushd "$vdir" >/dev/null
-    $_CHROOT_CAT /etc/mtab <"$mtab_src"
+    $_CHROOT_SH truncate /etc/mtab <"$mtab_src"
     popd >/dev/null
 
     test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$cfgdir"
@@ -729,13 +780,62 @@ function initSync
     vshelper.initSync "$1" "$2" "$_is_meth"
 }
 
-## Usage: waitForSync <vserver> [<vshelper-fifo>]
+## Usage: initWait <vserver> <context> <vwait-tmpdir-varname>
+function initWait
+{
+    if $_VSERVER_INFO - FEATURE vwait; then
+       local _is_tmpdir
+       _is_tmpdir=$($_MKTEMPDIR /tmp/vwaitstat.XXXXXX)
+           
+       $_NOHUP $_VWAIT --timeout "$VSHELPER_SYNC_TIMEOUT" \
+           --terminate --status-fd 3 "$2"  \
+           >>$_is_tmpdir/out 2>$_is_tmpdir/err 3>$_is_tmpdir/fifo &
+
+       echo "$!" >$_is_tmpdir/pid
+       eval "$4"=$_is_tmpdir
+    fi
+}
+
+
+## Usage: _waitForVWait <fifo> <pid>
+function _waitForVWait
+{
+    declare -a status
+    getFileArray status "$1"
+    wait "$2" || :
+    set -- ${status[0]}
+
+    case "$1" in
+       (ERROR)         warning $"\
+'vwait' exited with error '$2' which indicates that vserver could not
+be stopped properly"
+                       ;;
+       (FINISHED)      ;;
+       (KILLED)        warning $"\
+A timeout occured while waiting for the vserver to finish and it was
+killed by sending a SIGKILL signal. Please investigate the reasons
+and/or increase the timeout in apps/vshelper/sync-timeout."
+                       ;;
+       (TIMEOUT|\?\?\?|*)      warning $"\
+internal error: 'vwait' exited with an unexpected status; I will try
+to continue but be prepared for unexpected events."
+                       ;;
+    esac
+
+    return 0
+}
+
+## Usage: waitForSync <vserver> [<vshelper-fifo>] [<vwait-statdir>]
 function waitForSync
 {
     local cfgdir=$1
     local fifo=$2
-    
-    if test -n "$_NEED_VSHELPER_SYNC"; then
+    local vwait_statdir=$3
+    local vwait_pid=$4
+
+    if test -d "$vwait_statdir"; then
+       _waitForVWait "$vwait_statdir/fifo" "$( <$vwait_statdir/pid )"
+    elif test -n "$_NEED_VSHELPER_SYNC"; then
        $_VSHELPER_SYNC "$fifo" "$VSHELPER_SYNC_TIMEOUT" || \
            warning $"\
 A timeout or other error occured while waiting for the synchronization
@@ -771,7 +871,9 @@ function execScriptlets
        local   already_handled=
        
        for i in "$base/$flavor" "$base/$flavor.d"/*; do
-           test -f "$i" -a -r "$i" || continue
+           isRegularFile "$i" || continue
+           test  -r "$i"      || continue
+
            already_handled=1
            local start=
            test -x "$i" || start=_sourceWrap
@@ -823,4 +925,10 @@ WARNING: The 'only_ip' flag for interface '$iface' is deprecated; use
 
     $_VSERVER_INFO - VERIFYCAP ||
        panic $"capabilities are not enabled in kernel-setup"
+
+    $_VSERVER_INFO - VERIFYPROC ||
+       panic $"\
+/proc/uptime can not be accessed. Usually, this is caused by
+procfs-security. Please read the FAQ for more details
+http://www.linux-vserver.org/index.php?page=Linux-Vserver+FAQ"
 }