8dd8714aa9d1008e6e0c06e35bae9be9df7d0f13
[bootcd.git] / initscripts / pl_netinit
1 #!/bin/sh
2 #-*-shell-script-*-
3
4 # the name of the floppy based network configuration
5 # files (checked first). the name planet.cnf is kept
6 # for backward compatibility with old nodes, and only
7 # the floppy disk is searched for files with this name.
8 # new files are named plnode.txt and can be located on
9 # a floppy or usb device or on the cdrom
10 OLD_NODE_CONF_NAME=planet.cnf
11 NEW_NODE_CONF_NAME=plnode.txt
12
13 # one location of cd-based network configuration file
14 # (checked if floppy conf file missing and usb
15 # configuration file is missing)
16 CD_NET_CONF_BOOT=/usr/boot/$NEW_NODE_CONF_NAME
17
18 # the other location of cd-based network configuration file
19 CD_NET_CONF_ROOT=/usr/$NEW_NODE_CONF_NAME
20
21 # if all other network configuration file sources 
22 # don't exist, fall back to this one (always on the cd)
23 FALLBACK_NET_CONF=/usr/boot/default-node.txt
24
25 # a temporary place to hold the old configuration file
26 # off of the floppy disk if we find it (so we don't have
27 # to remount the floppy later)
28 TMP_OLD_FLOPPY_CONF_FILE=/tmp/oldfloppy_planet.cnf
29
30 # once a configuration file is found, save it in /tmp
31 # (may be used later by boot scripts)
32 USED_NET_CONF=/tmp/planet.cnf
33
34 # default device to use for contacting PLC if not specified
35 # in the configuration file
36 DEFAULT_NET_DEV=eth0
37
38 # where to store the temporary dhclient conf file
39 DHCLIENT_CONF_FILE=/tmp/dhclient.conf
40
41 # which fs types we support finding node configuration files on
42 # (will be based as a -t parameter to mount)
43 NODE_CONF_DEVICE_FS_TYPES="msdos,ext2"
44
45 # a temporary place to mount devices that might contain configuration
46 # files on
47 CONF_DEVICE_MOUNT_POINT=/mnt/confdevice
48 /bin/mkdir -p $CONF_DEVICE_MOUNT_POINT
49
50 # for some backwards compatibility, save the ifconfig <device>
51 # output to this file after everything is online
52 IFCONFIG_OUTPUT=/tmp/ifconfig
53
54 # set to 1 if the default network configuration was loaded off the cd
55 # (no other configuration file found)
56 DEFAULT_NET_CONF=0
57
58
59 function net_init_failed() {
60     echo
61     echo $(date "+%H:%M:%S") " pl_netinit: network initialization failed,"
62     echo $(date "+%H:%M:%S") " 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 function check_ip() {
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 function find_node_config() {
85     /bin/rm -f $TMP_OLD_FLOPPY_CONF_FILE 2>&1 > /dev/null
86
87     echo $(date "+%H:%M:%S") " pl_netinit: looking for node configuration file on floppy"
88     
89     /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES /dev/fd0 \
90         $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
91     if [[ $? -eq 0 ]]; then
92
93         # 1. check for new named file first on the floppy disk
94         if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
95             conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
96             echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
97             /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
98             /bin/umount $CONF_DEVICE_MOUNT_POINT
99             return 1
100
101         # since we have the floppy mounted already, see if an old file
102         # exists there so we don't have to remount the floppy when we need
103         # to check for an old file on it (later in the order). if it does
104         # just copy it off to a special location
105         elif [ -r "$CONF_DEVICE_MOUNT_POINT/$OLD_NODE_CONF_NAME" ]; then
106             conf_file="$CONF_DEVICE_MOUNT_POINT/$OLD_NODE_CONF_NAME"
107             /bin/cp -f $conf_file $TMP_OLD_FLOPPY_CONF_FILE
108             echo $(date "+%H:%M:%S") " pl_netinit: found old named configuration file, checking later."
109         else
110             echo $(date "+%H:%M:%S") " pl_netinit: floppy mounted, but no configuration file."
111         fi
112
113         /bin/umount $CONF_DEVICE_MOUNT_POINT
114     else
115         echo $(date "+%H:%M:%S") " pl_netinit: no floppy could be mounted, continuing search."
116     fi
117
118     # 2. check for a new named file on removable flash devices (those 
119     # that start with sd*, because usb_storage emulates scsi devices).
120     # to prevent checking normal scsi disks, also make sure
121     # /sys/block/<dev>/removable is set to 1
122
123     echo $(date "+%H:%M:%S") " pl_netinit: looking for node configuration file on flash based devices"
124
125     # make the sd* hd* expansion fail to an empty string if there are no sd
126     # devices
127     shopt -s nullglob
128
129     for device in /sys/block/[hsv]d*; do
130         removable=$(cat $device/removable)
131         if [[ $removable -ne 1 ]]; then
132             continue
133         fi
134
135         partitions=$(/bin/awk "\$4 ~ /$(basename $device)[0-9]*/ { print \$4 }" /proc/partitions)
136         for partition in $partitions ; do
137             check_dev=/dev/$partition
138
139             echo $(date "+%H:%M:%S") " pl_netinit: looking for node configuration file on device $check_dev"
140             /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES $check_dev \
141                 $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
142             if [[ $? -eq 0 ]]; then
143                 if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
144                     conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
145                     echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
146                     /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
147                     echo $(date "+%H:%M:%S") " pl_netinit: found configuration"
148                     /bin/umount $CONF_DEVICE_MOUNT_POINT
149                     return 1
150                 fi
151                 
152                 echo $(date "+%H:%M:%S") " pl_netinit: not found"
153
154                 /bin/umount $CONF_DEVICE_MOUNT_POINT
155             fi
156         done
157     done
158
159     # normal filename expansion setting
160     shopt -u nullglob
161
162     # 3. see if there is an old file on the floppy disk. if there was,
163     # the file $TMP_OLD_FLOPPY_CONF_FILE will be readable.
164     if [ -r "$TMP_OLD_FLOPPY_CONF_FILE" ]; then
165         conf_file=$TMP_OLD_FLOPPY_CONF_FILE
166         echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
167         /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
168         return 1
169     fi
170
171
172     # 4. check for plnode.txt on the cd at /usr/boot
173     echo $(date "+%H:%M:%S") " pl_netinit: looking for network configuration on cd in /usr/boot"
174     if [ -r "$CD_NET_CONF_BOOT" ]; then
175         
176         echo $(date "+%H:%M:%S") " pl_netinit: found cd configuration file $CD_NET_BOOT_CONF"
177         /etc/init.d/pl_validateconf < $CD_NET_CONF_BOOT > $USED_NET_CONF
178         return 1
179     fi
180     
181
182     # 5. check for plnode.txt on the cd at /usr
183     echo $(date "+%H:%M:%S") " pl_netinit: looking for network configuration on cd in /usr"
184     if [ -r "$CD_NET_CONF_ROOT" ]; then
185         
186         echo $(date "+%H:%M:%S") " pl_netinit: found cd configuration file $CD_NET_CONF_ROOT"
187         /etc/init.d/pl_validateconf < $CD_NET_CONF_ROOT > $USED_NET_CONF
188         return 1
189     fi
190
191
192     # 6. no node configuration file could be found, fall back to
193     # builtin default. this can't be used to install a machine, but
194     # will at least let it download and run the boot manager, which
195     # can inform the users appropriately.
196     echo $(date "+%H:%M:%S") " pl_netinit: using default network configuration"
197     if [ -r "$FALLBACK_NET_CONF" ]; then
198         echo $(date "+%H:%M:%S") " pl_netinit: found cd default configuration file $FALLBACK_NET_CONF"
199         /etc/init.d/pl_validateconf < $FALLBACK_NET_CONF > $USED_NET_CONF
200         DEFAULT_NET_CONF=1
201         return 1
202     fi
203
204     return 0
205 }
206
207
208 echo $(date "+%H:%M:%S") " pl_netinit: bringing loopback network device up"
209 /sbin/ifconfig lo 127.0.0.1 up
210
211 find_node_config
212 if [ $? -eq 0 ]; then
213     # no network configuration file found. this should not happen as the
214     # default cd image has a backup one. halt.
215     echo $(date "+%H:%M:%S") " pl_netinit: unable to find even a default network configuration"
216     echo $(date "+%H:%M:%S") " pl_netinit: file, this cd may be corrupt."
217     net_init_failed
218 fi
219
220 # load the configuration file. if it was a default one (not user specified),
221 # then remove the saved copy from /tmp, but continue on. since a network 
222 # configuration file is required and boot scripts only know about this location
223 # they will fail (as they should) - but the network will be up if dhcp is
224 # available
225
226 echo $(date "+%H:%M:%S") " pl_netinit: loading network configuration"
227 . $USED_NET_CONF
228
229 if [[ $DEFAULT_NET_CONF -eq 1 ]]; then
230     /bin/rm -f $USED_NET_CONF
231 fi
232
233 # initialize IPMI device
234 if [[ -n "$IPMI_ADDRESS" ]] ; then
235     echo -n "pl_netinit: initializing IPMI: "
236     cmd="ipnmac -i $IPMI_ADDRESS"
237     if [[ -n "$IPMI_MAC" ]] ; then
238         cmd="$cmd -m $IPMI_MAC"
239     fi
240     echo $cmd
241     $cmd
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 ETH_DEVICE=
250 if [[ -n "$NET_DEVICE" ]]; then
251     # the user specified a mac address we should use. find the network
252     # device for it.
253     NET_DEVICE=$(tr A-Z a-z <<<$NET_DEVICE)
254
255     pushd /sys/class/net
256     for device in *; do
257         dev_address=$(cat $device/address | tr A-Z a-z)
258         if [ "$device" == "$NET_DEVICE" -o "$dev_address" == "$NET_DEVICE" ]; then
259             ETH_DEVICE=$device
260             echo $(date "+%H:%M:%S") " pl_netinit: found device $ETH_DEVICE with mac address $dev_address"
261             break
262         fi
263     done
264     popd
265 fi
266
267 # if we didn't find a device yet, check which is the primary
268 if [[ -z "$ETH_DEVICE" ]]; then
269     pushd /etc/sysconfig/network-scripts > /dev/null
270     for conf in ifcfg-*; do
271         egrep -q '^PRIMARY=["'"'"']?[yY1t]' $conf || continue
272         ETH_DEVICE=${conf#ifcfg-}
273         break
274     done
275     popd > /dev/null
276 fi
277
278 # still nothing? fail the boot.
279 if [[ -z "$ETH_DEVICE" ]]; then
280     echo $(date "+%H:%M:%S") " pl_netinit: unable to find a usable device, check to make sure"
281     echo $(date "+%H:%M:%S") " pl_netinit: the NET_DEVICE field in the configuration file"
282     echo $(date "+%H:%M:%S") " pl_netinit: corresponds with a network adapter on this system"
283     net_init_failed
284 fi
285
286
287 # actually check to make sure ifconfig <device> succeeds
288 /sbin/ifconfig $ETH_DEVICE up 2>&1 > /dev/null
289 if [[ $? -ne 0 ]]; then
290     echo $(date "+%H:%M:%S") " pl_netinit: device $ETH_DEVICE does not exist, most likely"
291     echo $(date "+%H:%M:%S") " pl_netinit: this CD does not have hardware support for your"
292     echo $(date "+%H:%M:%S") " pl_netinit: network adapter. please send the following lines"
293     echo $(date "+%H:%M:%S") " pl_netinit: to your PlanetLab support for further assistance"
294     echo ========== lspci beg
295     /sbin/lspci -n | /bin/grep "Class 0200"    
296     echo ========== lspci end
297     echo ========== ifconfig beg
298     /sbin/ifconfig
299     echo ========== ifconfig beg
300
301     net_init_failed
302 fi
303
304 echo $(date "+%H:%M:%S") " pl_netinit: attempting to start networking"
305 /sbin/service network start
306
307 # for backwards compatibility
308 /sbin/ifconfig $ETH_DEVICE > $IFCONFIG_OUTPUT
309
310 echo $(date "+%H:%M:%S") " pl_netinit: network online"