03c44d631018652fe7740dad97b5fccb301fd514
[util-vserver.git] / scripts / vserver-setup.functions
1 # $Id: vserver-setup.functions 2300 2006-09-12 12:16:53Z 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; a dynamic
38                      context will be assumed]
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     test "$dev" != "$tmp" || dev=
137
138     tmp=${tmp##${dev}:}
139     local mask=${tmp##*/}
140     test "$mask" != "$tmp"  || mask=
141
142     local ip=${tmp%%/${mask}}
143
144     local prefix=
145     test "${mask%%.*}" != "$mask" || {
146         prefix=$mask
147         mask=
148     }
149
150     d=$vdir/interfaces/$idx
151     mkdir "$d"
152     
153     _setup_writeSingleOption "$name"   $d/name
154     _setup_writeSingleOption "$dev"    $d/dev
155     _setup_writeSingleOption "$ip"     $d/ip
156     _setup_writeSingleOption "$mask"   $d/mask
157     _setup_writeSingleOption "$prefix" $d/prefix
158
159     test -n "$dev" -o -n "$SETUP_NETDEV" || \
160         echo $"No device specified for interface '$idx'; do not forget to set the 'nodev' option" >&2
161 }
162
163 function setup_setDefaults
164 {
165     : ${SETUP_CONFDIR:=$__CONFDIR/$1}
166     : ${SETUP_LOCKFILE:=$__RUNDIR/$1}
167     findFile SETUP_FSTAB "$__CONFDIR"/.defaults/fstab "$__PKGLIBDEFAULTDIR"/fstab
168 }
169
170 function setup_writeOption
171 {
172     local name=$1
173     local cfgdir=${SETUP_CONFDIR:?}
174     local i
175
176     mkdir -p "$cfgdir"/interfaces "$cfgdir"/apps/init "$cfgdir"/uts "$cfgdir"/cpuset
177
178     _setup_writeSingleOption "$name"            "$cfgdir"/name
179     _setup_writeSingleOption "$SETUP_CONTEXT"   "$cfgdir"/context
180     _setup_writeSingleOption "$SETUP_HOSTNAME"  "$cfgdir"/uts/nodename
181     _setup_writeSingleOption "$SETUP_NETDEV"    "$cfgdir"/interfaces/dev
182     _setup_writeSingleOption "$SETUP_NETMASK"   "$cfgdir"/interfaces/mask
183     _setup_writeSingleOption "$SETUP_NETPREFIX" "$cfgdir"/interfaces/prefix
184     _setup_writeSingleOption "$SETUP_NETBCAST"  "$cfgdir"/interfaces/bcast
185     _setup_writeSingleOption "$SETUP_INITSTYLE" "$cfgdir"/apps/init/style
186     _setup_writeSingleOption "$SETUP_CPUSET"    "$cfgdir"/cpuset/name
187     _setup_writeSingleOption "$SETUP_CPUSETCPUS"     "$cfgdir"/cpuset/cpus
188     _setup_writeSingleOption "$SETUP_CPUSETCPUSEXCL" "$cfgdir"/cpuset/cpus_exclusive
189     _setup_writeSingleOption "$SETUP_CPUSETMEMS"     "$cfgdir"/cpuset/mems
190     _setup_writeSingleOption "$SETUP_CPUSETMEMSEXCL" "$cfgdir"/cpuset/mem_exclusive
191     _setup_writeSingleOption "$SETUP_CPUSETVIRT"     "$cfgdir"/cpuset/virtualized
192
193     local idx=0
194     for i in "${SETUP_INTERFACES[@]}"; do
195         _setup_writeInterface "$cfgdir" $idx "$i"
196         let ++idx
197     done
198
199     test -z "$SETUP_FLAGS" || for i in "${SETUP_FLAGS[@]}"; do
200         echo "$i"
201     done >"$cfgdir"/flags
202
203     ln -s "$SETUP_LOCKFILE"   "$cfgdir"/run
204 }
205
206 function setup_writeInitialFstab
207 {
208     cat "${SETUP_FSTAB:?}" >"${SETUP_CONFDIR:?}"/fstab
209 }
210
211 function setup_test
212 {
213     SETUP_INTERFACES=()
214
215     setup_setOption2 --interface foo0=eth0:1.2.3.4/1
216     setup_setOption2 --interface foo1=eth0:1.2.3.4/255.255.248.0
217     setup_setOption2 --interface foo2=eth0:1.2.3.4
218     setup_setOption2 --interface foo3=1.2.3.4
219     setup_setOption2 --interface foo4=1.2.3.4/1
220     setup_setOption2 --interface eth0:1.2.3.4
221     setup_setOption2 --interface eth0:1.2.3.4/1
222     setup_setOption2 --interface 1.2.3.4
223     setup_setOption2 --interface 1.2.3.4/1
224
225     setup_writeOption xx
226 }