b5851b5fd2dca05fefc9fcd2041c25cd71a8e53f
[bootcd.git] / conf_files / pl_netinit
1 #!/bin/sh
2
3 # the name of the floppy based network configuration
4 # files (checked first). the name planet.cnf is kept
5 # for backward compatibility with old nodes, and only
6 # the floppy disk is searched for files with this name.
7 # new files are named plnode.txt and can be located on
8 # a floppy or usb device or on the cdrom
9 OLD_NODE_CONF_NAME=planet.cnf
10 NEW_NODE_CONF_NAME=plnode.txt
11
12 # one location of cd-based network configuration file
13 # (checked if floppy conf file missing and usb
14 # configuration file is missing)
15 CD_NET_CONF_BOOT=/usr/boot/$NEW_NODE_CONF_NAME
16
17 # the other location of cd-based network configuration file
18 CD_NET_CONF_ROOT=/usr/$NEW_NODE_CONF_NAME
19
20 # if all other network configuration file sources 
21 # don't exist, fall back to this one (always on the cd)
22 FALLBACK_NET_CONF=/usr/boot/default-net.cnf
23
24 # a temporary place to hold the old configuration file
25 # off of the floppy disk if we find it (so we don't have
26 # to remount the floppy later)
27 TMP_OLD_FLOPPY_CONF_FILE=/tmp/oldfloppy_planet.cnf
28
29 # once a configuration file is found, save it in /tmp
30 # (may be used later by boot scripts)
31 USED_NET_CONF=/tmp/planet.cnf
32
33 # default device to use for contacting PLC if not specified
34 # in the configuration file
35 DEFAULT_NET_DEV=eth0
36
37 # where to store the temporary dhclient conf file
38 DHCLIENT_CONF_FILE=/tmp/dhclient.conf
39
40 # which fs types we support finding node configuration files on
41 # (will be based as a -t parameter to mount)
42 NODE_CONF_DEVICE_FS_TYPES="msdos,ext2"
43
44 # a temporary place to mount devices that might contain configuration
45 # files on
46 CONF_DEVICE_MOUNT_POINT=/mnt/confdevice
47 /bin/mkdir -p $CONF_DEVICE_MOUNT_POINT
48
49 # for some backwards compatibility, save the ifconfig <device>
50 # output to this file after everything is online
51 IFCONFIG_OUTPUT=/tmp/ifconfig
52
53 # set to 1 if the default network configuration was loaded off the cd
54 # (no other configuration file found)
55 DEFAULT_NET_CONF=0
56
57 net_init_failed()
58 {
59     echo
60     echo "pl_netinit: network initialization failed,"
61     echo "pl_netinit: shutting down machine in two hours"
62     /bin/sleep 7200
63     /sbin/shutdown -h now
64     exit 1
65 }
66
67 # Function for checking the IP address to see if its sensible.
68 check_ip()
69 {
70     case "$*" in
71         "" | *[!0-9.]* | *[!0-9]) return 1 ;;
72     esac
73     local IFS=.
74     set -- $*
75     [ $# -eq 4 ] &&
76     [ ${1:-666} -le 255 ] && [ ${2:-666} -le 255 ] &&
77     [ ${3:-666} -le 255 ] && [ ${4:-666} -le 255 ]
78 }
79
80 # find and parse a node network configuration file. return 0 if not found,
81 # return 1 if found and parsed. if this is the case, DEFAULT_NET_CONF will 
82 # be set to 1. For any found configuration file, $USED_NET_CONF will
83 # contain the validated contents
84 find_node_config()
85 {
86     /bin/rm -f $TMP_OLD_FLOPPY_CONF_FILE 2>&1 > /dev/null
87
88     echo "pl_netinit: looking for node configuration file on floppy"
89     
90     /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES /dev/fd0 \
91         $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
92     if [[ $? -eq 0 ]]; then
93
94         # 1. check for new named file first on the floppy disk
95         if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
96             echo "pl_netinit: found node configuration file plnode.txt, using"
97
98             conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
99             /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
100             /bin/umount $CONF_DEVICE_MOUNT_POINT
101             return 1
102
103         # since we have the floppy mounted already, see if an old file
104         # exists there so we don't have to remount the floppy when we need
105         # to check for an old file on it (later in the order). if it does
106         # just copy it off to a special location
107         elif [ -r "$CONF_DEVICE_MOUNT_POINT/$OLD_NODE_CONF_NAME" ]; then
108             conf_file="$CONF_DEVICE_MOUNT_POINT/$OLD_NODE_CONF_NAME"
109             /bin/cp -f $conf_file $TMP_OLD_FLOPPY_CONF_FILE
110             echo "pl_netinit: found old named configuration file, checking later."
111         else
112             echo "pl_netinit: floppy mounted, but no configuration file."
113         fi
114
115         /bin/umount $CONF_DEVICE_MOUNT_POINT
116     else
117         echo "pl_netinit: no floppy could be mounted, continuing search."
118     fi
119
120     # 2. check for a new named file on removable flash devices (those 
121     # that start with sd*, because usb_storage emulates scsi devices).
122     # to prevent checking normal scsi disks, also make sure
123     # /sys/block/<dev>/removable is set to 1
124
125     echo "pl_netinit: looking for node configuration file on flash based devices"
126
127     echo "pl_netinit: waiting for flash devices if any to come online"
128     # some usb flash based devices take a couple of seconds to initialize
129     # also, try to read the partition table off the device to make sure
130     # it shows up in /sys/block
131     /bin/sleep 10s
132
133     # make the sd* expansion fail to an empty string if there are no sd
134     # devices
135     shopt -s nullglob
136
137     for device in /sys/block/sd*; do
138         removable=`cat $device/removable`
139         if [[ $removable -ne 1 ]]; then
140             continue
141         fi
142
143         check_dev=/dev/`basename $device`1
144
145         echo "pl_netinit: looking for node configuration file on device $check_dev"
146         /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES $check_dev \
147             $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
148         if [[ $? -eq 0 ]]; then
149             if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
150                 echo "pl_netinit: found node configuration file plnode.txt, using"
151
152                 conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
153                 /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
154                 echo "pl_netinit: found configuration"
155                 /bin/umount $CONF_DEVICE_MOUNT_POINT
156                 return 1
157             fi
158             
159             echo "pl_netinit: not found"
160
161             /bin/umount $CONF_DEVICE_MOUNT_POINT
162         fi
163     done
164
165     # normal filename expansion setting
166     shopt -u nullglob
167
168     # 3. see if there is an old file on the floppy disk. if there was,
169     # the file $TMP_OLD_FLOPPY_CONF_FILE will be readable.
170     if [ -r "$TMP_OLD_FLOPPY_CONF_FILE" ]; then
171         echo "pl_netinit: found node configuration file planet.cnf, using"
172
173         conf_file=$TMP_OLD_FLOPPY_CONF_FILE
174         /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
175         return 1
176     fi
177
178
179     # 4. check for plnode.txt on the cd at /usr/boot
180     echo "pl_netinit: looking for network configuration on cd in /usr/boot"
181     if [ -r "$CD_NET_CONF_BOOT" ]; then
182         
183         echo "pl_netinit: found cd configuration file, using"
184         /etc/init.d/pl_validateconf < $CD_NET_CONF_BOOT > $USED_NET_CONF
185         return 1
186     fi
187     
188
189     # 5. check for plnode.txt on the cd at /usr
190     echo "pl_netinit: looking for network configuration on cd in /usr"
191     if [ -r "$CD_NET_CONF_ROOT" ]; then
192         
193         echo "pl_netinit: found cd configuration file, using"
194         /etc/init.d/pl_validateconf < $CD_NET_CONF_ROOT > $USED_NET_CONF
195         return 1
196     fi
197
198
199     # 6. no node configuration file could be found, fall back to
200     # builtin default. this can't be used to install a machine, but
201     # will at least let it download and run the boot manager, which
202     # can inform the users appropriately.
203     echo "pl_netinit: using default network configuration"
204     if [ -r "$FALLBACK_NET_CONF" ]; then
205         echo "pl_netinit: found cd default configuration file, using"
206         /etc/init.d/pl_validateconf < $FALLBACK_NET_CONF > $USED_NET_CONF
207         DEFAULT_NET_CONF=1
208         return 1
209     fi
210
211     echo "pl_netinit: using default network configuration from initrd"
212     echo 'IP_METHOD="dhcp"' > $USED_NET_CONF
213     echo 'HOST_NAME="planetlabnode"' >> $USED_NET_CONF
214     echo 'DOMAIN_NAME="localhost"' >> $USED_NET_CONF
215     DEFAULT_NET_CONF=1
216     return 1
217 }
218
219
220 echo "pl_netinit: bringing loopback network device up"
221 /sbin/ifconfig lo 127.0.0.1 up
222
223 find_node_config
224 if [ $? -eq 0 ]; then
225     # no network configuration file found. this should not happen as the
226     # default cd image has a backup one. halt.
227     echo "pl_netinit: unable to find even a default network configuration"
228     echo "pl_netinit: file, this cd may be corrupt."
229     net_init_failed
230 fi
231
232 # load the configuration file. if it was a default one (not user specified),
233 # then remove the saved copy from /tmp, but continue on. since a network 
234 # configuration file is required and boot scripts only know about this location
235 # they will fail (as they should) - but the network will be up if dhcp is
236 # available
237
238 echo "pl_netinit: loading network configuration"
239 . $USED_NET_CONF
240
241 if [[ $DEFAULT_NET_CONF -eq 1 ]]; then
242     /bin/rm -f $USED_NET_CONF
243 fi
244
245 # now, we need to find which device to use (ie, eth0 or eth1). start out
246 # by defaulting to eth0, then see if the network configuration file specified
247 # either a mac address (in which case we will need to find the device), or
248 # the device itself
249
250 if [[ -n "$NET_DEVICE" ]]; then
251     # the user specified a mac address we should use. find the network
252     # device for it.
253     echo "pl_netinit: looking for a device with mac address $NET_DEVICE"
254
255     pushd .
256     cd /sys/class/net
257     for device in *; do
258         dev_address=`cat $device/address`
259         if [[ "$dev_address" == "$NET_DEVICE" ]]; then
260             ETH_DEVICE=$device
261             echo "pl_netinit: found device $ETH_DEVICE"
262             break
263         fi
264     done
265     popd
266
267 elif [[ -n "$PRIMARY_DEV" ]]; then
268     # the user specified a virtual ethernet device to use.
269     ETH_DEVICE=$PRIMARY_DEV
270     echo "pl_netinit: using user specified device $ETH_DEVICE"
271
272 else
273     ETH_DEVICE=$DEFAULT_NET_DEV
274     echo "pl_netinit: using default device $ETH_DEVICE"
275
276 fi
277
278
279 # if we couldn't find a device (would happen if NET_DEVICE was specified
280 # but we couldn't find a device for that addresS), then abort the rest
281 # of the startup
282
283 if [[ -z "$ETH_DEVICE" ]]; then
284     echo "pl_netinit: unable to find a usable device, check to make sure"
285     echo "pl_netinit: the NET_DEVICE field in the configuration file"
286     echo "pl_netinit: cooresponds with a network adapter on this system"
287     net_init_failed
288 fi
289
290
291 # actually check to make sure ifconfig <device> succeeds
292 /sbin/ifconfig $ETH_DEVICE up 2>&1 > /dev/null
293 if [[ $? -ne 0 ]]; then
294     echo "pl_netinit: device $ETH_DEVICE does not exist,"
295     echo "pl_netinit: this is due to either the device not existing,"
296     echo "pl_netinit: or its device drivers not being loaded"
297     net_init_failed
298 fi
299
300 if [[ "$IP_METHOD" == "dhcp" ]]; then
301     echo "pl_netinit: attempting to bring up device with dhcp"
302
303     # setup a dhclient conf file for this device (used to send
304     # our hostname to the dhcp server)
305     echo "# generated by pl_netinit" > $DHCLIENT_CONF_FILE
306     echo "option planetlab-nfs-cluster-server code 200 = ip-address;" >> $DHCLIENT_CONF_FILE
307     echo "interface \"$ETH_DEVICE\" {" >> $DHCLIENT_CONF_FILE
308     echo "send host-name \"$HOST_NAME.$DOMAIN_NAME\";" >> $DHCLIENT_CONF_FILE
309     echo "request subnet-mask,broadcast-address,domain-name,host-name,routers,domain-name-servers,planetlab-nfs-cluster-server;" >> $DHCLIENT_CONF_FILE
310     echo "}" >> $DHCLIENT_CONF_FILE
311
312     # touch the redhat net device configuration file so 
313     # dhclient doesn't complain
314     /bin/touch /etc/sysconfig/network-scripts/ifcfg-$ETH_DEVICE
315
316     configured=0
317     while [[ $configured -eq 0 ]]; do
318         /sbin/dhclient -1 -cf $DHCLIENT_CONF_FILE $ETH_DEVICE 
319         if [[ $? -ne 0 ]]; then
320             echo "pl_netinit: dhcp failed, retrying in 2 minutes"
321             /bin/sleep 120
322         else
323             echo "pl_netinit: dhcp succeeded"
324             configured=1
325             break
326         fi
327     done
328 else
329     echo "pl_netinit: configuring device statically"
330
331     /sbin/ifconfig $ETH_DEVICE $IP_ADDRESS broadcast $IP_BROADCASTADDR \
332         netmask $IP_NETMASK
333     /sbin/route add default gw $IP_GATEWAY dev $ETH_DEVICE
334
335     if [[ -z "$IP_DNS1" ]]; then
336         echo "pl_netinit: no dns server specified, cannot continue."
337         net_init_failed
338     fi
339
340     echo "nameserver $IP_DNS1" > /etc/resolv.conf
341     if [[ -n "$IP_DNS2" ]]; then
342         echo "nameserver $IP_DNS2" >> /etc/resolv.conf
343     fi
344 fi
345
346 # for backwards compatibility
347 /sbin/ifconfig $ETH_DEVICE > $IFCONFIG_OUTPUT
348
349 echo "pl_netinit: network online"
350