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