added $_VSERVER_INFO
[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 [--nodeps] <start-vservers-opts>*
41                 ... starts the specified vserver
42     stop [--nodeps] <start-vservers-opts>*
43                 ... stops the specified vserver
44     restart     ... restarts the specified vserver; this is the subsequent
45                     execution of a synchronized 'stop' and a 'start'
46     condrestart ... restarts the vserver when it is running already
47     suexec <user> <shell-command> <args*>
48                 ... executes a command as the specified user in the vserver
49     exec <shell-command> <args*>
50                 ... executes a command as root in the vserver
51     enter       ... executes the configured shell in the vserver
52     chkconfig <chkconfig-options*>
53                 ... modifies the init-system; currently, only Red Hat's
54                     chkconfig is supported
55     running     ... succeeds iff the vserver is running
56     status      ... gives out some human readable status information about
57                     the vserver, and succeeds iff the vserver is running
58
59     build <buildopts>*
60                 ... builds a new vserver from scratch
61
62     apt-get,apt-config,apt-cache <apt-opts>*
63                 ... execute the apt-* command for the given vserver
64     rpm <rpm-opts>*
65                 ... execute the rpm command for the given vserver
66
67     pkgmgmt externalize|internalize [-y]
68                 ... externalize or internalize the package-management for the
69                     given vserver. 'Externalize' means that package metadata
70                     and management tools (apt-get,rpm) are living in the host,
71                     while 'internalize' means that data and programs from the
72                     vserver will be used.
73
74     unify <vunify-opts>*
75                 ... unify the vserver with its reference vserver(s).
76                     
77
78 Please report bugs to $PACKAGE_BUGREPORT"
79     exit 0
80 }
81
82 function showVersion()
83 {
84     echo \
85 $"vserver $PACKAGE_VERSION -- manages the state of vservers
86 This program is part of $PACKAGE_STRING
87
88 Copyright (C) 2003 Enrico Scholz
89 This program is free software; you may redistribute it under the terms of
90 the GNU General Public License.  This program has absolutely no warranty."
91     exit 0
92 }
93
94
95 ### main starts here
96
97 set +e
98
99 tmp=$(getopt -o +sv --long help,version,sync,verbose -n "$0" -- "$@") || exit 1
100 eval set -- "$tmp"
101
102 OPTION_FORCE_SYNC=
103 OPTION_VERBOSE=
104
105 while true; do
106     case "$1" in
107         --help)         showHelp $0 ;;
108         --version)      showVersion ;;
109         -v|--verbose)   OPTION_VERBOSE=1;    shift;;
110         -s|--sync)      OPTION_FORCE_SYNC=1; shift;;
111         --)             shift; break;;
112         *)              echo $"vserver: internal error; arg=='$1'"; exit 1;;
113     esac
114 done
115
116
117 vserver=$1
118 cmd=$2
119
120 test "$cmd" != build || { shift 2; exec "$_VSERVER_BUILD" -n "$vserver" "$@"; }
121
122
123 allow_legacy=
124
125 case "$vserver" in
126     ./*) VSERVER_DIR=`pwd`/$vserver;;
127     /*)  VSERVER_DIR=$vserver;;
128     *)   VSERVER_DIR=$CONFDIR/$vserver
129          allow_legacy=1
130          ;;
131 esac
132
133 if test "$allow_legacy"; then
134     do_legacy=
135     test ! -e "$VSERVER_DIR/legacy" || do_legacy=1
136     test -d "$VSERVER_DIR" -o ! -e "$CONFDIR/$vserver.conf" || do_legacy=1
137
138     test -z "$do_legacy" || {
139         echo $"WARNING: can not find configuration, assuming legacy method"
140         exec "$_VSERVER_LEGACY" "$@"
141     }
142 fi
143
144 test -d "$VSERVER_DIR" || {
145     echo "Can not find vserver-setup"
146     exit 1
147 }
148
149 if test -e "$VSERVER_DIR"/name; then
150     read VSERVER_NAME <"$VSERVER_DIR"/name
151 else
152     VSERVER_NAME=$(basename "$VSERVER_DIR")
153 fi
154
155 . $PKGLIBDIR/vserver.functions
156 case "$2" in
157     start|stop|suexec)
158         shift 2
159         . $PKGLIBDIR/vserver.$cmd
160         ;;
161     restart)
162         "$0" --sync "$1" stop
163         exec "$0" "$1" start
164         ;;
165     condrestart)
166         test ! isVserverRunning "$VSERVER_DIR" || exec "$0" "$1" restart
167         ;;
168     exec)
169         shift 2
170         exec "$0" "$vserver" suexec root "$@"
171         ;;
172     chkconfig)
173         shift 2
174         exec "$0" "$vserver" suexec root chkconfig "$@"
175         ;;
176     enter)
177         getEnterShell "$VSERVER_DIR"
178         exec "$0" "$1" suexec root "${ENTER_SHELL[@]}"
179         ;;
180     running)
181         isVserverRunning "$VSERVER_DIR"
182         ;;
183
184     apt-get|apt-config|apt-cache)
185         export _APT_GET=$2
186         shift 2
187         exec $_VAPT_GET -- "$@"
188         ;;
189     rpm)
190         exec $_VRPM -- "$@"
191         ;;
192         
193     status)
194         if getVserverStatus "$VSERVER_DIR" ctx procnum; then
195             echo $"Vserver '$1' is running at context '$ctx'"
196
197             if test "$2" = status; then
198                 echo    $"Number of processes:" $procnum
199                 echo -n $"Uptime:              "
200                 "$_FILETIME" "$VSERVER_DIR/run"
201             fi
202             exit 0
203         else
204             echo $"Vserver '$1' is not running"
205             exit 1
206         fi
207         ;;
208     *)
209         echo $"Usage: $0 {start|stop|suexec|restart|condrestart|exec|enter|chkconfig|running|status}"
210         exit 2
211         ;;
212 esac