added some doc
[util-vserver.git] / util-vserver / scripts / start-vservers
1 #! /bin/bash
2
3 # Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 #  
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
8 #  
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #  
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 # Usage: start-vservers [-c <CFGDIR>] [-m <MARK>] [-j <NUM>] [--start|--stop|--status|--condrestart|--restart] [--test] [--all] [--debug] -- <name>+
19
20 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
21 test -e "$UTIL_VSERVER_VARS" || {
22     echo "Can not find util-vserver installation; aborting..." >&2
23     exit 1
24 }
25 . "$UTIL_VSERVER_VARS"
26 . "$_LIB_FUNCTIONS"
27
28 ### Some local functions
29
30 function showHelp()
31 {
32     echo \
33 $"Usage: $(basename $0) [-c <CFGDIR>] [-m <MARK>] [-j <NUM] [--test]
34              [--start|--stop] [--all] -- <name>+
35
36 Please report bugs to $PACKAGE_BUGREPORT"
37     exit 0
38 }
39
40
41 function showVersion()
42 {
43     echo \
44 $"start-vserver $PACKAGE_VERSION -- starts/stops a bunch of vservers
45 This program is part of $PACKAGE_STRING
46
47 Copyright (C) 2004 Enrico Scholz
48 This program is free software; you may redistribute it under the terms of
49 the GNU General Public License.  This program has absolutely no warranty."
50     exit 0
51 }
52
53 function verifyVserver()
54 {
55     true
56 }
57
58 ###
59
60 set +e
61
62
63 tmp=$(getopt -o c:j:m: --long debug,help,version,start,stop,test,all -n "$0" -- "$@") || exit 1
64 eval set -- "$tmp"
65
66 declare -r TAB=$(echo -en "\t")
67 OPTION_MARK=
68 OPTION_ALL=
69 OPTION_PARALLEL=99
70 OPTION_DEBUG=
71 NOOPTION_DEBUG=1
72
73 case "`basename $0`" in
74     start-*)    OPTION_FLAVOR=start;;
75     stop-*)     OPTION_FLAVOR=stop;;
76     *)          OPTION_FLAVOR=;;
77 esac
78
79 while true; do
80     case "$1" in
81         --help)         showHelp    $0 ;;
82         --version)      showVersion $0 ;;
83         -c)             CONFDIR=$2;     shift;;
84         -m)             OPTION_MARK=$2; shift;;
85         -j)             OPTION_PARALLEL=$2; shift;;
86         --start)        OPTION_FLAVOR=start;;
87         --stop)         OPTION_FLAVOR=stop;;
88         --all)          OPTION_ALL=1;;
89         --debug)        OPTION_DEBUG=1; NOOPTION_DEBUG=; set -x;;
90         --)             shift; break;;
91         *)              echo $"$0: internal error; arg=='$1'" >&2; exit 1;;
92     esac
93     shift
94 done
95
96 test -n "$OPTION_FLAVOR" || {
97     echo "$0: unknown invocation method; aborting..." >&2
98     exit 1
99 }
100
101 vservers=( "$@" )
102 test -z  "$OPTION_ALL" || getAllVservers vservers
103
104 makedir=$(mktemp -d /tmp/vserver-init.XXXXXX)
105 trap "rm -rf $makedir" EXIT
106
107 defaulttty="$CONFDIR/.defaults/apps/init/tty"
108 test -c "$defaulttty" || defaulttty=$(tty)
109 test -c "$defaulttty" || defaulttty=/dev/null
110
111 test_cmd=false
112 case "$OPTION_FLAVOR" in
113     start)      test_cmd="${_VSERVER}   --silent '\$*' status";;
114     stop)       test_cmd="! ${_VSERVER} --silent '\$*' status";;
115 esac
116
117
118 {
119     cat <<EOF
120 TTY = ${defaulttty}
121 .%.stamp:
122 ${TAB}$test_cmd || \
123 ${TAB}${_VSERVER} --sync ${OPTION_DEBUG:+--debug} "\$*" ${OPTION_FLAVOR} >\$(TTY) 2>\$(TTY) <\$(TTY)
124 ${TAB}@touch "\$@"
125 EOF
126
127     echo -ne "all:\t"
128     for i in "${vservers[@]}"; do
129         echo -n ".$i.stamp "
130     done
131     echo
132 } >$makedir/Makefile
133
134 orig_vservers=$vservers
135 i=${#vservers[*]}
136
137 while test $i -gt 0; do
138     let --i
139     d=$CONFDIR/${vservers[$i]}/apps/init
140     { test "$OPTION_MARK"    && grep -sx "$OPTION_MARK" "$d"/mark; } || \
141     { test -z "$OPTION_MARK" && test ! -e "$d"/mark; } || \
142     unset vservers[$i]
143 done
144
145 for i in "${vservers[@]}"; do
146     d=$CONFDIR/$i/apps/init
147     echo "$i"
148     test -e "$d"/depends || continue
149     cat "$d"/depends
150 done | sort -u | while read vserver; do
151     d=$CONFDIR/$vserver/apps/init
152     test ! -e $d/tty || {
153         echo -e ".$vserver.stamp:\tTTY=$d/tty" >>$makedir/Makefile
154     }
155
156     case "$OPTION_FLAVOR" in
157         start)
158             if test -e "$d"/depends; then
159                 echo -ne ".$vserver.stamp:\t"
160                 cat "$d"/depends | while read dep; do
161                     verifyVserver "$dep"
162                     echo -n ".$dep.stamp "
163                 done
164                 echo
165             fi >>$makedir/Makefile
166             ;;
167         stop)
168             if test -e "$d"/depends; then
169                 cat "$d"/depends | while read dep; do
170                     verifyVserver "$dep"
171                     echo -ne ".$dep.stamp:\t.$vserver.stamp"
172                 done
173                 echo
174             fi >>$makedir/Makefile
175     esac
176 done
177
178 #cat $makedir/Makefile
179 make -k ${NOOPTION_DEBUG:+-s} ${OPTION_PARALLEL:+-j$OPTION_PARALLEL} -C $makedir