0fa960aa5d791663bafdc741683007dc5cd4f790
[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 net_init_failed()
60 {
61     echo
62     echo $(date "+%H:%M:%S") " pl_netinit: network initialization failed,"
63     echo $(date "+%H:%M:%S") " pl_netinit: shutting down machine in two hours"
64     /bin/sleep 2h
65     /sbin/shutdown -h now
66     exit 1
67 }
68
69 # Function for checking the IP address to see if its sensible.
70 check_ip()
71 {
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 find_node_config()
87 {
88     /bin/rm -f $TMP_OLD_FLOPPY_CONF_FILE 2>&1 > /dev/null
89
90     echo $(date "+%H:%M:%S") " pl_netinit: looking for node configuration file on floppy"
91     
92     /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES /dev/fd0 \
93         $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
94     if [[ $? -eq 0 ]]; then
95
96         # 1. check for new named file first on the floppy disk
97         if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
98             conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
99             echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
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 $(date "+%H:%M:%S") " pl_netinit: found old named configuration file, checking later."
112         else
113             echo $(date "+%H:%M:%S") " pl_netinit: floppy mounted, but no configuration file."
114         fi
115
116         /bin/umount $CONF_DEVICE_MOUNT_POINT
117     else
118         echo $(date "+%H:%M:%S") " 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 $(date "+%H:%M:%S") " 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/[hsv]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 $(date "+%H:%M:%S") " 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                     conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
148                     echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
149                     /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
150                     echo $(date "+%H:%M:%S") " pl_netinit: found configuration"
151                     /bin/umount $CONF_DEVICE_MOUNT_POINT
152                     return 1
153                 fi
154                 
155                 echo $(date "+%H:%M:%S") " pl_netinit: not found"
156
157                 /bin/umount $CONF_DEVICE_MOUNT_POINT
158             fi
159         done
160     done
161
162     # normal filename expansion setting
163     shopt -u nullglob
164
165     # 3. see if there is an old file on the floppy disk. if there was,
166     # the file $TMP_OLD_FLOPPY_CONF_FILE will be readable.
167     if [ -r "$TMP_OLD_FLOPPY_CONF_FILE" ]; then
168         conf_file=$TMP_OLD_FLOPPY_CONF_FILE
169         echo $(date "+%H:%M:%S") " pl_netinit: found node configuration file $conf_file"
170         /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
171         return 1
172     fi
173
174
175     # 4. check for plnode.txt on the cd at /usr/boot
176     echo $(date "+%H:%M:%S") " pl_netinit: looking for network configuration on cd in /usr/boot"
177     if [ -r "$CD_NET_CONF_BOOT" ]; then
178         
179         echo $(date "+%H:%M:%S") " pl_netinit: found cd configuration file $CD_NET_BOOT_CONF"
180         /etc/init.d/pl_validateconf < $CD_NET_CONF_BOOT > $USED_NET_CONF
181         return 1
182     fi
183     
184
185     # 5. check for plnode.txt on the cd at /usr
186     echo $(date "+%H:%M:%S") " pl_netinit: looking for network configuration on cd in /usr"
187     if [ -r "$CD_NET_CONF_ROOT" ]; then
188         
189         echo $(date "+%H:%M:%S") " pl_netinit: found cd configuration file $CD_NET_CONF_ROOT"
190         /etc/init.d/pl_validateconf < $CD_NET_CONF_ROOT > $USED_NET_CONF
191         return 1
192     fi
193
194
195     # 6. no node configuration file could be found, fall back to
196     # builtin default. this can't be used to install a machine, but
197     # will at least let it download and run the boot manager, which
198     # can inform the users appropriately.
199     echo $(date "+%H:%M:%S") " pl_netinit: using default network configuration"
200     if [ -r "$FALLBACK_NET_CONF" ]; then
201         echo $(date "+%H:%M:%S") " pl_netinit: found cd default configuration file $FALLBACK_NET_CONF"
202         /etc/init.d/pl_validateconf < $FALLBACK_NET_CONF > $USED_NET_CONF
203         DEFAULT_NET_CONF=1
204         return 1
205     fi
206
207     return 0
208 }
209
210
211 echo $(date "+%H:%M:%S") " pl_netinit: bringing loopback network device up"
212 /sbin/ifconfig lo 127.0.0.1 up
213
214 find_node_config
215 if [ $? -eq 0 ]; then
216     # no network configuration file found. this should not happen as the
217     # default cd image has a backup one. halt.
218     echo $(date "+%H:%M:%S") " pl_netinit: unable to find even a default network configuration"
219     echo $(date "+%H:%M:%S") " pl_netinit: file, this cd may be corrupt."
220     net_init_failed
221 fi
222
223 # load the configuration file. if it was a default one (not user specified),
224 # then remove the saved copy from /tmp, but continue on. since a network 
225 # configuration file is required and boot scripts only know about this location
226 # they will fail (as they should) - but the network will be up if dhcp is
227 # available
228
229 echo $(date "+%H:%M:%S") " pl_netinit: loading network configuration"
230 . $USED_NET_CONF
231
232 if [[ $DEFAULT_NET_CONF -eq 1 ]]; then
233     /bin/rm -f $USED_NET_CONF
234 fi
235
236 # initialize IPMI device
237 if [[ -n "$IPMI_ADDRESS" ]] ; then
238     echo -n "pl_netinit: initializing IPMI: "
239     cmd="ipnmac -i $IPMI_ADDRESS"
240     if [[ -n "$IPMI_MAC" ]] ; then
241         cmd="$cmd -m $IPMI_MAC"
242     fi
243     echo $cmd
244     $cmd
245 fi
246
247 # now, we need to find which device to use (ie, eth0 or eth1). start out
248 # by defaulting to eth0, then see if the network configuration file specified
249 # either a mac address (in which case we will need to find the device), or
250 # the device itself
251
252 ETH_DEVICE=
253 if [[ -n "$NET_DEVICE" ]]; then
254     # the user specified a mac address we should use. find the network
255     # device for it.
256     NET_DEVICE=$(tr A-Z a-z <<<$NET_DEVICE)
257
258     pushd /sys/class/net
259     for device in *; do
260         dev_address=$(cat $device/address | tr A-Z a-z)
261         if [ "$device" == "$NET_DEVICE" -o "$dev_address" == "$NET_DEVICE" ]; then
262             ETH_DEVICE=$device
263             echo $(date "+%H:%M:%S") " pl_netinit: found device $ETH_DEVICE with mac address $dev_address"
264             break
265         fi
266     done
267     popd
268 fi
269
270 # if we didn't find a device yet, check which is the primary
271 if [[ -z "$ETH_DEVICE" ]]; then
272     pushd /etc/sysconfig/network-scripts > /dev/null
273     for conf in ifcfg-*; do
274         egrep -q '^PRIMARY=["'"'"']?[yY1t]' $conf || continue
275         ETH_DEVICE=${conf#ifcfg-}
276         break
277     done
278     popd > /dev/null
279 fi
280
281 # still nothing? fail the boot.
282 if [[ -z "$ETH_DEVICE" ]]; then
283     echo $(date "+%H:%M:%S") " pl_netinit: unable to find a usable device, check to make sure"
284     echo $(date "+%H:%M:%S") " pl_netinit: the NET_DEVICE field in the configuration file"
285     echo $(date "+%H:%M:%S") " pl_netinit: corresponds with a network adapter on this system"
286     net_init_failed
287 fi
288
289
290 # actually check to make sure ifconfig <device> succeeds
291 /sbin/ifconfig $ETH_DEVICE up 2>&1 > /dev/null
292 if [[ $? -ne 0 ]]; then
293     echo $(date "+%H:%M:%S") " pl_netinit: device $ETH_DEVICE does not exist, most likely"
294     echo $(date "+%H:%M:%S") " pl_netinit: this CD does not have hardware support for your"
295     echo $(date "+%H:%M:%S") " pl_netinit: network adapter. please send the following lines"
296     echo $(date "+%H:%M:%S") " pl_netinit: to your PlanetLab support for further assistance"
297     echo
298     /sbin/lspci -n | /bin/grep "Class 0200"    
299     echo
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"
311