allow to configure fstab-file
[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:=/usr/lib/util-vserver/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     unify [-R]
63                 ... (de)unify vserver
64                 
65     pkg install <pkg>
66                 ... installs package(s) in the vserver
67                 
68     apt-get,apt-config,apt-cache <apt-opts>*
69                 ... execute the apt-* command for the given vserver
70     rpm <rpm-opts>*
71                 ... execute the rpm command for the given vserver
72
73     pkgmgmt externalize|internalize [-y]
74                 ... externalize or internalize the package-management for the
75                     given vserver. 'Externalize' means that package metadata
76                     and management tools (apt-get,rpm) are living in the host,
77                     while 'internalize' means that data and programs from the
78                     vserver will be used.
79
80     unify <vunify-opts>*
81                 ... unify the vserver with its reference vserver(s).
82                     
83
84 Please report bugs to $PACKAGE_BUGREPORT"
85     exit 0
86 }
87
88 function showVersion()
89 {
90     echo \
91 $"vserver $PACKAGE_VERSION -- manages the state of vservers
92 This program is part of $PACKAGE_STRING
93
94 Copyright (C) 2003 Enrico Scholz
95 This program is free software; you may redistribute it under the terms of
96 the GNU General Public License.  This program has absolutely no warranty."
97     exit 0
98 }
99
100
101 ### main starts here
102
103 set +e
104
105 tmp=$(getopt -o +sv --long help,debug,version,sync,verbose -n "$0" -- "$@") || exit 1
106 eval set -- "$tmp"
107
108 OPTION_FORCE_SYNC=
109 OPTION_VERBOSE=
110
111 while true; do
112     case "$1" in
113         --help)         showHelp $0 ;;
114         --version)      showVersion ;;
115         --debug)        set -x;              shift;;
116         -v|--verbose)   OPTION_VERBOSE=1;    shift;;
117         -s|--sync)      OPTION_FORCE_SYNC=1; shift;;
118         --)             shift; break;;
119         *)              echo $"vserver: internal error; arg=='$1'"; exit 1;;
120     esac
121 done
122
123
124 vserver=$1
125 cmd=$2
126
127 test "$cmd" != build || { shift 2; exec "$_VSERVER_BUILD" -n "$vserver" "$@"; }
128
129
130 allow_legacy=
131
132 case "$vserver" in
133     ./*) VSERVER_DIR=`pwd`/$vserver;;
134     /*)  VSERVER_DIR=$vserver;;
135     *)   VSERVER_DIR=$CONFDIR/$vserver
136          allow_legacy=1
137          ;;
138 esac
139
140 if test "$allow_legacy"; then
141     do_legacy=
142     test ! -e "$VSERVER_DIR/legacy" || do_legacy=1
143     test -d "$VSERVER_DIR" -o ! -e "$CONFDIR/$vserver.conf" || do_legacy=1
144
145     test -z "$do_legacy" || {
146         echo $"WARNING: can not find configuration, assuming legacy method"
147         exec "$_VSERVER_LEGACY" "$@"
148     }
149 fi
150
151 test -d "$VSERVER_DIR" || {
152     echo "Can not find vserver-setup"
153     exit 1
154 }
155
156 if test -e "$VSERVER_DIR"/name; then
157     read VSERVER_NAME <"$VSERVER_DIR"/name
158 else
159     VSERVER_NAME=$(basename "$VSERVER_DIR")
160 fi
161
162 . $PKGLIBDIR/vserver.functions
163 case "$2" in
164     start|stop|suexec)
165         shift 2
166         . $PKGLIBDIR/vserver.$cmd
167         ;;
168     restart)
169         "$0" --sync "$1" stop
170         exec "$0" "$1" start
171         ;;
172     condrestart)
173         test ! isVserverRunning "$VSERVER_DIR" || exec "$0" "$1" restart
174         ;;
175     exec)
176         shift 2
177         exec "$0" "$vserver" suexec root "$@"
178         ;;
179     chkconfig)
180         shift 2
181         exec "$0" "$vserver" suexec root chkconfig "$@"
182         ;;
183     enter)
184         getEnterShell "$VSERVER_DIR"
185         exec "$0" "$1" suexec root "${ENTER_SHELL[@]}"
186         ;;
187     running)
188         isVserverRunning "$VSERVER_DIR"
189         ;;
190
191     unify)
192         shift 2
193         exec $_VUNIFY "$@" "$vserver"
194         ;;
195         
196     pkg)
197         shift 2
198         exec $_VPKG "$vserver" "$@"
199         ;;
200
201     apt-get|apt-config|apt-cache)
202         export _APT_GET=$2
203         shift 2
204         exec $_VAPT_GET -- "$@"
205         ;;
206     rpm)
207         exec $_VRPM -- "$@"
208         ;;
209         
210     status)
211         if getVserverStatus "$VSERVER_DIR" ctx procnum; then
212             echo $"Vserver '$1' is running at context '$ctx'"
213
214             if test "$2" = status; then
215                 echo    $"Number of processes:" $procnum
216                 echo -n $"Uptime:              "
217                 "$_FILETIME" "$VSERVER_DIR/run"
218             fi
219             exit 0
220         else
221             echo $"Vserver '$1' is not running"
222             exit 1
223         fi
224         ;;
225     *)
226         echo $"Usage: $0 {start|stop|suexec|restart|condrestart|exec|enter|chkconfig|running|status}"
227         exit 2
228         ;;
229 esac