fixed the logic for the timeout (break instead of continue..)
[bootcd.git] / initscripts / pl_netinit
1 #!/bin/sh
2 #-*-shell-script-*-
3
4 set -x
5
6 # the name of the floppy based network configuration
7 # files (checked first). the name planet.cnf is kept
8 # for backward compatibility with old nodes, and only
9 # the floppy disk is searched for files with this name.
10 # new files are named plnode.txt and can be located on
11 # a floppy or usb device or on the cdrom
12 OLD_NODE_CONF_NAME=planet.cnf
13 NEW_NODE_CONF_NAME=plnode.txt
14
15 # one location of cd-based network configuration file
16 # (checked if floppy conf file missing and usb
17 # configuration file is missing)
18 CD_NET_CONF_BOOT=/usr/boot/$NEW_NODE_CONF_NAME
19
20 # the other location of cd-based network configuration file
21 CD_NET_CONF_ROOT=/usr/$NEW_NODE_CONF_NAME
22
23 # if all other network configuration file sources 
24 # don't exist, fall back to this one (always on the cd)
25 FALLBACK_NET_CONF=/usr/boot/default-node.txt
26
27 # a temporary place to hold the old configuration file
28 # off of the floppy disk if we find it (so we don't have
29 # to remount the floppy later)
30 TMP_OLD_FLOPPY_CONF_FILE=/tmp/oldfloppy_planet.cnf
31
32 # once a configuration file is found, save it in /tmp
33 # (may be used later by boot scripts)
34 USED_NET_CONF=/tmp/planet.cnf
35
36 # default device to use for contacting PLC if not specified
37 # in the configuration file
38 DEFAULT_NET_DEV=eth0
39
40 # where to store the temporary dhclient conf file
41 DHCLIENT_CONF_FILE=/tmp/dhclient.conf
42
43 # which fs types we support finding node configuration files on
44 # (will be based as a -t parameter to mount)
45 NODE_CONF_DEVICE_FS_TYPES="msdos,ext2"
46
47 # a temporary place to mount devices that might contain configuration
48 # files on
49 CONF_DEVICE_MOUNT_POINT=/mnt/confdevice
50 /bin/mkdir -p $CONF_DEVICE_MOUNT_POINT
51
52 # for some backwards compatibility, save the ifconfig <device>
53 # output to this file after everything is online
54 IFCONFIG_OUTPUT=/tmp/ifconfig
55
56 # set to 1 if the default network configuration was loaded off the cd
57 # (no other configuration file found)
58 DEFAULT_NET_CONF=0
59
60
61 function net_init_failed() {
62     echo
63     echo $(date "+%H:%M:%S") " pl_netinit: network initialization failed,"
64     echo $(date "+%H:%M:%S") " pl_netinit: shutting down machine in two hours"
65     /bin/sleep 2h
66     /sbin/shutdown -h now
67     exit 1
68 }
69
70 # Function for checking the IP address to see if its sensible.
71 function check_ip() {
72     case "$*" in
73         "" | *[!0-9.]* | *[!0-9]) return 1 ;;
74     esac
75     local IFS=.
76     set -- $*
77     [ $# -eq 4 ] &&
78     [ ${1:-666} -le 255 ] && [ ${2:-666} -le 255 ] &&
79     [ ${3:-666} -le 255 ] && [ ${4:-666} -le 255 ]
80 }
81
82 # find and parse a node network configuration file. return 0 if not found,
83 # return 1 if found and parsed. if this is the case, DEFAULT_NET_CONF will 
84 # be set to 1. For any found configuration file, $USED_NET_CONF will
85 # contain the validated contents
86 function find_node_config() {
87     /bin/rm -f $TMP_OLD_FLOPPY_CONF_FILE 2>&1 > /dev/null
88
89     echo $(date "+%H:%M:%S") " 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             conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
98             echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
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 $(date "+%H:%M:%S") " pl_netinit: found old named configuration file, checking later."
111         else
112             echo $(date "+%H:%M:%S") " pl_netinit: floppy mounted, but no configuration file."
113         fi
114
115         /bin/umount $CONF_DEVICE_MOUNT_POINT
116     else
117         echo $(date "+%H:%M:%S") " 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 $(date "+%H:%M:%S") " pl_netinit: looking for node configuration file on flash based devices"
126
127     # make the sd* hd* expansion fail to an empty string if there are no sd
128     # devices
129     shopt -s nullglob
130
131     for device in /sys/block/[hsv]d*; do
132         removable=$(cat $device/removable)
133         if [[ $removable -ne 1 ]]; then
134             continue
135         fi
136
137         partitions=$(/bin/awk "\$4 ~ /$(basename $device)[0-9]*/ { print \$4 }" /proc/partitions)
138         for partition in $partitions ; do
139             check_dev=/dev/$partition
140
141             echo $(date "+%H:%M:%S") " pl_netinit: looking for node configuration file on device $check_dev"
142             /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES $check_dev \
143                 $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
144             if [[ $? -eq 0 ]]; then
145                 if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
146                     conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
147                     echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
148                     /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
149                     echo $(date "+%H:%M:%S") " pl_netinit: found configuration"
150                     /bin/umount $CONF_DEVICE_MOUNT_POINT
151                     return 1
152                 fi
153                 
154                 echo $(date "+%H:%M:%S") " pl_netinit: not found"
155
156                 /bin/umount $CONF_DEVICE_MOUNT_POINT
157             fi
158         done
159     done
160
161     # normal filename expansion setting
162     shopt -u nullglob
163
164     # 3. see if there is an old file on the floppy disk. if there was,
165     # the file $TMP_OLD_FLOPPY_CONF_FILE will be readable.
166     if [ -r "$TMP_OLD_FLOPPY_CONF_FILE" ]; then
167         conf_file=$TMP_OLD_FLOPPY_CONF_FILE
168         echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
169         /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
170         return 1
171     fi
172
173
174     # 4. check for plnode.txt on the cd at /usr/boot
175     echo $(date "+%H:%M:%S") " pl_netinit: looking for network configuration on cd in /usr/boot"
176     if [ -r "$CD_NET_CONF_BOOT" ]; then
177         
178         echo $(date "+%H:%M:%S") " pl_netinit: found cd configuration file $CD_NET_BOOT_CONF"
179         /etc/init.d/pl_validateconf < $CD_NET_CONF_BOOT > $USED_NET_CONF
180         return 1
181     fi
182     
183
184     # 5. check for plnode.txt on the cd at /usr
185     echo $(date "+%H:%M:%S") " pl_netinit: looking for network configuration on cd in /usr"
186     if [ -r "$CD_NET_CONF_ROOT" ]; then
187         
188         echo $(date "+%H:%M:%S") " pl_netinit: found cd configuration file $CD_NET_CONF_ROOT"
189         /etc/init.d/pl_validateconf < $CD_NET_CONF_ROOT > $USED_NET_CONF
190         return 1
191     fi
192
193
194     # 6. no node configuration file could be found, fall back to
195     # builtin default. this can't be used to install a machine, but
196     # will at least let it download and run the boot manager, which
197     # can inform the users appropriately.
198     echo $(date "+%H:%M:%S") " pl_netinit: using default network configuration"
199     if [ -r "$FALLBACK_NET_CONF" ]; then
200         echo $(date "+%H:%M:%S") " pl_netinit: found cd default configuration file $FALLBACK_NET_CONF"
201         /etc/init.d/pl_validateconf < $FALLBACK_NET_CONF > $USED_NET_CONF
202         DEFAULT_NET_CONF=1
203         return 1
204     fi
205
206     return 0
207 }
208
209
210 echo $(date "+%H:%M:%S") " pl_netinit: bringing loopback network device up"
211 /sbin/ifconfig lo 127.0.0.1 up
212
213 find_node_config
214 if [ $? -eq 0 ]; then
215     # no network configuration file found. this should not happen as the
216     # default cd image has a backup one. halt.
217     echo $(date "+%H:%M:%S") " pl_netinit: unable to find even a default network configuration"
218     echo $(date "+%H:%M:%S") " pl_netinit: file, this cd may be corrupt."
219     net_init_failed
220 fi
221
222 # load the configuration file. if it was a default one (not user specified),
223 # then remove the saved copy from /tmp, but continue on. since a network 
224 # configuration file is required and boot scripts only know about this location
225 # they will fail (as they should) - but the network will be up if dhcp is
226 # available
227
228 echo $(date "+%H:%M:%S") " pl_netinit: loading network configuration"
229 . $USED_NET_CONF
230
231 if [[ $DEFAULT_NET_CONF -eq 1 ]]; then
232     /bin/rm -f $USED_NET_CONF
233 fi
234
235 # initialize IPMI device
236 if [[ -n "$IPMI_ADDRESS" ]] ; then
237     echo -n "pl_netinit: initializing IPMI: "
238     cmd="ipnmac -i $IPMI_ADDRESS"
239     if [[ -n "$IPMI_MAC" ]] ; then
240         cmd="$cmd -m $IPMI_MAC"
241     fi
242     echo $cmd
243     $cmd
244 fi
245
246 # now, we need to find which device to use (ie, eth0 or eth1). start out
247 # by defaulting to eth0, then see if the network configuration file specified
248 # either a mac address (in which case we will need to find the device), or
249 # the device itself
250
251 ETH_DEVICE=
252 if [[ -n "$NET_DEVICE" ]]; then
253     # the user specified a mac address we should use. find the network
254     # device for it.
255     NET_DEVICE=$(tr A-Z a-z <<<$NET_DEVICE)
256
257     pushd /sys/class/net
258     for device in *; do
259         dev_address=$(cat $device/address | tr A-Z a-z)
260         if [ "$device" == "$NET_DEVICE" -o "$dev_address" == "$NET_DEVICE" ]; then
261             ETH_DEVICE=$device
262             echo $(date "+%H:%M:%S") " pl_netinit: found device $ETH_DEVICE with mac address $dev_address"
263             break
264         fi
265     done
266     popd
267 fi
268
269 # if we didn't find a device yet, check which is the primary
270 if [[ -z "$ETH_DEVICE" ]]; then
271     pushd /etc/sysconfig/network-scripts > /dev/null
272     for conf in ifcfg-*; do
273         egrep -q '^PRIMARY=["'"'"']?[yY1t]' $conf || continue
274         ETH_DEVICE=${conf#ifcfg-}
275         break
276     done
277     popd > /dev/null
278 fi
279
280 # still nothing? fail the boot.
281 if [[ -z "$ETH_DEVICE" ]]; then
282     echo $(date "+%H:%M:%S") " pl_netinit: unable to find a usable device, check to make sure"
283     echo $(date "+%H:%M:%S") " pl_netinit: the NET_DEVICE field in the configuration file"
284     echo $(date "+%H:%M:%S") " pl_netinit: corresponds with a network adapter on this system"
285     net_init_failed
286 fi
287
288 # within a systemd-driven startup, we often see this stage
289 # triggered before the network is actually up
290 # although of course we have network-online.target
291 # as a requirement; go figure what systemd actually does..
292
293 # in any case, let us try to work around that by allowing some delay
294 # here
295
296 TIMEOUT=30
297 while true; do
298     if /sbin/ifconfig $ETH_DEVICE >& /dev/null; then
299         echo Device $ETH_DEVICE found - proceeding - timeout=$TIMEOUT
300         break
301     fi
302     echo $(date "+%H:%M:%S") " pl_netinit: waiting for device $ETH_DEVICE, ${TIMEOUT}s remaining "
303     TIMEOUT=$(($TIMEOUT-1))
304     [ $TIMEOUT -le 0 ] && net_init_failed
305     sleep 1
306 done
307
308
309 # actually check to make sure ifconfig <device> succeeds
310
311 /sbin/ifconfig $ETH_DEVICE up 2>&1 > /dev/null || {
312     echo $(date "+%H:%M:%S") " pl_netinit: device $ETH_DEVICE does not exist, most likely"
313     echo $(date "+%H:%M:%S") " pl_netinit: this CD does not have hardware support for your"
314     echo $(date "+%H:%M:%S") " pl_netinit: network adapter. please send the following lines"
315     echo $(date "+%H:%M:%S") " pl_netinit: to your PlanetLab support for further assistance"
316     echo ========== lspci beg
317     /sbin/lspci -n | /bin/grep "Class 0200"    
318     echo ========== lspci end
319     echo ========== ifconfig beg
320     /sbin/ifconfig
321     echo ========== ifconfig beg
322
323     net_init_failed
324 }
325
326 echo $(date "+%H:%M:%S") " pl_netinit: attempting to start networking"
327 /sbin/service network start
328
329 # for backwards compatibility
330 /sbin/ifconfig $ETH_DEVICE > $IFCONFIG_OUTPUT
331
332 echo $(date "+%H:%M:%S") " pl_netinit: network online"