stupid me
[bootcd.git] / initscripts / pl_netinit
index babff5c..071873c 100755 (executable)
@@ -1,4 +1,7 @@
 #!/bin/sh
+#-*-shell-script-*-
+
+set -x
 
 # the name of the floppy based network configuration
 # files (checked first). the name planet.cnf is kept
@@ -55,10 +58,20 @@ IFCONFIG_OUTPUT=/tmp/ifconfig
 DEFAULT_NET_CONF=0
 
 
-net_init_failed()
-{
+function net_init_failed() {
     echo
     echo $(date "+%H:%M:%S") " pl_netinit: network initialization failed,"
+    echo
+    echo For forensics
+    echo
+    echo ========== lspci beg
+    /sbin/lspci -n | /bin/grep "Class 0200"    
+    echo ========== lspci end
+    echo
+    echo ========== ifconfig beg
+    /sbin/ifconfig
+    echo ========== ifconfig end
+    echo 
     echo $(date "+%H:%M:%S") " pl_netinit: shutting down machine in two hours"
     /bin/sleep 2h
     /sbin/shutdown -h now
@@ -66,8 +79,7 @@ net_init_failed()
 }
 
 # Function for checking the IP address to see if its sensible.
-check_ip()
-{
+function check_ip() {
     case "$*" in
         "" | *[!0-9.]* | *[!0-9]) return 1 ;;
     esac
@@ -82,8 +94,7 @@ check_ip()
 # return 1 if found and parsed. if this is the case, DEFAULT_NET_CONF will 
 # be set to 1. For any found configuration file, $USED_NET_CONF will
 # contain the validated contents
-find_node_config()
-{
+function find_node_config() {
     /bin/rm -f $TMP_OLD_FLOPPY_CONF_FILE 2>&1 > /dev/null
 
     echo $(date "+%H:%M:%S") " pl_netinit: looking for node configuration file on floppy"
@@ -128,13 +139,13 @@ find_node_config()
     # devices
     shopt -s nullglob
 
-    for device in /sys/block/[hs]d*; do
-       removable=`cat $device/removable`
+    for device in /sys/block/[hsv]d*; do
+       removable=$(cat $device/removable)
        if [[ $removable -ne 1 ]]; then
            continue
        fi
 
-       partitions=$(/bin/awk "\$4 ~ /`basename $device`[0-9]*/ { print \$4 }" /proc/partitions)
+       partitions=$(/bin/awk "\$4 ~ /$(basename $device)[0-9]*/ { print \$4 }" /proc/partitions)
        for partition in $partitions ; do
            check_dev=/dev/$partition
 
@@ -248,6 +259,7 @@ fi
 # either a mac address (in which case we will need to find the device), or
 # the device itself
 
+ETH_DEVICE=
 if [[ -n "$NET_DEVICE" ]]; then
     # the user specified a mac address we should use. find the network
     # device for it.
@@ -255,7 +267,7 @@ if [[ -n "$NET_DEVICE" ]]; then
 
     pushd /sys/class/net
     for device in *; do
-       dev_address=`cat $device/address | tr A-Z a-z`
+       dev_address=$(cat $device/address | tr A-Z a-z)
        if [ "$device" == "$NET_DEVICE" -o "$dev_address" == "$NET_DEVICE" ]; then
            ETH_DEVICE=$device
            echo $(date "+%H:%M:%S") " pl_netinit: found device $ETH_DEVICE with mac address $dev_address"
@@ -263,80 +275,64 @@ if [[ -n "$NET_DEVICE" ]]; then
        fi
     done
     popd
-else
-    ETH_DEVICE=$DEFAULT_NET_DEV
-    echo $(date "+%H:%M:%S") " pl_netinit: using default device $ETH_DEVICE"
-
 fi
 
+# if we didn't find a device yet, check which is the primary
+if [[ -z "$ETH_DEVICE" ]]; then
+    pushd /etc/sysconfig/network-scripts > /dev/null
+    for conf in ifcfg-*; do
+       egrep -q '^PRIMARY=["'"'"']?[yY1t]' $conf || continue
+       ETH_DEVICE=${conf#ifcfg-}
+       break
+    done
+    popd > /dev/null
+fi
 
-# if we couldn't find a device (would happen if NET_DEVICE was specified
-# but we couldn't find a device for that addresS), then abort the rest
-# of the startup
-
+# still nothing? fail the boot.
 if [[ -z "$ETH_DEVICE" ]]; then
     echo $(date "+%H:%M:%S") " pl_netinit: unable to find a usable device, check to make sure"
     echo $(date "+%H:%M:%S") " pl_netinit: the NET_DEVICE field in the configuration file"
-    echo $(date "+%H:%M:%S") " pl_netinit: cooresponds with a network adapter on this system"
+    echo $(date "+%H:%M:%S") " pl_netinit: corresponds with a network adapter on this system"
     net_init_failed
 fi
 
+# within a systemd-driven startup, we often see this stage
+# triggered before the network interface is actually exposed
+# by udev/kernel
+# although of course we have network-online.target
+# as a requirement; go figure what systemd actually does..
+
+# in any case, let us try to work around that by allowing some delay
+# here
+
+ALLOW=5
+COUNTER=0
+while true; do
+    if /sbin/ifconfig $ETH_DEVICE >& /dev/null; then
+       echo "pl_netinit: device present $ETH_DEVICE, proceeding (${COUNTER}s/${ALLOW}s)"
+       break
+    fi
+    echo $(date "+%H:%M:%S") " pl_netinit: waiting for device $ETH_DEVICE,${COUNTER}s/${ALLOW}s"
+    COUNTER=$(($COUNTER+1))
+    [ $COUNTER -ge $ALLOW ] && net_init_failed
+    sleep 1
+done
+
 
 # actually check to make sure ifconfig <device> succeeds
-/sbin/ifconfig $ETH_DEVICE up 2>&1 > /dev/null
-if [[ $? -ne 0 ]]; then
+
+/sbin/ifconfig $ETH_DEVICE up 2>&1 > /dev/null || {
     echo $(date "+%H:%M:%S") " pl_netinit: device $ETH_DEVICE does not exist, most likely"
-    echo $(date "+%H:%M:%S") " pl_netinit: this cd does not have hardware support for your"
+    echo $(date "+%H:%M:%S") " pl_netinit: this CD does not have hardware support for your"
     echo $(date "+%H:%M:%S") " pl_netinit: network adapter. please send the following lines"
-    echo $(date "+%H:%M:%S") " pl_netinit: to PlanetLab Support: support@planet-lab.org"
-    echo $(date "+%H:%M:%S") " pl_netinit: for further assistance"
-    echo
-    /sbin/lspci -n | /bin/grep "Class 0200"    
-    echo
-
+    echo $(date "+%H:%M:%S") " pl_netinit: to your PlanetLab support for further assistance"
     net_init_failed
-fi
-
-ifcfg=/etc/sysconfig/network-scripts/ifcfg-$ETH_DEVICE
-echo "DEVICE=$ETH_DEVICE" > $ifcfg
-[ -n "$NET_DEVICE" ] && \
-  echo "HWADDR=$NET_DEVICE" >> $ifcfg
-echo "BOOTPROTO=$IP_METHOD" >> $ifcfg
-echo "DHCP_HOSTNAME=$HOST_NAME.$DOMAIN_NAME" >> $ifcfg
-
-if [ -n "$WLAN_SSID$WLAN_IWCONFIG$WLAN_MODE" ]; then
-    [ -n "$WLAN_SSID" ] && \
-      echo "ESSID=$WLAN_SSID" >> $ifcfg
-    [ -n "$WLAN_IWCONFIG" ] && \
-      echo "IWCONFIG=$WLAN_IWCONFIG" >> $ifcfg
-    [ -n "$WLAN_MODE" ] && \
-      echo "MODE=$WLAN_MODE" >> $ifcfg
-fi
-
-if [ "$IP_METHOD" = "static" ]; then
-    echo "IPADDR=$IP_ADDRESS" >> $ifcfg
-    echo "NETMASK=$IP_NETMASK" >> $ifcfg
-    echo "GATEWAY=$IP_GATEWAY" >> $ifcfg
-
-    /bin/hostname "$HOST_NAME.$DOMAIN_NAME"
-
-    if [[ -z "$IP_DNS1" ]]; then
-       echo $(date "+%H:%M:%S") " pl_netinit: no dns server specified, cannot continue."
-       net_init_failed
-    fi
-
-    echo "nameserver $IP_DNS1" > /etc/resolv.conf
-    if [[ -n "$IP_DNS2" ]]; then
-       echo "nameserver $IP_DNS2" >> /etc/resolv.conf
-    fi
-fi
-
+}
 
-echo $(date "+%H:%M:%S") " pl_netinit: attempting to bring up device $ETH_DEVICE"
-/sbin/ifup $ETH_DEVICE
+echo $(date "+%H:%M:%S") " pl_netinit: attempting to start networking"
+/sbin/service network start
 
 # for backwards compatibility
 /sbin/ifconfig $ETH_DEVICE > $IFCONFIG_OUTPUT
 
 echo $(date "+%H:%M:%S") " pl_netinit: network online"
-