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