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