added Vector_zeroEnd() function
[util-vserver.git] / util-vserver / scripts / vserver-setup.functions
1 # $Id$  --*- sh -*--
2
3 # Copyright (C) 2003 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_LOCKREVDIR=
25 SETUP_CONFDIR=
26 SETUP_CONTEXT=
27 SETUP_INITSTYLE=
28
29 declare -a SETUP_INTERFACES=()
30 declare -a SETUP_FLAGS=()
31
32 declare -r SETUP_OPTIONS="confdir:,lockfile:,lockrevdir:,hostname:,netdev:,netmask:,netprefix:,netbcast:,interface:,flags:,context:,initstyle:"
33 declare -r SETUP_HELPMSG=$"
34     --context   ...  the static context of the vserver [default: none; a dynamic
35                      context will be assumed]
36     --confdir   ...  [default: $CONFDIR/<name>]
37     --lockfile <filename>
38                 ...  [default: $RUNDIR/<name>]
39     --lockrevdir <dirname>
40                 ...  [default: $CONFDIR/.defaults/run.rev]
41     --hostname <hostname>
42     --netdev   <device>
43     --netbcast <broadcast>
44     --netmask <netmask>|--netprefix <prefix>
45                 ...  sets the  default netmask  (a.b.c.d quadruple)  or prefix
46                      (length of the interface)
47     --interface [<name-suffix>=][<device>:]<ip>[/<mask|prefix>]
48                 ...  declares an network-interface;  this option can be specified
49                      multiple times
50     --flags <flags>+
51                 ...  sets comma-separated list of flags; possible flags are
52                      lock:  Prevent the vserver from setting new security context
53                      sched: Merge  scheduler priority  of all processes in the
54                             vserver so that it acts a like a single one.
55                      nproc: Limit the number of processes in the vserver
56                             according to ulimit  (instead of a per user limit,
57                             this becomes a per vserver limit)
58                      private: No other process can join this security context.
59                             Even root
60     --initstyle <style>
61                 ...  configures the initstyle (e.g. minit,sysv,plain)
62 "
63
64 function setup_setOption2
65 {
66     case "$1" in
67         (--context)     SETUP_CONTEXT=$2;;
68         (--confdir)     SETUP_CONFDIR=$2;;
69         (--lockfile)    SETUP_LOCKFILE=$2;;
70         (--lockrevdir)  SEUTP_LOCKREVDIR=$2;;
71         (--hostname)    SETUP_HOSTNAME=$2;;
72         (--netdev)      SETUP_NETDEV=$2;;
73         (--netmask)     SETUP_NETMASK=$2;;
74         (--netprefix)   SETUP_NETPREFIX=$2;;
75         (--netbcast)    SETUP_NETBCAST=$2;;
76         (--interface)   SETUP_INTERFACES=( "${SETUP_INTERFACES[@]}" "$2" );;
77         (--initstyle)   SETUP_INITSTYLE=$2;;
78         (--flags)       old_IFS=$IFS
79                         IFS=,
80                         set -- $2
81                         SETUP_FLAGS=( "${SETUP_FLAGS[@]}" "$@" )
82                         IFS=$old_IFS
83                         ;;
84         (*)             return 1;;
85     esac
86
87     return 0
88 }
89
90 function _setup_writeSingleOption
91 {
92     test -z "$1" || echo "$1" >"$2"
93 }
94
95 function _setup_writeInterface
96 {
97     local vdir=$1
98     local idx=$2
99     local tmp=$3
100
101     local name=${tmp%%=*}
102     test "$name" != "$tmp" || name=
103
104     tmp=${tmp##${name}=}
105     local dev=${tmp%%:*}
106     test "$dev" != "$tmp" || dev=
107
108     tmp=${tmp##${dev}:}
109     local mask=${tmp##*/}
110     test "$mask" != "$tmp"  || mask=
111
112     local ip=${tmp%%/${mask}}
113
114     local prefix=
115     test "${mask%%.*}" != "$mask" || {
116         prefix=$mask
117         mask=
118     }
119
120     d=$vdir/interfaces/$idx
121     mkdir "$d"
122     
123     _setup_writeSingleOption "$name"   $d/name
124     _setup_writeSingleOption "$dev"    $d/dev
125     _setup_writeSingleOption "$ip"     $d/ip
126     _setup_writeSingleOption "$mask"   $d/mask
127     _setup_writeSingleOption "$prefix" $d/prefix
128 }
129
130 function setup_setDefaults
131 {
132     : ${SETUP_CONFDIR:=$CONFDIR/$1}
133     : ${SETUP_LOCKFILE:=$RUNDIR/$1}
134     : ${SETUP_LOCKREVDIR:=$CONFDIR/.defaults/run.rev}
135     findFile SETUP_FSTAB "$CONFDIR"/.defaults/fstab "$PKGLIBDEFAULTDIR"/fstab
136 }
137
138 function setup_writeOption
139 {
140     local name=$1
141     local cfgdir=$SETUP_CONFDIR
142     local i
143
144     mkdir -p "$cfgdir"/interfaces "$cfgdir"/apps/init "$cfgdir"/uts
145
146     _setup_writeSingleOption "$name"            "$cfgdir"/name
147     _setup_writeSingleOption "$SETUP_CONTEXT"   "$cfgdir"/context
148     _setup_writeSingleOption "$SETUP_HOSTNAME"  "$cfgdir"/uts/nodename
149     _setup_writeSingleOption "$SETUP_NETDEV"    "$cfgdir"/interfaces/dev
150     _setup_writeSingleOption "$SETUP_NETMASK"   "$cfgdir"/interfaces/mask
151     _setup_writeSingleOption "$SETUP_NETPREFIX" "$cfgdir"/interfaces/prefix
152     _setup_writeSingleOption "$SETUP_NETBCAST"  "$cfgdir"/interfaces/bcast
153     _setup_writeSingleOption "$SETUP_INITSTYLE" "$cfgdir"/apps/init/style
154
155     local idx=0
156     for i in "${SETUP_INTERFACES[@]}"; do
157         _setup_writeInterface "$cfgdir" $idx "$i"
158         let ++idx
159     done
160
161     test -z "$SETUP_FLAGS" || for i in "${SETUP_FLAGS[@]}"; do
162         echo "$i"
163     done >"$cfgdir"/flags
164
165     ln -s "$SETUP_LOCKFILE"   "$cfgdir"/run
166     ln -s "$SETUP_LOCKREVDIR" "$cfgdir"/run.rev
167 }
168
169 function setup_writeInitialFstab
170 {
171     cat "$SETUP_FSTAB" >"$SETUP_CONFDIR"/fstab
172 }
173
174 function setup_test
175 {
176     SETUP_INTERFACES=()
177
178     setup_setOption2 --interface foo0=eth0:1.2.3.4/1
179     setup_setOption2 --interface foo1=eth0:1.2.3.4/255.255.248.0
180     setup_setOption2 --interface foo2=eth0:1.2.3.4
181     setup_setOption2 --interface foo3=1.2.3.4
182     setup_setOption2 --interface foo4=1.2.3.4/1
183     setup_setOption2 --interface eth0:1.2.3.4
184     setup_setOption2 --interface eth0:1.2.3.4/1
185     setup_setOption2 --interface 1.2.3.4
186     setup_setOption2 --interface 1.2.3.4/1
187
188     setup_writeOption xx
189 }