initial checkin
[util-vserver.git] / util-vserver / scripts / vserver-legacy
1 #!/bin/sh
2
3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 # based on vserver by Jacques Gelinas
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; either version 2, or (at your option)
9 # any later version.
10 #  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #  
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 # This is a script to control a virtual server
21
22 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
23 test -e "$UTIL_VSERVER_VARS" || {
24     echo "Can not find util-vserver installation; aborting..."
25     exit 1
26 }
27 . "$UTIL_VSERVER_VARS"
28
29 USR_SBIN=$SBINDIR
30 USR_LIB_VSERVER=$PKGLIBDIR
31
32 VSERVERKILLALL_CMD=$USR_LIB_VSERVER/vserverkillall
33 DEFAULTPATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
34 vserver_mknod(){
35         mknod $1 $2 $3 $4
36         chmod $5 $1
37 }
38
39 mountproc()
40 {
41         mkdir -p $1/proc $1/dev/pts
42         if [ ! -d $1/proc/1 ] ; then
43                 mount -t proc none $1/proc
44                 mount -t devpts none $1/dev/pts
45         fi
46 }
47 umountproc()
48 {
49         umount $1/proc 2>/dev/null
50         umount $1/dev/pts 2>/dev/null
51 }
52
53 # Check that the vservers parent directory has permission 000
54 # This is the key to avoid chroot escape
55 testperm()
56 {
57         return
58         PERM=`$USR_LIB_VSERVER/showperm /vservers/$1/..`
59         if [ "$PERM" != 000 ] ; then
60                 echo
61                 echo "**********************************************************"
62                 echo /vservers/$1/.. has insecure permissions.
63                 echo A vserver administrator may be able to visit the root server.
64                 echo To fix this, do
65                 echo "  " chmod 000 /vservers/$1/..
66                 echo do it anytime you want, even if vservers are running.
67                 echo "**********************************************************"
68                 echo
69         fi
70 }
71 # Set the IP alias needed by a vserver
72 ifconfig_iproot()
73 {
74         if [ "$NODEV" = "" -a "$IPROOT" != "" -a "$IPROOT" != "0.0.0.0" -a "$IPROOT" != "ALL" ] ;then
75                 # A vserver may have more than one IP
76                 # The first alias is dev:vserver
77                 # and the other are dev:vserver1,2,3 and so on
78                 # An IP may hold the device. The following is valid
79                 #       IPROOT="1.2.4.5 eth1:1.2.3.5"
80                 #       IPROOTDEV=eth0
81                 # The first IP 1.2.3.4 will go on eth0 and the other on eth1
82                 # VLAN devices are also supported (eth0.231 for vlan 231)
83                 SUFFIX=
84                 for oneip in $IPROOT
85                 do
86                         IPDEV=$IPROOTDEV
87                         MASK=$IPROOTMASK
88                         BCAST=$IPROOTBCAST
89                         # Split the device and IP if available
90                         case $oneip in
91                         *:*)
92                                 eval `echo $oneip | tr : ' ' | (read dev ip; echo oneip=$ip; echo IPDEV=$dev)`
93                                 ;;
94                         esac
95                         # Split the IP and the netmask if available
96                         case $oneip in
97                         */*)
98                                 eval `echo $oneip | tr / ' ' | (read ip msk; echo oneip=$ip; echo MASK=$msk)`
99                                 eval `$USR_LIB_VSERVER/ifspec "" "$oneip" "$MASK" "$BCAST"`
100                                 ;;
101                         esac
102                         if [ "$IPDEV" != "" ] ; then
103                                 case $IPDEV in
104                                 *.*)
105                                         if [ ! -f /proc/net/vlan/$IPDEV ] ; then
106                                                 /sbin/vconfig add `echo $IPDEV | tr . ' '`
107                                                 # Put a dummy IP
108                                                 /sbin/ifconfig $IPDEV 127.0.0.1
109                                         fi
110                                         ;;
111                                 esac
112                                 # Compute the default netmask, if missing
113                                 eval `$USR_LIB_VSERVER/ifspec $IPDEV "$oneip" "$MASK" "$BCAST"`
114                                 IPROOTMASK=$NETMASK
115                                 IPROOTBCAST=$BCAST
116                                 #echo /sbin/ifconfig $IPDEV:$1$SUFFIX $oneip netmask $IPROOTMASK broadcast $IPROOTBCAST
117                                 /sbin/ifconfig $IPDEV:$1$SUFFIX $oneip netmask $IPROOTMASK broadcast $IPROOTBCAST
118                         fi
119                         if [ "$SUFFIX" = "" ] ; then
120                                 SUFFIX=1
121                         else
122                                 SUFFIX=`expr $SUFFIX + 1`
123                         fi
124                 done
125         fi
126         if [ "$IPROOTBCAST" = "" ] ; then
127                 IPROOTBCAST=255.255.255.255
128         fi
129 }
130 ifconfig_iproot_off()
131 {
132         if [ "$NODEV" = "" -a "$IPROOT" != "" -a "$IPROOT" != "0.0.0.0" -a "$IPROOT" != "ALL"  -a "$IPROOTDEV" != "" ] ;then
133                 SUFFIX=
134                 for oneip in $IPROOT
135                 do
136                         IPDEV=$IPROOTDEV
137                         # Split the device and IP if available
138                         case $oneip in
139                         *:*)
140                                 eval `echo $oneip | tr : ' ' | (read dev ip; echo IPDEV=$dev)`
141                                 ;;
142                         esac
143                         /sbin/ifconfig $IPDEV:$1$SUFFIX down 2>/dev/null
144                         if [ "$SUFFIX" = "" ] ; then
145                                 SUFFIX=1
146                         else
147                                 SUFFIX=`expr $SUFFIX + 1`
148                         fi
149                 done
150         fi
151 }
152 # Split an IPROOT definition, trash the devices and
153 # compose a set of --ip option for chbind
154 setipopt(){
155         RET=
156         IPS="$*"
157         if [ "$IPS" = "" ] ; then
158                 IPS=0.0.0.0
159         fi
160         if [ "$1" = "ALL" ] ; then
161                 IPS=`$USR_LIB_VSERVER/listdevip`
162         fi
163         for oneip in $IPS
164         do
165                 # Split the device and IP if available
166                 case $oneip in
167                 *:*)
168                         eval `echo $oneip | tr : ' ' | (read dev ip; echo oneip=$ip)`
169                         ;;
170                 esac
171                 #case $oneip in
172                 #*/*)
173                 #       eval `echo $oneip | tr / ' ' | (read ip msk; echo oneip=$ip)`
174                 #       ;;
175                 #esac
176                 echo --ip $oneip
177         done
178 }
179
180 # Extract the initial runlevel from the vserver inittab
181 get_initdefault()
182 {
183         INITDEFAULT=`grep :initdefault /vservers/$1/etc/inittab | sed 's/:/ /g' | ( read a level b; echo $level)`
184 }
185
186 # Read the vserver configuration file, reusing the PROFILE value
187 # found in /var/run/vservers
188 readlastconf()
189 {
190         if [ -f /var/run/vservers/$1.ctx ] ; then
191                 . /var/run/vservers/$1.ctx
192                 if [ "$S_PROFILE" != "" ] ; then
193                         export PROFILE=$S_PROFILE
194                 fi
195         fi
196         export PROFILE
197         . /etc/vservers/$1.conf
198 }
199 usage()
200 {
201         echo vserver [ options ] server-name command ...
202         echo
203         echo server-name is a directory in /vservers
204         echo
205         echo The commands are:
206         echo " build   : Create a virtual server by copying the packages"
207         echo "           of the root server"
208         echo " enter   : Enter in the virtual server context and starts a shell"
209         echo "           Same as \"vserver name exec /bin/sh\""
210         echo " exec    : Exec a command in the virtual server context"
211         echo " suexec  : Exec a command in the virtual server context uid"
212         echo " service : Control a service inside a vserver"
213         echo "           vserver name service service-name start/stop/restart/status"
214         echo " start   : Starts the various services in the vserver, runlevel 3"
215         echo " stop    : Ends all services and kills the remaining processes"
216         echo " running : Tells if a virtual server is running"
217         echo "           It returns proper exit code, so you can use it as a test"
218         echo " status  : Tells some information about a vserver"
219         echo " chkconfig : It turns a server on or off in a vserver"
220         echo
221         echo "--nodev  : Do not configure the IP aliases of the vserver"
222         echo "           Useful to enter a vserver without enabling its network"
223         echo "           and avoiding conflicts with another copy of this vserver"
224         echo "           running elsewhere"
225         echo "--silent : No informative messages about vserver context and IP numbers"
226         echo "           Useful when you want to redirect the output"
227 }
228
229 calculateCaps()
230 {
231     local f
232     for f in "$@"; do
233         case $f in
234             !CAP_SYS_CHROOT)
235                 CHROOTOPT=--nochroot
236                 ;;
237             *)
238                 CAPS="$CAPS --cap $f"
239                 ;;
240         esac
241     done
242 }
243
244 SILENT=
245 NODEV=
246 while true
247 do
248         if [ "$1" = "--silent" ] ; then
249                 SILENT=--silent
250                 shift
251         elif [ "$1" = "--nodev" ] ; then
252                 NODEV=--nodev
253                 shift
254         else
255                 break
256         fi
257 done
258 # Setup the default ulimit for a vserver
259 setdefulimit(){
260         # File handle are limited to half of the current system limit
261         # Virtual memory is limited to the ram size
262         NFILE=`cat /proc/sys/fs/file-max`
263         NFILE=`expr $NFILE / 2`
264         VMEM=`cat /proc/meminfo  | grep MemTotal | (read a b c; echo $b)`
265         # Disabled for now, we need a different to set the security
266         # context limit than fiddling with ulimit
267         #ulimit -H -n $NFILE -v $VMEM
268 }
269 if [ $# -lt 2 ] ; then
270         usage
271 elif [ "$2" = "build" ] ; then
272         # Either the directory does not exist or is empty
273         NBSUB=`ls /vservers/$1 2>/dev/null | grep -v lost+found | wc -l` 
274         NBSUB=`expr $NBSUB`
275         if [ "$NBSUB" != 0 ] ; then
276                 echo Virtual server /vservers/$1 already exist
277         else
278                 if [ ! -d /vservers ] ; then
279                         mkdir /vservers || exit 1
280                         chmod 000 /vservers
281                         echo Directory /vservers was created with permissions 000
282                 fi
283                 mkdir -p /vservers/$1 || exit 1
284                 chmod 755 /vservers/$1
285                 mkdir -p /vservers/$1/{etc/rc.d/init.d,sbin,var/run,var/log}
286                 cd /vservers/$1 || exit 1
287                 rm -fr lib/modules/*
288                 rm -f var/spool/mail/*
289                 rm -f `find var/run -type f`
290                 rm -f `find var/log -type f`
291                 touch var/log/wtmp
292                 rm -f var/lock/subsys/*
293                 rm -f etc/cron.d/kmod
294                 mkdir proc tmp home root boot
295                 test -f /root/.bashrc && cp -a /root/.bashrc root/.
296                 test -f /root/.bash_profile && cp -a /root/.bash_profile root/.
297                 chmod 1777 tmp
298                 chmod 750 root
299                 # Create a minimal dev so the virtual server can't grab
300                 # more privileges
301                 mkdir dev dev/pts
302                 vserver_mknod dev/null c 1 3 666
303                 vserver_mknod dev/zero c 1 5 666
304                 vserver_mknod dev/full c 1 7 666
305                 vserver_mknod dev/random c 1 8 644
306                 vserver_mknod dev/urandom c 1 9 644
307                 vserver_mknod dev/tty c 5 0 666
308                 vserver_mknod dev/ptmx c 5 2 666
309                 touch dev/hdv1
310                 # Turn off some service useless on a vserver
311                 #               vserver_turnoff apmd network autofs dhcpd gpm ipchains iptables \
312                 #                       irda isdn keytable kudzu linuxconf-setup netfs nfs nfslock \
313                 #                       pcmcia portmap pppoe random rawdevices rhnsd rstatd ruserd \
314                 #                       rwalld rwhod sendmail smb snmpd v_httpd h_xinetd v_sshd vservers \
315                 #                       xfs ypbind xinetd
316                 (
317                         cd etc/init.d 2>/dev/null || cd etc/rc.d/init.d
318                         for serv in *
319                         do
320                                 case $serv in
321                                 *.bak|*~|functions|killall|halt|single)
322                                         ;;
323                                 *)
324                                         #$USR_LIB_VSERVER/capchroot /vservers/$1 /sbin/chkconfig --level 2345 $serv off
325                                         $0 --silent $1 chkconfig --level 2345 $serv off
326                                         ;;
327                                 esac
328                         done
329                 )
330                 rm -f etc/rc.d/rc6.d/S*reboot
331                 # Create a dummy /etc/fstab and /etc/mtab to please
332                 # df and linuxconf. We use hdv1, which does not exist
333                 # to remind the admin that it is not the real drive
334                 echo /dev/hdv1 / ext2 defaults 1 1 >etc/fstab
335                 echo /dev/hdv1 / ext2 rw 0 0 >etc/mtab
336                 # Install the vreboot utility
337                 cp -a $USR_LIB_VSERVER/vreboot sbin/.
338                 ln -sf vreboot sbin/vhalt
339
340                 echo Directory /vservers/$1 has been populated
341                 if [ ! -d /etc/vservers ] ; then
342                         mkdir /etc/vservers
343                         chmod 600 /etc/vservers
344                         echo Directory /etc/vservers has been created
345                 fi
346                 if [ ! -f /etc/vservers/$1.conf ] ; then
347                         CONF=/etc/vservers/$1.conf
348                         cat >$CONF <<-EOF
349 if [ "$PROFILE" = "" ] ; then
350         PROFILE=prod
351 fi
352 # Select the IP number assigned to the virtual server
353 # This IP must be one IP of the server, either an interface
354 # or an IP alias
355 # A vserver may have more than one IP. Separate them with spaces.
356 # do not forget double quotes.
357 # Some examples:
358 # IPROOT="1.2.3.4 2.3.4.5"
359 # IPROOT="eth0:1.2.3.4 eth1:2.3.4.5"
360 # If the device is not specified, IPROOTDEV is used
361 case \$PROFILE in
362 prod)
363         IPROOT=1.2.3.4
364         # The netmask and broadcast are computed by default from IPROOTDEV
365         #IPROOTMASK=
366         #IPROOTBCAST=
367         # You can define on which device the IP alias will be done
368         # The IP alias will be set when the server is started and unset
369         # when the server is stopped
370         #IPROOTDEV=eth0
371         # You can set a different host name for the vserver
372         # If empty, the host name of the main server is used
373         S_HOSTNAME=
374         ;;
375 backup)
376         IPROOT=1.2.3.4
377         #IPROOTMASK=
378         #IPROOTBCAST=
379         #IPROOTDEV=eth0
380         S_HOSTNAME=
381         ;;
382 esac
383 # Uncomment the onboot line if you want to enable this
384 # virtual server at boot time
385 #ONBOOT=yes
386 # You can set a different NIS domain for the vserver
387 # If empty, the current on is kept
388 # Set it to "none" to have no NIS domain set
389 S_DOMAINNAME=
390 # You can set the priority level (nice) of all process in the vserver
391 # Even root won't be able to raise it
392 S_NICE=
393 # You can set various flags for the new security context
394 # lock: Prevent the vserver from setting new security context
395 # sched: Merge scheduler priority of all processes in the vserver
396 #        so that it acts a like a single one.
397 # nproc: Limit the number of processes in the vserver according to ulimit
398 #        (instead of a per user limit, this becomes a per vserver limit)
399 # private: No other process can join this security context. Even root
400 # Do not forget the quotes around the flags
401 S_FLAGS="lock nproc"
402 # You can set various ulimit flags and they will be inherited by the
403 # vserver. You enter here various command line argument of ulimit
404 # ULIMIT="-H -u 200"
405 # The example above, combined with the nproc S_FLAGS will limit the
406 # vserver to a maximum of 200 processes
407 ULIMIT="-H -u 1000"
408 # You can set various capabilities. By default, the vserver are run
409 # with a limited set, so you can let root run in a vserver and not
410 # worry about it. He can't take over the machine. In some cases
411 # you can to give a little more capabilities (such as CAP_NET_RAW)
412 # S_CAPS="CAP_NET_RAW"
413 S_CAPS=""
414 # Select an unused context (this is optional)
415 # The default is to allocate a free context on the fly
416 # In general you don't need to force a context
417 #S_CONTEXT=
418                         EOF
419                         echo $CONF has been created. Look at it\!
420                 fi
421         fi
422 elif [ ! -f /etc/vservers/$1.conf ] ; then
423         echo No configuration for this vserver: /etc/vservers/$1.conf
424         exit 1
425 elif [ ! -d /vservers/$1/. ] ; then
426         echo No directory for this vserver: /vservers/$1
427         exit 1
428 elif [ "$2" = "start" ] ; then
429         echo Starting the virtual server $1
430         testperm $1
431         if ! $0 $1 running
432         then
433                 test -x /etc/vservers/$1.sh && /etc/vservers/$1.sh pre-start $1
434                 IPROOT=
435                 IPROOTMASK=
436                 IPROOTBCAST=
437                 IPROOTDEV=
438                 S_NICE=
439                 S_FLAGS=
440                 . /etc/vservers/$1.conf
441                 export PROFILE
442                 ifconfig_iproot $1
443                 cd /vservers/$1 || exit 1
444
445                 if [ "$PROFILE" != "" ] ; then
446                         echo export PROFILE=$PROFILE >etc/PROFILE
447                 fi
448
449                 rm -f `find var/run -type f`
450                 touch var/run/utmp
451                 rm -f  var/lock/subsys/*
452                 mountproc /vservers/$1
453                 CTXOPT=
454                 HOSTOPT=
455                 DOMAINOPT=
456                 NICECMD=
457                 FLAGS=
458                 CAPS=
459                 get_initdefault $1
460                 STARTCMD="/etc/rc.d/rc $INITDEFAULT"
461                 if [ -x /vservers/$1/etc/init.d/rc ] ; then
462                         STARTCMD="/etc/init.d/rc $INITDEFAULT"
463                 fi
464
465                 DISCONNECT=
466                 FAKEINIT=
467                 for f in $S_FLAGS dummy
468                 do
469                         case $f in
470                         dummy)
471                                 ;;
472
473                         minit)
474                                 FAKEINIT=true
475                                 FLAGS="$FLAGS --flag fakeinit"
476                                 STARTCMD=/sbin/minit-start
477                                 DISCONNECT=--disconnect
478                                 ;;
479
480                         fakeinit)
481                                 FAKEINIT=true
482                                 FLAGS="$FLAGS --flag $f"
483                                 STARTCMD=/sbin/init
484                                 DISCONNECT=--disconnect
485                                 ;;
486                         *)
487                                 FLAGS="$FLAGS --flag $f"
488                                 ;;
489                         esac
490                 done
491                 if [ "$FAKEINIT" = "" ] ; then
492                         $USR_LIB_VSERVER/fakerunlevel $INITDEFAULT var/run/utmp
493                 fi
494
495                 calculateCaps $S_CAPS
496
497                 if [ "$S_CONTEXT" != "" ] ; then
498                         CTXOPT="--ctx $S_CONTEXT"
499                 fi
500                 if [ "$S_HOSTNAME" != "" ] ; then
501                         HOSTOPT="--hostname $S_HOSTNAME"
502                         export HOSTNAME=$S_HOSTNAME
503                 fi
504                 if [ "$S_DOMAINNAME" != "" ] ; then
505                         DOMAINOPT="--domainname $S_DOMAINNAME"
506                 fi
507                 if [ "$S_NICE" != "" ] ; then
508                         NICECMD="nice -$S_NICE"
509                 fi
510                 mkdir -p /var/run/vservers
511                 chmod 700 /var/run/vservers
512                 setdefulimit
513                 if [ "$ULIMIT" != "" ] ; then
514                         ulimit $ULIMIT
515                 fi
516                 #echo FLAGS=$FLAGS
517                 #echo CAPS=$CAPS
518                 # We switch to /vservers/$1 now, because after the
519                 # security context switch /vservers directory becomes a dead zone.
520                 cd /vservers/$1
521                 IPOPT=`setipopt $IPROOT`
522                 export PATH=$DEFAULTPATH
523                 $NICECMD $_CHBIND $SILENT $IPOPT --bcast $IPROOTBCAST \
524                         $_CHCONTEXT $SILENT $DISCONNECT $CAPS $FLAGS $CTXOPT $HOSTOPT $DOMAINOPT --secure \
525                         $_SAVE_S_CONTEXT /var/run/vservers/$1.ctx \
526                         $_CAPCHROOT $CHROOTOPT . $STARTCMD
527                 sleep 2
528                 test -x /etc/vservers/$1.sh && /etc/vservers/$1.sh post-start $1
529         fi
530 elif [ "$2" = "running" ] ; then
531         if [ ! -f /var/run/vservers/$1.ctx ] ; then
532                 echo Server $1 is not running
533                 exit 1
534         else
535                 . /var/run/vservers/$1.ctx
536                 NB=$($USR_SBIN/vps ax | awk '{print $2}' | grep \^$S_CONTEXT\$ | wc -l)
537                 #NB=`$_CHCONTEXT --silent --ctx $S_CONTEXT ps ax | wc -l`
538                 #NB=`eval expr $NB + 0`
539                 if [ "$NB" -gt 0 ] ; then
540                         echo Server $1 is running
541                         exit 0
542                 else
543                         echo Server $1 is not running
544                         exit 1
545                 fi
546         fi
547 elif [ "$2" = "status" ] ; then
548         if $0 $1 running
549         then
550                 . /var/run/vservers/$1.ctx
551                 NB=$($USR_SBIN/vps ax | awk '{print $2}' | grep \^$S_CONTEXT\$ | wc -l)
552                 echo $NB processes running
553                 echo Vserver uptime: `$USR_LIB_VSERVER/filetime /var/run/vservers/$1.ctx`
554         fi
555 elif [ "$2" = "stop" ] ; then
556         echo Stopping the virtual server $1
557         IPROOT=
558         IPROOTMASK=
559         IPROOTBCAST=
560         IPROOTDEV=
561         CAPS=
562         IS_MINIT=
563         readlastconf $1
564         if $0 $1 running
565         then
566                 test -x /etc/vservers/$1.sh && /etc/vservers/$1.sh pre-stop $1
567                 ifconfig_iproot $1
568                 cd /vservers/$1
569                 mountproc /vservers/$1
570                 # The fakeinit flag tell us how to turn off the server
571                 get_initdefault $1
572                 export PREVLEVEL=$INITDEFAULT
573                 STOPCMD="/etc/rc.d/rc 6"
574                 if [ -x /vservers/$1/etc/init.d/rc ] ; then
575                         STOPCMD="/etc/init.d/rc 6"
576                 fi
577                 for f in $S_FLAGS dummy
578                 do
579                         case $f in
580                         minit)
581                                 IS_MINIT=1
582                                 FLAGS="$FLAGS --flag fakeinit"
583                                 STOPCMD="/sbin/minit-stop"
584                                 ;;
585
586                         fakeinit)
587                                 FLAGS="$FLAGS --flag $f"
588                                 STOPCMD="/sbin/init 6"
589                                 ;;
590                         *)
591                                 ;;
592                         esac
593                 done
594
595                 calculateCaps $S_CAPS
596
597                 cd /vservers/$1
598                 IPOPT=`setipopt $IPROOT`
599                 export PATH=$DEFAULTPATH
600                 $_CHBIND $SILENT $IPOPT --bcast $IPROOTBCAST \
601                         $_CHCONTEXT $SILENT $CAPS --secure --ctx $S_CONTEXT \
602                         $_CAPCHROOT . $STOPCMD
603
604                 if test "$IS_MINIT"; then
605                     echo "Waiting for minit finish-signal"
606                     dd if=var/run/minit-stop of=/dev/zero bs=1 count=1 &>/dev/null
607                     sleep 1
608                 else
609                     echo sleeping 5 seconds
610                     sleep 5
611                 fi
612
613                 echo Killing all processes
614                 $_CHBIND --silent $IPOPT --bcast $IPROOTBCAST \
615                         $_CHCONTEXT $CAPS --secure --silent --ctx $S_CONTEXT \
616                         $VSERVERKILLALL_CMD
617         fi
618         # We umount anyway, because "enter" establish the mount
619         # but when you exit, the server is considered not running
620         umountproc /vservers/$1
621         cd /
622         test -x /etc/vservers/$1.sh && /etc/vservers/$1.sh post-stop $1
623         ifconfig_iproot_off $1
624 elif [ "$2" = "restart" ] ; then
625         if $0 $1 running
626         then
627                 $0 $1 stop
628                 $0 $1 start
629         fi
630 elif [ "$2" = "suexec" ] ; then
631         if [ -z "$3" ] ; then
632                 echo "Missing user!" >&2
633                 echo "vserver vserver-name suexec user command [ args ... ]" >&2
634                 exit 1
635         elif [ -z "$4" ] ; then
636                 echo "Missing command and arguments!" >&2
637                 echo "vserver vserver-name suexec user command [ args ... ]" >&2
638                 exit 1
639         else
640                 IPROOT=
641                 IPROOTMASK=
642                 IPROOTBCAST=
643                 IPROOTDEV=
644                 readlastconf $1
645                 . /etc/vservers/$1.conf
646                 cd /vservers/$1
647                 ifconfig_iproot $1
648                 mountproc /vservers/$1
649                 PS1="[\u@vserver:$1 \W]"
650                 export PS1
651                 VSERVER=$1
652                 USERID=$3
653                 shift; shift; shift
654                 CAPS=
655                 for f in $S_CAPS dummy
656                 do
657                         case $f in
658                         dummy)
659                                 ;;
660                         !CAP_SYS_CHROOT)
661                                 CHROOTOPT=--nochroot
662                                 ;;
663                         *)
664                                 CAPS="$CAPS --cap $f"
665                                 ;;
666                         esac
667                 done
668                 FLAGS=
669                 for f in $S_FLAGS dummy
670                 do
671                         case $f in
672                         minit)
673                                 FLAGS="$FLAGS --flag fakeinit"
674                                 ;;
675
676                         dummy)
677                                 ;;
678                         *)
679                                 FLAGS="$FLAGS --flag $f"
680                                 ;;
681                         esac
682                 done
683                 setdefulimit
684                 if [ "$ULIMIT" != "" ] ; then
685                         ulimit $ULIMIT
686                 fi
687                 if $0 $VSERVER running >/dev/null
688                 then
689                         . /var/run/vservers/$VSERVER.ctx
690                         cd /vservers/$VSERVER
691                         IPOPT=`setipopt $IPROOT`
692                         export PATH=$DEFAULTPATH
693                         $_CHBIND $SILENT $IPOPT --bcast $IPROOTBCAST \
694                                 $_CHCONTEXT $SILENT $FLAGS $CAPS --secure --ctx $S_CONTEXT \
695                                 $_CAPCHROOT --suid $USERID . "$@"
696                 else
697                         test -x /etc/vservers/$1.sh && /etc/vservers/$1.sh pre-start $1
698                         CTXOPT=
699                         HOSTOPT=
700                         DOMAINOPT=
701                         if [ "$S_CONTEXT" != "" ] ; then
702                                 CTXOPT="--ctx $S_CONTEXT"
703                         fi
704                         if [ "$S_HOSTNAME" != "" ] ; then
705                                 HOSTOPT="--hostname $S_HOSTNAME"
706                                 export HOSTNAME=$S_HOSTNAME
707                         fi
708                         if [ "$S_DOMAINNAME" != "" ] ; then
709                                 DOMAINOPT="--domainname $S_DOMAINNAME"
710                         fi
711                         mkdir -p /var/run/vservers
712                         cd /vservers/$VSERVER
713                         IPOPT=`setipopt $IPROOT`
714                         export PATH=$DEFAULTPATH
715                         $_CHBIND $SILENT $IPOPT --bcast $IPROOTBCAST \
716                                 $_CHCONTEXT $SILENT $FLAGS $CAPS --secure $CTXOPT $HOSTOPT $DOMAINOPT \
717                                 $_SAVE_S_CONTEXT /var/run/vservers/$VSERVER.ctx \
718                                 $_CAPCHROOT --suid $USERID $CHROOTOPT . "$@"
719                 fi
720         fi
721 elif [ "$2" = "exec" ] ; then
722         VSERV=$1
723         shift; shift
724         exec $0 $NODEV $SILENT $VSERV suexec root "$@"
725 elif [ "$2" = "enter" ] ; then
726         testperm $1
727         exec $0 $NODEV $SILENT $1 exec /bin/bash -login
728 elif [ "$2" = "service" ] ; then
729         VSERVER=$1
730         shift
731         shift
732         exec $0 $NODEV $SILENT $VSERVER exec /sbin/service "$@"
733 elif [ "$2" = "chkconfig" ] ; then
734         VSERVER=$1
735         shift
736         shift
737         if [ "$1" = "--level" ] ; then
738                 shift
739                 LEVELS=$1
740                 shift
741         fi
742         if [ $# != 2 -a ! -x /vservers/$VSERVER/sbin/chkconfig ] ; then
743                 echo Invalid argument, expected vserver name chkconfig [ --level nnn ] service on\|off
744         elif [ -x /vservers/$VSERVER/sbin/chkconfig ] ; then
745                 exec $0 --silent $VSERVER exec /sbin/chkconfig "$@"
746         elif [ -x /vservers/$VSERVER/usr/sbin/update-rc.d ] ; then
747                 if [ "$2" = "on" -o "$2" = "start" ] ; then
748                         $0 --silent $VSERVER exec /usr/sbin/update-rc.d -f $1 remove >/dev/null
749                         exec $0 --silent $VSERVER exec /usr/sbin/update-rc.d $1 start 80 2 3 4 5 . stop 20 0 1 6 . >/dev/null
750                 elif [ "$2" = "off" -o "$2" = "stop" ] ; then
751                         $0 --silent $VSERVER exec /usr/sbin/update-rc.d -f $1 remove >/dev/null
752                         exec $0 --silent $VSERVER exec /usr/sbin/update-rc.d $1 stop 20 0 1 2 3 4 5 6 . >/dev/null
753                 else
754                         echo vserver chkconfig: Expecting on or off
755                 fi
756         else
757                 echo chkconfig functionality is not available on this
758                 echo vserver distribution.
759                 echo Looked for /sbin/chkconfig and /usr/sbin/update-rc.d
760         fi
761 else
762         echo Command unknown $2
763         echo
764         usage
765 fi
766