127f42f8340d4dcb6b9741d70eb80507c90810b0
[util-vserver.git] / scripts / vserver-setup.functions
1 # $Id: vserver-setup.functions,v 1.18 2005/02/25 23:32:02 ensc Exp $    --*- 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_CONFDIR=
25 SETUP_CONTEXT=
26 SETUP_INITSTYLE=
27
28 declare -a SETUP_INTERFACES=()
29 declare -a SETUP_FLAGS=()
30
31 declare -r SETUP_OPTIONS="confdir:,lockfile:,hostname:,netdev:,netmask:,netprefix:,netbcast:,interface:,flags:,context:,initstyle:"
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     --hostname <hostname>
39     --netdev   <device>
40     --netbcast <broadcast>
41     --netmask <netmask>|--netprefix <prefixlen>
42                 ...  sets the  default netmask  (a.b.c.d quadruple)  or prefixlen
43                      (length of the interface)
44     --interface [<name-suffix>=][<device>:]<ip>[/<mask|prefixlen>]
45                 ...  declares an network-interface;  this option can be specified
46                      multiple times
47     --flags <flags>+
48                 ...  sets comma-separated list of flags; possible flags are
49                      lock:  Prevent the vserver from setting new security context
50                      sched: Merge  scheduler priority  of all processes in the
51                             vserver so that it acts a like a single
52                             one (kernel 2.4 only).
53                      nproc: Limit the number of processes in the vserver
54                             according to ulimit  (instead of a per user limit,
55                             this becomes a per vserver limit)
56                      private: No other process can join this security context.
57                             Even root
58     --initstyle <style>
59                 ...  configures the initstyle (e.g. minit,sysv,plain)
60 "
61
62 function setup_setOption2
63 {
64     case "$1" in
65         (--context)     SETUP_CONTEXT=$2;;
66         (--confdir)     SETUP_CONFDIR=$2;;
67         (--lockfile)    SETUP_LOCKFILE=$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         (--initstyle)   SETUP_INITSTYLE=$2;;
75         (--flags)       old_IFS=$IFS
76                         IFS=,
77                         set -- $2
78                         SETUP_FLAGS=( "${SETUP_FLAGS[@]}" "$@" )
79                         IFS=$old_IFS
80                         ;;
81         (*)             return 1;;
82     esac
83
84     return 0
85 }
86
87 function _setup_writeSingleOption
88 {
89     test -z "$1" || echo "$1" >"$2"
90 }
91
92 function _setup_writeInterface
93 {
94     local vdir=$1
95     local idx=$2
96     local tmp=$3
97
98     local name=${tmp%%=*}
99     test "$name" != "$tmp" || name=
100
101     tmp=${tmp##${name}=}
102     local dev=${tmp%%:*}
103     test "$dev" != "$tmp" || dev=
104
105     tmp=${tmp##${dev}:}
106     local mask=${tmp##*/}
107     test "$mask" != "$tmp"  || mask=
108
109     local ip=${tmp%%/${mask}}
110
111     local prefix=
112     test "${mask%%.*}" != "$mask" || {
113         prefix=$mask
114         mask=
115     }
116
117     d=$vdir/interfaces/$idx
118     mkdir "$d"
119     
120     _setup_writeSingleOption "$name"   $d/name
121     _setup_writeSingleOption "$dev"    $d/dev
122     _setup_writeSingleOption "$ip"     $d/ip
123     _setup_writeSingleOption "$mask"   $d/mask
124     _setup_writeSingleOption "$prefix" $d/prefix
125
126     test -n "$dev" -o -n "$SETUP_NETDEV" || \
127         echo $"No device specified for interface '$idx'; do not forget to set the 'nodev' option" >&2
128 }
129
130 function setup_setDefaults
131 {
132     : ${SETUP_CONFDIR:=$__CONFDIR/$1}
133     : ${SETUP_LOCKFILE:=$__RUNDIR/$1}
134     findFile SETUP_FSTAB "$__CONFDIR"/.defaults/fstab "$__PKGLIBDEFAULTDIR"/fstab
135 }
136
137 function setup_writeOption
138 {
139     local name=$1
140     local cfgdir=${SETUP_CONFDIR:?}
141     local i
142
143     mkdir -p "$cfgdir"/interfaces "$cfgdir"/apps/init "$cfgdir"/uts
144
145     _setup_writeSingleOption "$name"            "$cfgdir"/name
146     _setup_writeSingleOption "$SETUP_CONTEXT"   "$cfgdir"/context
147     _setup_writeSingleOption "$SETUP_HOSTNAME"  "$cfgdir"/uts/nodename
148     _setup_writeSingleOption "$SETUP_NETDEV"    "$cfgdir"/interfaces/dev
149     _setup_writeSingleOption "$SETUP_NETMASK"   "$cfgdir"/interfaces/mask
150     _setup_writeSingleOption "$SETUP_NETPREFIX" "$cfgdir"/interfaces/prefix
151     _setup_writeSingleOption "$SETUP_NETBCAST"  "$cfgdir"/interfaces/bcast
152     _setup_writeSingleOption "$SETUP_INITSTYLE" "$cfgdir"/apps/init/style
153
154     local idx=0
155     for i in "${SETUP_INTERFACES[@]}"; do
156         _setup_writeInterface "$cfgdir" $idx "$i"
157         let ++idx
158     done
159
160     test -z "$SETUP_FLAGS" || for i in "${SETUP_FLAGS[@]}"; do
161         echo "$i"
162     done >"$cfgdir"/flags
163
164     ln -s "$SETUP_LOCKFILE"   "$cfgdir"/run
165 }
166
167 function setup_writeInitialFstab
168 {
169     cat "${SETUP_FSTAB:?}" >"${SETUP_CONFDIR:?}"/fstab
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 }