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