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