b253e867a87d57add3c528449d25caf14a6ad30c
[util-vserver.git] / util-vserver / scripts / vserver
1 #! /bin/bash
2 # $Id$
3
4 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 #  
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
9 #  
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #  
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 # set -e
20
21 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
22 test -e "$UTIL_VSERVER_VARS" || {
23     echo "Can not find util-vserver installation; aborting..."
24     exit 1
25 }
26 . "$UTIL_VSERVER_VARS"
27 . "$_LIB_FUNCTIONS"
28
29 ### Some local functions
30
31 function showHelp()
32 {
33     echo \
34 $"Usage: $(basename $0) [-s|--sync] [-v|--verbose]
35              [--] <vserver> <command> <args>*
36
37 <vserver> is the name of a vserver.
38
39 Possible commands are:
40     start       ... starts the specified vserver
41     stop        ... stops the specified vserver
42     restart     ... restarts the specified vserver; this is the subsequent
43                     execution of a synchronized 'stop' and a 'start'
44     condrestart ... restarts the vserver when it is running already
45     suexec <user> <shell-command> <args*>
46                 ... executes a command as the specified user in the vserver
47     exec <shell-command> <args*>
48                 ... executes a command as root in the vserver
49     enter       ... executes the configured shell in the vserver
50     chkconfig <chkconfig-options*>
51                 ... modifies the init-system; currently, only Red Hat's
52                     chkconfig is supported
53     running     ... succeeds iff the vserver is running
54     status      ... gives out some human readable status information about
55                     the vserver, and succeeds iff the vserver is running
56
57     build <buildopts>*
58                 ... builds a new vserver from scratch
59
60 Please report bugs to $PACKAGE_BUGREPORT"
61     exit 0
62 }
63
64 function showVersion()
65 {
66     echo \
67 $"vserver $PACKAGE_VERSION -- manages the state of vservers
68 This program is part of $PACKAGE_STRING
69
70 Copyright (C) 2003 Enrico Scholz
71 This program is free software; you may redistribute it under the terms of
72 the GNU General Public License.  This program has absolutely no warranty."
73     exit 0
74 }
75
76
77 ### main starts here
78
79 set +e
80
81 tmp=$(getopt -o +sv --long help,version,sync,verbose -n "$0" -- "$@") || exit 1
82 eval set -- "$tmp"
83
84 OPTION_FORCE_SYNC=
85 OPTION_VERBOSE=
86
87 while true; do
88     case "$1" in
89         --help)         showHelp $0 ;;
90         --version)      showVersion ;;
91         -v|--verbose)   OPTION_VERBOSE=1;    shift;;
92         -s|--sync)      OPTION_FORCE_SYNC=1; shift;;
93         --)             shift; break;;
94         *)              echo $"vserver: internal error; arg=='$1'"; exit 1;;
95     esac
96 done
97
98
99 vserver=$1
100 cmd=$2
101
102 test "$cmd" != build || { shift 2; exec "$_VSERVER_BUILD" -n "$vserver" "$@"; }
103
104
105 allow_legacy=
106
107 case "$vserver" in
108     ./*) VSERVER_DIR=`pwd`/$vserver;;
109     /*)  VSERVER_DIR=$vserver;;
110     *)   VSERVER_DIR=$CONFDIR/$vserver
111          allow_legacy=1
112          ;;
113 esac
114
115 if test "$allow_legacy"; then
116     do_legacy=
117     test ! -e "$VSERVER_DIR/legacy" || do_legacy=1
118     test -d "$VSERVER_DIR" -o ! -e "$CONFDIR/$vserver.conf" || do_legacy=1
119
120     test -z "$do_legacy" || {
121         echo $"WARNING: can not find configuration, assuming legacy method"
122         exec "$_VSERVER_LEGACY" "$@"
123     }
124 fi
125
126 test -d "$VSERVER_DIR" || {
127     echo "Can not find vserver-setup"
128     exit 1
129 }
130
131 if test -e "$VSERVER_DIR"/name; then
132     read VSERVER_NAME <"$VSERVER_DIR"/name
133 else
134     VSERVER_NAME=$(basename "$VSERVER_DIR")
135 fi
136
137 . $PKGLIBDIR/vserver.functions
138 case "$2" in
139     start|stop|suexec)
140         shift 2
141         . $PKGLIBDIR/vserver.$cmd
142         ;;
143     restart)
144         "$0" --sync "$1" stop
145         exec "$0" "$1" start
146         ;;
147     condrestart)
148         test ! isVserverRunning "$VSERVER_DIR" || exec "$0" "$1" restart
149         ;;
150     exec)
151         shift 2
152         exec "$0" "$vserver" suexec root "$@"
153         ;;
154     chkconfig)
155         shift 2
156         exec "$0" "$vserver" suexec root chkconfig "$@"
157         ;;
158     enter)
159         getEnterShell "$VSERVER_DIR"
160         exec "$0" "$1" suexec root "${ENTER_SHELL[@]}"
161         ;;
162     running)
163         isVserverRunning "$VSERVER_DIR"
164         ;;
165     status)
166         if getVserverStatus "$VSERVER_DIR" ctx procnum; then
167             echo $"Vserver '$1' is running at context '$ctx'"
168
169             if test "$2" = status; then
170                 echo    $"Number of processes:" $procnum
171                 echo -n $"Uptime:              "
172                 "$_FILETIME" "$VSERVER_DIR/run"
173             fi
174             exit 0
175         else
176             echo $"Vserver '$1' is not running"
177             exit 1
178         fi
179         ;;
180     *)
181         echo $"Usage: $0 {start|stop|suexec|restart|condrestart|exec|enter|chkconfig|running|status}"
182         exit 2
183         ;;
184 esac