merge with 0.30.213
[util-vserver.git] / scripts / vserver-setup.functions
1 # $Id: vserver-setup.functions 2517 2007-03-18 22:02:22Z dhozac $       --*- sh -*--
2
3 # Copyright (C) 2003,2004,2005,2006 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 SETUP_HOSTNAME=
19 SETUP_NETDEV=
20 SETUP_NETMASK=
21 SETUP_NETPREFIX=
22 SETUP_NETBCAST=
23 SETUP_LOCKFILE=
24 SETUP_CONFDIR=
25 SETUP_CONTEXT=
26 SETUP_INITSTYLE=
27 SETUP_CPUSET=
28 SETUP_CPUSETCPUS=
29 SETUP_CPUSETMEMS=
30 SETUP_CPUSETVIRT=
31
32 declare -a SETUP_INTERFACES=()
33 declare -a SETUP_FLAGS=()
34
35 declare -r SETUP_OPTIONS="confdir:,lockfile:,hostname:,netdev:,netmask:,netprefix:,netbcast:,interface:,flags:,context:,initstyle:,cpuset:,cpusetcpus:,cpusetmems:,cpusetvirt"
36 declare -r SETUP_HELPMSG=$"
37     --context   ...  the static context of the vserver [default: none; one will
38                      be generated for you]
39     --confdir   ...  [default: $__CONFDIR/<name>]
40     --lockfile <filename>
41                 ...  [default: $__RUNDIR/<name>]
42     --hostname <hostname>
43     --netdev   <device>
44     --netbcast <broadcast>
45     --netmask <netmask>|--netprefix <prefixlen>
46                 ...  sets the  default netmask  (a.b.c.d quadruple)  or prefixlen
47                      (length of the interface)
48     --interface [<name-suffix>=][<device>:]<ip>[/<mask|prefixlen>]
49                 ...  declares an network-interface;  this option can be specified
50                      multiple times
51     --flags <flags>+
52                 ...  sets comma-separated list of flags; possible flags are
53                      lock:  Prevent the vserver from setting new security context
54                      sched: Merge  scheduler priority  of all processes in the
55                             vserver so that it acts a like a single
56                             one (kernel 2.4 only).
57                      nproc: Limit the number of processes in the vserver
58                             according to ulimit  (instead of a per user limit,
59                             this becomes a per vserver limit)
60                      private: No other process can join this security context.
61                             Even root
62     --cpuset <name>
63                 ...  declares the CPUSET this vserver will run in [default: none]
64     --cpusetcpus <number[-number][:<exclusive>]>
65                 ...  sets which cpus belong to the CPUSET,
66                      exclusive is a flag (0|1) prohibiting any other cpuset from
67                      using those cpus
68     --cpusetmems <number[-number][:<exclusive>]>
69                 ...  sets which memory pools belong to the CPUSET,
70                      exclusive is a flag (0|1) prohibiting any other cpuset from
71                      using those memory pools
72     --cpusetvirt
73                 ...  virtualize cpuset (guest will see only CPUs defined in cpuset)
74                      Requires kernel patch from http://www.bullopensource.org/cpuset/
75     --initstyle <style>
76                 ...  configures the initstyle (e.g. minit,sysv,plain)
77 "
78
79 function setup_setOption2
80 {
81     case "$1" in
82         (--context)     SETUP_CONTEXT=$2;;
83         (--confdir)     SETUP_CONFDIR=$2;;
84         (--lockfile)    SETUP_LOCKFILE=$2;;
85         (--hostname)    SETUP_HOSTNAME=$2;;
86         (--netdev)      SETUP_NETDEV=$2;;
87         (--netmask)     SETUP_NETMASK=$2;;
88         (--netprefix)   SETUP_NETPREFIX=$2;;
89         (--netbcast)    SETUP_NETBCAST=$2;;
90         (--interface)   SETUP_INTERFACES=( "${SETUP_INTERFACES[@]}" "$2" );;
91         (--initstyle)   SETUP_INITSTYLE=$2;;
92         (--cpuset)      SETUP_CPUSET=$2;;
93         (--cpusetcpus)  old_IFS=$IFS
94                         IFS=:
95                         set -- $2
96                         SETUP_CPUSETCPUS=$1
97                         SETUP_CPUSETCPUSEXCL=$2
98                         IFS=$old_IFS
99                         ;;
100         (--cpusetmems)  old_IFS=$IFS
101                         IFS=:
102                         set -- $2
103                         SETUP_CPUSETMEMS=$1
104                         SETUP_CPUSETMEMSEXCL=$2
105                         IFS=$old_IFS
106                         ;;
107         (--cpusetvirt)  SETUP_CPUSETVIRT=1;;
108         (--flags)       old_IFS=$IFS
109                         IFS=,
110                         set -- $2
111                         SETUP_FLAGS=( "${SETUP_FLAGS[@]}" "$@" )
112                         IFS=$old_IFS
113                         ;;
114         (*)             return 1;;
115     esac
116
117     return 0
118 }
119
120 function _setup_writeSingleOption
121 {
122     test -z "$1" || echo "$1" >"$2"
123 }
124
125 function _setup_writeInterface
126 {
127     local vdir=$1
128     local idx=$2
129     local tmp=$3
130
131     local name=${tmp%%=*}
132     test "$name" != "$tmp" || name=
133
134     tmp=${tmp##${name}=}
135     local dev=${tmp%%:*}
136     local nodev=
137     test "$dev" != "$tmp"  || dev=
138
139     tmp=${tmp##${dev}:}
140     test "$dev" != "nodev" || {
141         dev=
142         nodev=1
143     }
144     local mask=${tmp##*/}
145     test "$mask" != "$tmp"  || mask=
146
147     local ip=${tmp%%/${mask}}
148
149     local prefix=
150     test "${mask%%.*}" != "$mask" || {
151         prefix=$mask
152         mask=
153     }
154
155     d=$vdir/interfaces/$idx
156     mkdir "$d"
157     
158     _setup_writeSingleOption "$name"   $d/name
159     _setup_writeSingleOption "$dev"    $d/dev
160     _setup_writeSingleOption "$ip"     $d/ip
161     _setup_writeSingleOption "$mask"   $d/mask
162     _setup_writeSingleOption "$prefix" $d/prefix
163
164     test -n "$dev" -o -n "$SETUP_NETDEV" || {
165         test -n "$nodev" || \
166             echo $"No device specified for interface '$idx'; setting 'nodev'" >&2
167         $_TOUCH $d/nodev
168     }
169 }
170
171 function setup_setDefaults
172 {
173     : ${SETUP_CONFDIR:=$__CONFDIR/$1}
174     : ${SETUP_LOCKFILE:=$__RUNDIR/$1}
175     findFile SETUP_FSTAB "$__CONFDIR"/.defaults/fstab "$__PKGLIBDEFAULTDIR"/fstab
176 }
177
178 function _setup_generateContext
179 {
180     if test -z "$SETUP_CONTEXT" && test ! -e "$__CONFDIR/.defaults/context.dynamic"; then
181         if test -e "$__CONFDIR/.defaults/context.next"; then
182             SETUP_CONTEXT=`$_CAT "$__CONFDIR/.defaults/context.next"`
183         else
184             SETUP_CONTEXT=`$_CAT "$__PKGLIBDEFAULTDIR/context.start"`
185         fi
186         expr "$SETUP_CONTEXT" + 1 > "$__CONFDIR/.defaults/context.next"
187     fi
188 }
189
190 function setup_writeOption
191 {
192     local name=$1
193     local cfgdir=${SETUP_CONFDIR:?}
194     local i
195
196     mkdir -p "$cfgdir"/interfaces "$cfgdir"/apps/init "$cfgdir"/uts "$cfgdir"/cpuset
197
198     _setup_generateContext
199
200     _setup_writeSingleOption "$name"            "$cfgdir"/name
201     _setup_writeSingleOption "$SETUP_CONTEXT"   "$cfgdir"/context
202     _setup_writeSingleOption "$SETUP_HOSTNAME"  "$cfgdir"/uts/nodename
203     _setup_writeSingleOption "$SETUP_NETDEV"    "$cfgdir"/interfaces/dev
204     _setup_writeSingleOption "$SETUP_NETMASK"   "$cfgdir"/interfaces/mask
205     _setup_writeSingleOption "$SETUP_NETPREFIX" "$cfgdir"/interfaces/prefix
206     _setup_writeSingleOption "$SETUP_NETBCAST"  "$cfgdir"/interfaces/bcast
207     _setup_writeSingleOption "$SETUP_INITSTYLE" "$cfgdir"/apps/init/style
208     _setup_writeSingleOption "$SETUP_CPUSET"    "$cfgdir"/cpuset/name
209     _setup_writeSingleOption "$SETUP_CPUSETCPUS"     "$cfgdir"/cpuset/cpus
210     _setup_writeSingleOption "$SETUP_CPUSETCPUSEXCL" "$cfgdir"/cpuset/cpus_exclusive
211     _setup_writeSingleOption "$SETUP_CPUSETMEMS"     "$cfgdir"/cpuset/mems
212     _setup_writeSingleOption "$SETUP_CPUSETMEMSEXCL" "$cfgdir"/cpuset/mem_exclusive
213     _setup_writeSingleOption "$SETUP_CPUSETVIRT"     "$cfgdir"/cpuset/virtualized
214
215     local idx=0
216     for i in "${SETUP_INTERFACES[@]}"; do
217         _setup_writeInterface "$cfgdir" $idx "$i"
218         let ++idx
219     done
220
221     test -z "$SETUP_FLAGS" || for i in "${SETUP_FLAGS[@]}"; do
222         echo "$i"
223     done >"$cfgdir"/flags
224
225     ln -s "$SETUP_LOCKFILE"   "$cfgdir"/run
226 }
227
228 function setup_writeInitialFstab
229 {
230     cat "${SETUP_FSTAB:?}" >"${SETUP_CONFDIR:?}"/fstab
231 }
232
233 function setup_test
234 {
235     SETUP_INTERFACES=()
236
237     setup_setOption2 --interface foo0=eth0:1.2.3.4/1
238     setup_setOption2 --interface foo1=eth0:1.2.3.4/255.255.248.0
239     setup_setOption2 --interface foo2=eth0:1.2.3.4
240     setup_setOption2 --interface foo3=1.2.3.4
241     setup_setOption2 --interface foo4=1.2.3.4/1
242     setup_setOption2 --interface eth0:1.2.3.4
243     setup_setOption2 --interface eth0:1.2.3.4/1
244     setup_setOption2 --interface 1.2.3.4
245     setup_setOption2 --interface 1.2.3.4/1
246
247     setup_writeOption xx
248 }