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