have pl_netinit source pl_functions, for messages to show up as expected
[bootcd.git] / initscripts / pl_netinit
1 #!/bin/bash
2 #-*-shell-script-*-
3
4 set -x
5
6 source /etc/init.d/pl_functions
7
8 # the name of the floppy based network configuration
9 # files (checked first). the name planet.cnf is kept
10 # for backward compatibility with old nodes, and only
11 # the floppy disk is searched for files with this name.
12 # new files are named plnode.txt and can be located on
13 # a floppy or usb device or on the cdrom
14 OLD_NODE_CONF_NAME=planet.cnf
15 NEW_NODE_CONF_NAME=plnode.txt
16
17 # one location of cd-based network configuration file
18 # (checked if floppy conf file missing and usb
19 # configuration file is missing)
20 CD_NET_CONF_BOOT=/usr/boot/$NEW_NODE_CONF_NAME
21
22 # the other location of cd-based network configuration file
23 CD_NET_CONF_ROOT=/usr/$NEW_NODE_CONF_NAME
24
25 # if all other network configuration file sources
26 # don't exist, fall back to this one (always on the cd)
27 FALLBACK_NET_CONF=/usr/boot/default-node.txt
28
29 # a temporary place to hold the old configuration file
30 # off of the floppy disk if we find it (so we don't have
31 # to remount the floppy later)
32 TMP_OLD_FLOPPY_CONF_FILE=/tmp/oldfloppy_planet.cnf
33
34 # once a configuration file is found, save it in /tmp
35 # (may be used later by boot scripts)
36 USED_NET_CONF=/tmp/planet.cnf
37
38 # default device to use for contacting PLC if not specified
39 # in the configuration file
40 DEFAULT_NET_DEV=eth0
41
42 # where to store the temporary dhclient conf file
43 DHCLIENT_CONF_FILE=/tmp/dhclient.conf
44
45 # which fs types we support finding node configuration files on
46 # (will be based as a -t parameter to mount)
47 NODE_CONF_DEVICE_FS_TYPES="msdos,ext2"
48
49 # a temporary place to mount devices that might contain configuration
50 # files on
51 CONF_DEVICE_MOUNT_POINT=/mnt/confdevice
52 /bin/mkdir -p $CONF_DEVICE_MOUNT_POINT
53
54 # for some backwards compatibility, save the ifconfig <device>
55 # output to this file after everything is online
56 IFCONFIG_OUTPUT=/tmp/ifconfig
57
58 # set to 1 if the default network configuration was loaded off the cd
59 # (no other configuration file found)
60 DEFAULT_NET_CONF=0
61
62
63 function net-init-failed() {
64     verbose-message "pl_netinit: network initialization failed with interface $ETH_DEVICE"
65     verbose-forensics "failed to configure $ETH_DEVICE"
66     hang-and-shutdown "net-init-failed"
67     exit 1
68 }
69
70 # find and parse a node network configuration file. return 0 if not found,
71 # return 1 if found and parsed. if this is the case, DEFAULT_NET_CONF will
72 # be set to 1. For any found configuration file, $USED_NET_CONF will
73 # contain the validated contents
74 function find-node-config() {
75     /bin/rm -f $TMP_OLD_FLOPPY_CONF_FILE 2>&1 > /dev/null
76
77     verbose-message "pl_netinit: looking for node configuration file on floppy"
78
79     /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES /dev/fd0 \
80         $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
81     if [[ $? -eq 0 ]]; then
82
83         # 1. check for new named file first on the floppy disk
84         if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
85             conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
86             verbose-message "pl_netinit: found node configuration file $conf_file"
87             /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
88             /bin/umount $CONF_DEVICE_MOUNT_POINT
89             return 1
90
91         # since we have the floppy mounted already, see if an old file
92         # exists there so we don't have to remount the floppy when we need
93         # to check for an old file on it (later in the order). if it does
94         # just copy it off to a special location
95         elif [ -r "$CONF_DEVICE_MOUNT_POINT/$OLD_NODE_CONF_NAME" ]; then
96             conf_file="$CONF_DEVICE_MOUNT_POINT/$OLD_NODE_CONF_NAME"
97             /bin/cp -f $conf_file $TMP_OLD_FLOPPY_CONF_FILE
98             verbose-message "pl_netinit: found old named configuration file, checking later."
99         else
100             verbose-message "pl_netinit: floppy mounted, but no configuration file."
101         fi
102
103         /bin/umount $CONF_DEVICE_MOUNT_POINT
104     else
105         verbose-message "pl_netinit: no floppy could be mounted, continuing search."
106     fi
107
108     # 2. check for a new named file on removable flash devices (those
109     # that start with sd*, because usb_storage emulates scsi devices).
110     # to prevent checking normal scsi disks, also make sure
111     # /sys/block/<dev>/removable is set to 1
112
113     verbose-message "pl_netinit: looking for node configuration file on flash based devices"
114
115     # make the sd* hd* expansion fail to an empty string if there are no sd
116     # devices
117     shopt -s nullglob
118
119     for device in /sys/block/[hsv]d*; do
120         removable=$(cat $device/removable)
121         if [[ $removable -ne 1 ]]; then
122             continue
123         fi
124
125         partitions=$(/bin/awk "\$4 ~ /$(basename $device)[0-9]*/ { print \$4 }" /proc/partitions)
126         for partition in $partitions ; do
127             check_dev=/dev/$partition
128
129             verbose-message "pl_netinit: looking for node configuration file on device $check_dev"
130             /bin/mount -o ro -t $NODE_CONF_DEVICE_FS_TYPES $check_dev \
131                 $CONF_DEVICE_MOUNT_POINT 2>&1 > /dev/null
132             if [[ $? -eq 0 ]]; then
133                 if [ -r "$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME" ]; then
134                     conf_file="$CONF_DEVICE_MOUNT_POINT/$NEW_NODE_CONF_NAME"
135                     verbose-message "pl_netinit: found node configuration file $conf_file"
136                     /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
137                     verbose-message "pl_netinit: found configuration"
138                     /bin/umount $CONF_DEVICE_MOUNT_POINT
139                     return 1
140                 fi
141
142                 verbose-message "pl_netinit: ERROR - not found"
143
144                 /bin/umount $CONF_DEVICE_MOUNT_POINT
145             fi
146         done
147     done
148
149     # normal filename expansion setting
150     shopt -u nullglob
151
152     # 3. see if there is an old file on the floppy disk. if there was,
153     # the file $TMP_OLD_FLOPPY_CONF_FILE will be readable.
154     if [ -r "$TMP_OLD_FLOPPY_CONF_FILE" ]; then
155         conf_file=$TMP_OLD_FLOPPY_CONF_FILE
156         verbose-message "pl_netinit: found node configuration file $conf_file"
157         /etc/init.d/pl_validateconf < $conf_file > $USED_NET_CONF
158         return 1
159     fi
160
161
162     # 4. check for plnode.txt on the cd at /usr/boot
163     verbose-message "pl_netinit: looking for network configuration on cd in /usr/boot"
164     if [ -r "$CD_NET_CONF_BOOT" ]; then
165
166         verbose-message "pl_netinit: found cd configuration file $CD_NET_BOOT_CONF"
167         /etc/init.d/pl_validateconf < $CD_NET_CONF_BOOT > $USED_NET_CONF
168         return 1
169     fi
170
171
172     # 5. check for plnode.txt on the cd at /usr
173     verbose-message "pl_netinit: looking for network configuration on cd in /usr"
174     if [ -r "$CD_NET_CONF_ROOT" ]; then
175
176         verbose-message "pl_netinit: found cd configuration file $CD_NET_CONF_ROOT"
177         /etc/init.d/pl_validateconf < $CD_NET_CONF_ROOT > $USED_NET_CONF
178         return 1
179     fi
180
181
182     # 6. no node configuration file could be found, fall back to
183     # builtin default. this can't be used to install a machine, but
184     # will at least let it download and run the boot manager, which
185     # can inform the users appropriately.
186     verbose-message "pl_netinit: using default network configuration"
187     if [ -r "$FALLBACK_NET_CONF" ]; then
188         verbose-message "pl_netinit: found cd default configuration file $FALLBACK_NET_CONF"
189         /etc/init.d/pl_validateconf < $FALLBACK_NET_CONF > $USED_NET_CONF
190         DEFAULT_NET_CONF=1
191         return 1
192     fi
193
194     return 0
195 }
196
197
198 verbose-message "pl_netinit: bringing loopback network device up"
199 ip address add dev lo 127.0.0.1/8
200 ip link set lo up
201
202 find-node-config
203 if [ $? -eq 0 ]; then
204     # no network configuration file found. this should not happen as the
205     # default cd image has a backup one. halt.
206     verbose-message "pl_netinit: ERROR - unable to find even a default network configuration"
207     verbose-message "pl_netinit: file, this cd may be corrupt."
208     net-init-failed
209 fi
210
211 # load the configuration file. if it was a default one (not user specified),
212 # then remove the saved copy from /tmp, but continue on. since a network
213 # configuration file is required and boot scripts only know about this location
214 # they will fail (as they should) - but the network will be up if dhcp is
215 # available
216
217 verbose-message "pl_netinit: loading network configuration"
218 . $USED_NET_CONF
219
220 if [[ $DEFAULT_NET_CONF -eq 1 ]]; then
221     /bin/rm -f $USED_NET_CONF
222 fi
223
224 # initialize IPMI device
225 if [[ -n "$IPMI_ADDRESS" ]] ; then
226     echo -n "pl_netinit: initializing IPMI: "
227     cmd="ipnmac -i $IPMI_ADDRESS"
228     if [[ -n "$IPMI_MAC" ]] ; then
229         cmd="$cmd -m $IPMI_MAC"
230     fi
231     echo $cmd
232     $cmd
233 fi
234
235 # now, we need to find which device to use (ie, eth0 or eth1). start out
236 # by defaulting to eth0, then see if the network configuration file specified
237 # either a mac address (in which case we will need to find the device), or
238 # the device itself
239
240 ETH_DEVICE=
241 if [[ -n "$NET_DEVICE" ]]; then
242     # the user specified a mac address we should use. find the network
243     # device for it.
244     NET_DEVICE=$(tr A-Z a-z <<< $NET_DEVICE)
245
246     pushd /sys/class/net
247     for device in *; do
248         dev_address=$(cat $device/address | tr A-Z a-z)
249         if [ "$device" == "$NET_DEVICE" -o "$dev_address" == "$NET_DEVICE" ]; then
250             ETH_DEVICE=$device
251             verbose-message "pl_netinit: found device $ETH_DEVICE with mac address $dev_address"
252             break
253         fi
254     done
255     popd
256 fi
257
258 # if we didn't find a device yet, check which is the primary
259 if [[ -z "$ETH_DEVICE" ]]; then
260     pushd /etc/sysconfig/network-scripts > /dev/null
261     for conf in ifcfg-*; do
262         egrep -q '^PRIMARY=["'"'"']?[yY1t]' $conf || continue
263         ETH_DEVICE=${conf#ifcfg-}
264         break
265     done
266     popd > /dev/null
267 fi
268
269 # still nothing? fail the boot.
270 if [[ -z "$ETH_DEVICE" ]]; then
271     verbose-message "pl_netinit: unable to find a usable device, check to make sure"
272     verbose-message "pl_netinit: the NET_DEVICE field in the configuration file"
273     verbose-message "pl_netinit: corresponds with a network adapter on this system"
274     net-init-failed
275 fi
276
277 # within a systemd-driven startup, we often see this stage
278 # triggered before the network interface is actually exposed
279 # by udev/kernel
280 # so allow for some small delay here
281 ALLOW=10
282 COUNTER=0
283 while true; do
284     ### check if the interface is known
285     # instead of using ifconfig, check the kernel area
286     if [ -e /sys/class/net/$ETH_DEVICE ]; then
287         verbose-message "pl_netinit: device present $ETH_DEVICE"
288         break
289     fi
290     verbose-message "pl_netinit: waiting for device $ETH_DEVICE - ${COUNTER}s/${ALLOW}s"
291     sleep 1
292     COUNTER=$(($COUNTER+1))
293     [ $COUNTER -ge $ALLOW ] && net-init-failed
294     sleep 1
295 done
296
297
298 # actually check to make sure ifconfig <device> succeeds
299
300 /usr/libexec/nm-ifup $ETH_DEVICE >& /dev/null || {
301     verbose-message "pl_netinit: device $ETH_DEVICE does not exist, most likely"
302     verbose-message "pl_netinit: this CD does not have hardware support for your"
303     verbose-message "pl_netinit: network adapter. please send the following lines"
304     verbose-message "pl_netinit: to your PlanetLab support for further assistance"
305     net-init-failed
306 }
307
308 verbose-message "pl_netinit: attempting to start networking"
309 /sbin/service network start
310
311 # for backwards compatibility
312 /sbin/ifconfig $ETH_DEVICE > $IFCONFIG_OUTPUT
313
314 verbose-message "pl_netinit: network online"
315
316 # patch for f22 and above - if /etc/resolv.conf is empty or missing
317 function pl-netinit-patch-resolv-conf () {
318     file="$1"; shift
319     needed=""
320     # missing file : patch needed
321     if ! [ -f $file ]; then
322         needed=true
323     # empty file : patch needed
324     else
325         contents=$(grep -v '^#' $file)
326         contents=$(echo $contents)
327         [ -z "$contents" ] && needed=true
328     fi
329     if [ -z "$needed" ]; then
330         echo pl_netinit has no need to patch $file
331         return
332     fi
333
334     verbose-message "pl_netinit: patching $file"
335     source /etc/sysconfig/network-scripts/ifcfg-${ETH_DEVICE}
336     # delete because it may be a symlink to /run/systemd/resolve/resolv.conf
337     # which looks really weird (ls -lL /etc/resolv.conf does not show anything)
338     rm -f $file
339     # mention the DNS servers defined in the config - if any
340     [ -n "$DNS1" ] && echo nameserver $DNS1 >> $file
341     [ -n "$DNS2" ] && echo nameserver $DNS2 >> $file
342     # also add landmark for good measure
343     echo "nameserver 8.8.8.8" >> $file
344 }
345
346 pl-netinit-patch-resolv-conf /etc/resolv.conf
347
348 verbose-forensics "pl_netinit epilogue"