gentoo: use /var/run for new /run compatibility
[util-vserver.git] / scripts / vserver-build.functions
index 7baae59..e423e5c 100644 (file)
@@ -44,12 +44,26 @@ function makeDevEntry
        (f)     touch "$dst"
                chmod $4 "$dst"
                ;;
+       (l)     ln -s "$4" "$dst"
+               ;;
        (*)     echo "Unknown dev-entry mode '$3'" >&2
                false
                ;;
     esac
 }
 
+function populateDev
+{
+    local spec
+
+    mkdir -p -m755 "$VDIR"/dev
+    mkdir -m755 "$VDIR"/dev/pts
+
+    while read spec; do
+       makeDevEntry "$VDIR"/dev $spec
+    done <$_DEV_FILE
+}
+
 function populateDirectory
 {
     local dst=$1
@@ -101,7 +115,7 @@ function _setCacheDir
     }
 }
 
-function _setVserverDir
+function _setVserverDirName
 {
     test -z "$VSERVERDIRNAME" || return 0
     VSERVERDIRNAME="$VSERVER_NAME"
@@ -141,6 +155,9 @@ function getDistribution
        if test -e /etc/fedora-release; then
            set -- $(cat /etc/fedora-release)
            DISTRIBUTION=fdr$4
+       elif test -e /etc/mandrake-release; then
+           set -- $(cat /etc/mandrake-release)
+           DISTRIBUTION=mdv$4
        elif test -e /etc/redhat-release; then
            set -- $(cat /etc/redhat-release)
            DISTRIBUTION=rh$5
@@ -172,6 +189,8 @@ ERROR: Can not determine distribution; please specify it manually with
 ERROR: Can not find configuration for the distribution '$DISTRIBUTION';
   please read http://linux-vserver.org/HowToRegisterNewDistributions
   for information how to add support for your own distribution."
+
+    export DISTRIBUTION
 }
 
 function base._addGeneratedFile
@@ -183,39 +202,41 @@ function base._addGeneratedFile
 function base.initFilesystem
 {
     test -z "$1" || _renameVserverCfg
-    { isDirectoryEmpty "$VDIR" && test  ! -e "$SETUP_CONFDIR"; } || colpanic $"\
+    { { test -n "$OPTION_SEMIFORCE" || isDirectoryEmpty "$VDIR" 1; } && test  ! -e "$SETUP_CONFDIR"; } || colpanic $"\
 vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
 exist already; please try to use '--force', or remove them manually."
 
     mkdir -p -m755 "$VDIR"
     $_SETATTR --~barrier "$VDIR"
+    $_SETATTR --barrier "$VDIR"/.. || colwarn $"\
+WARNING: could not set the barrier attribute on '$VDIR/..',
+         please set it manually."
     base._addGeneratedFile "$VDIR"
     
-    mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/{dev/pts,etc}
+    mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/etc
     base._addGeneratedFile "$SETUP_CONFDIR"
     
     ln -s "$VDIR"                     "$SETUP_CONFDIR/vdir"
     ln -s "$CACHEDIR/$VSERVERDIRNAME" "$SETUP_CONFDIR/cache"
 
-    local spec
-    while read spec; do
-       makeDevEntry "$VDIR"/dev $spec
-    done <$_DEV_FILE
+    populateDev
 
     mkdir -p "$VDIR"/proc
     findAndCopy "$VDIR"/etc/hosts         "$__CONFDIR"/.defaults/files/hosts "$__CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
                                          "$__DISTRIBDIR/$DISTRIBUTION"/files/hosts "$__DISTRIBDIR"/defaults/files/hosts ""
 
-    for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime resolv.conf; do
+    for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime; do
        findAndCopy "$VDIR"/etc/$i  "$__CONFDIR/.defaults/files/$i" "$__CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
     done
+    findAndCopy "$VDIR"/etc/resolv.conf "$__CONFDIR/.defaults/files/resolv.conf" \
+       "$__CONFDIR/.distributions/$DISTRIBUTION/files/resolv.conf" /etc/resolv.conf ""
 }
 
 function base._initVariables
 {
     _setRootDir
     _setCacheDir
-    _setVserverDir
+    _setVserverDirName
     _setVdir
 
     findFile _DEV_FILE      "$__CONFDIR/.distributions/$DISTRIBUTION/devs"      "$__DISTRIBDIR/$DISTRIBUTION/devs"     "$__DISTRIBDIR/defaults/devs"
@@ -224,11 +245,25 @@ function base._initVariables
     findFile BUILD_INITPOST "$__CONFDIR/.distributions/$DISTRIBUTION/initpost"  "$__DISTRIBDIR/$DISTRIBUTION/initpost" ""
 }
 
+declare -a __BASE_CLEANUP
+function base.pushCleanup
+{
+    __BASE_CLEANUP=( "${__BASE_CLEANUP[@]}" "$*" )
+}
+
+function base.popCleanup
+{
+    unset __BASE_CLEANUP[$((${#__BASE_CLEANUP[@]} - 1))]
+}
+
 function base.__cleanup
 {
-    test -z "$OPTION_KEEP"    || return 0
-    test -z "$__BASE_SUCCESS" || return 0
+    for cmd in "${__BASE_CLEANUP[@]}"; do
+       $cmd
+    done
     
+    test -z "$__BASE_SUCCESS" || return 0
+    test -z "$OPTION_KEEP"    || return 0
     rm -rf "${__BASE_GENERATED_FILES[@]}"
 }
 
@@ -246,3 +281,23 @@ function base.setSuccess
 {
     __BASE_SUCCESS=1
 }
+
+function startSleepingGuest
+{
+    local guest="$1"
+    local timeout="${2:-15}"
+    $_VSERVER "$guest" start --rescue --rescue-init bash -c "
+       exec  > /dev/null
+       exec 2> /dev/null
+       trap 'kill -s 9 -- -1; exit 0' INT
+       sleep $timeout
+       kill -s 15 -- -1
+       sleep 1
+       kill -s 9 -- -1"
+}
+
+function stopSleepingGuest
+{
+    local guest="$1"
+    $_VSERVER "$guest" stop --rescue-init
+}