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