gentoo: use /var/run for new /run compatibility
[util-vserver.git] / scripts / vserver
index db03034..c9f9f24 100755 (executable)
@@ -31,21 +31,22 @@ test -e "$UTIL_VSERVER_VARS" || {
 function showHelp()
 {
     echo \
-$"Usage: $(basename $0) [-s|--sync] [-v|--verbose] [--silent]
+$"Usage: $(basename $0) [-s|--sync] [-v|--verbose] [--silent] [--debug]
              [--] <vserver> <command> <args>*
 
 <vserver> is the name of a vserver.
 
 Possible commands are:
-    start [--rescue [--rescue-init]] [<rescue-cmd> <args>*]
+    start [--rescue [--rescue-init] <rescue-cmd> <args>*]]
                 ... starts the specified vserver
-    stop        ... stops the specified vserver
+    stop [--rescue-init]
+                ... stops the specified vserver
     restart     ... restarts the specified vserver; this is the subsequent
                     execution of a synchronized 'stop' and a 'start'
     condrestart ... restarts the vserver when it is running already
-    suexec <user> <shell-command> <args*>
+    suexec <user> <command> <args*>
                 ... executes a command as the specified user in the vserver
-    exec <shell-command> <args*>
+    exec <command> <args*>
                 ... executes a command as root in the vserver
     enter       ... executes the configured shell in the vserver
     chkconfig <chkconfig-options*>
@@ -56,11 +57,12 @@ Possible commands are:
                     the vserver, and succeeds iff the vserver is running
 
     build <buildopts>*
-                ... builds a new vserver from scratch
+                ... builds a new vserver from scratch, see
+                    vserver ... build --help for details
     delete      ... remove a vserver
 
-    unify [-R]
-               ... (de)unify vserver
+    unify [-R] <vunify-opts>*
+               ... (de)unify the vserver with its reference vserver(s).
                
     pkg install <pkg>
                ... installs package(s) in the vserver
@@ -77,8 +79,8 @@ Possible commands are:
                    while 'internalize' means that data and programs from the
                    vserver will be used.
 
-    unify <vunify-opts>*
-                ... unify the vserver with its reference vserver(s).
+    hashify
+                ... hashify the guest.
                    
 
 Please report bugs to $PACKAGE_BUGREPORT"
@@ -119,7 +121,7 @@ function msg()
 set +e
 
 OPTIONS_ORIG=( "$@" )
-tmp=$(getopt -o +sv --long nonamespace,--nonamespace,--insecure,defaulttty,help,debug,version,sync,verbose,silent -n "$0" -- "$@") || exit 1
+tmp=$(getopt -o +sv --long nonamespace,--nonamespace,--insecure,defaulttty,help,debug,strace,debug-sysv,version,sync,verbose,silent -n "$0" -- "$@") || exit 1
 eval set -- "$tmp"
 
 OPTION_FORCE_SYNC=
@@ -129,6 +131,8 @@ OPTION_DEBUG=
 OPTION_NONAMESPACE=
 OPTION_INSECURE=
 OPTION_DEFAULTTTY=
+OPTION_STRACE=
+OPTION_DEBUG_SYSV=
 
 while true; do
     case "$1" in
@@ -141,6 +145,8 @@ while true; do
        (----nonamespace)OPTION_NONAMESPACE=$1;;
        (--defaulttty)  OPTION_DEFAULTTTY=$1;;
        (----insecure)  OPTION_INSECURE=1;;
+       (--strace)      OPTION_STRACE=1;;
+       (--debug-sysv*) OPTION_DEBUG_SYSV=$1;;
        (--)            shift; break;;
        (*)             echo $"vserver: internal error; arg=='$1'" >&2; exit 1;;
     esac
@@ -159,25 +165,28 @@ test "$cmd" != build || { shift 2; exec $_VNAMESPACE --new -- \
 
 allow_legacy=
 
+vserver="${vserver%%/}"
 case "$vserver" in
-    (./*) VSERVER_DIR=`pwd`/$vserver;;
-    (/*)  VSERVER_DIR=$vserver;;
-    (*)          VSERVER_DIR=$__CONFDIR/$vserver
-         allow_legacy=1
-         ;;
+    (./*)   VSERVER_DIR="`pwd`/$vserver";;
+    (/*)    VSERVER_DIR="$vserver";;
+    (*)            # Check if this is a context id
+           context=$(vuname --get --xid "$vserver" 2>/dev/null)
+           if test -d "$context"; then
+               VSERVER_DIR="$context"
+           else
+               VSERVER_DIR="$__CONFDIR/$vserver"
+               do_legacy=
+               test ! -e "$VSERVER_DIR/legacy" || do_legacy=1
+               test -d "$VSERVER_DIR" -o ! -e "$__CONFDIR/$vserver.conf" || do_legacy=1
+
+               test -z "$do_legacy" || {
+                   echo $"WARNING: can not find configuration, assuming legacy method" >&2
+                   exec "$_VSERVER_LEGACY" "$@"
+               }
+           fi
+           ;;
 esac
 
-if test -n "$allow_legacy"; then
-    do_legacy=
-    test ! -e "$VSERVER_DIR/legacy" || do_legacy=1
-    test -d "$VSERVER_DIR" -o ! -e "$__CONFDIR/$vserver.conf" || do_legacy=1
-
-    test -z "$do_legacy" || {
-       echo $"WARNING: can not find configuration, assuming legacy method" >&2
-       exec "$_VSERVER_LEGACY" "$@"
-    }
-fi
-
 test -d "$VSERVER_DIR" || {
     echo $"\
 Can not find a vserver-setup at '$VSERVER_DIR/'.
@@ -190,22 +199,21 @@ Possible solutions:
     exit 5
 } >&2
 
-if test -e "$VSERVER_DIR"/name; then
-    read VSERVER_NAME <"$VSERVER_DIR"/name
-else
-    VSERVER_NAME=$(basename "$VSERVER_DIR")
-fi
+_setVserverName
 
 # Create a new namespace when starting the guest
-test "$2" != start -o -n "$OPTION_NONAMESPACE" || isAvoidNamespace "$VSERVER_DIR" || \
+test "$cmd" != start -o -n "$OPTION_NONAMESPACE" || isAvoidNamespace "$VSERVER_DIR" || \
     exec $_VNAMESPACE --new -- $_VSERVER ----nonamespace "${OPTIONS_ORIG[@]}"
 
 # Enter the namespace early so we can test for files inside the guest
-test "$2" != enter -a "$2" != stop || \
+test "$cmd" != enter -a "$cmd" != stop || \
     test -n "$OPTION_NONAMESPACE" || isAvoidNamespace "$VSERVER_DIR" || \
     ! isVserverRunning "$VSERVER_DIR" || \
     exec $_VNAMESPACE --enter "$VSERVER_DIR" -- $_VSERVER ----nonamespace "${OPTIONS_ORIG[@]}"
 
+set_init_cwd
+cd /
+
 . $__PKGLIBDIR/vserver.functions
 case "$2" in
     (start|stop|delete)