Adding debian version 4.0~a1-1.
[live-boot-grml.git] / scripts / boot / 9990-networking.sh
1 #!/bin/sh
2
3 #set -e
4
5 do_netsetup ()
6 {
7         modprobe -q af_packet # For DHCP
8
9         udevadm trigger
10         udevadm settle
11
12         [ -n "$ETHDEV_TIMEOUT" ] || ETHDEV_TIMEOUT=15
13         echo "Using timeout of $ETHDEV_TIMEOUT seconds for network configuration."
14
15         if [ -z "${NETBOOT}" ] && [ -z "${FETCH}" ] && [ -z "${HTTPFS}" ] && [ -z "${FTPFS}" ]
16         then
17                 # support for Syslinux IPAPPEND parameter
18                 # it sets the BOOTIF variable on the kernel parameter
19
20                 if [ -n "${BOOTIF}" ]
21                 then
22                         # pxelinux sets BOOTIF to a value based on the mac address of the
23                         # network card used to PXE boot, so use this value for DEVICE rather
24                         # than a hard-coded device name from initramfs.conf. this facilitates
25                         # network booting when machines may have multiple network cards.
26                         # pxelinux sets BOOTIF to 01-$mac_address
27
28                         # strip off the leading "01-", which isn't part of the mac
29                         # address
30                         temp_mac=${BOOTIF#*-}
31
32                         # convert to typical mac address format by replacing "-" with ":"
33                         bootif_mac=""
34                         IFS='-'
35                         for x in $temp_mac
36                         do
37                                 if [ -z "$bootif_mac" ]
38                                 then
39                                         bootif_mac="$x"
40                                 else
41                                         bootif_mac="$bootif_mac:$x"
42                                 fi
43                         done
44                         unset IFS
45
46                         # look for devices with matching mac address, and set DEVICE to
47                         # appropriate value if match is found.
48
49                         for device in /sys/class/net/*
50                         do
51                                 if [ -f "$device/address" ]
52                                 then
53                                         current_mac=$(cat "$device/address")
54
55                                         if [ "$bootif_mac" = "$current_mac" ]
56                                         then
57                                                 DEVICE=${device##*/}
58                                                 break
59                                         fi
60                                 fi
61                         done
62                 fi
63
64                 # if ethdevice was not specified on the kernel command line
65                 # make sure we try to get a working network configuration
66                 # for *every* present network device (except for loopback of course)
67                 if [ -z "$ETHDEVICE" ]
68                 then
69                         echo "If you want to boot from a specific device use bootoption ethdevice=..."
70                         for device in /sys/class/net/*
71                         do
72                                 dev=${device##*/}
73                                 if [ "$dev" != "lo" ]
74                                 then
75                                         ETHDEVICE="$ETHDEVICE $dev"
76                                 fi
77                         done
78                 fi
79
80                 # split args of ethdevice=eth0,eth1 into "eth0 eth1"
81                 for device in $(echo $ETHDEVICE | sed 's/,/ /g')
82                 do
83                         devlist="$devlist $device"
84                 done
85
86                 # this is tricky (and ugly) because ipconfig sometimes just hangs/runs into
87                 # an endless loop; if execution fails give it two further tries, that's
88                 # why we use '$devlist $devlist $devlist' for the other for loop
89                 for dev in $devlist $devlist $devlist
90                 do
91                         echo "Executing ipconfig -t $ETHDEV_TIMEOUT $dev"
92                         ipconfig -t "$ETHDEV_TIMEOUT" $dev | tee -a /netboot.config &
93                         jobid=$!
94                         sleep "$ETHDEV_TIMEOUT" ; sleep 1
95                         if [ -r /proc/"$jobid"/status ]
96                         then
97                                 echo "Killing job $jobid for device $dev as ipconfig ran into recursion..."
98                                 kill -9 $jobid
99                         fi
100
101                         # if configuration of device worked we should have an assigned
102                         # IP address, if so let's use the device as $DEVICE for later usage.
103                         # simple and primitive approach which seems to work fine
104                         if ifconfig $dev | grep -q 'inet.*addr:'
105                         then
106                                 export DEVICE="$dev"
107                                 break
108                         fi
109                 done
110         else
111                 for interface in ${DEVICE}; do
112                         ipconfig -t "$ETHDEV_TIMEOUT" ${interface} | tee /netboot-${interface}.config
113
114                         [ -e /run/net-${interface}.conf ] && . /run/net-${interface}.conf
115
116                         if [ "$IPV4ADDR" != "0.0.0.0" ]
117                         then
118                                 break
119                         fi
120                 done
121         fi
122
123         for interface in ${DEVICE}
124         do
125                 # source relevant ipconfig output
126                 OLDHOSTNAME=${HOSTNAME}
127
128                 [ -e /run/net-${interface}.conf ] && . /run/net-${interface}.conf
129
130                 [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
131                 export HOSTNAME
132
133                 if [ -n "${interface}" ]
134                 then
135                         HWADDR="$(cat /sys/class/net/${interface}/address)"
136                 fi
137
138                 if [ ! -e "/etc/resolv.conf" ]
139                 then
140                         echo "Creating /etc/resolv.conf"
141
142                         if [ -n "${DNSDOMAIN}" ]
143                         then
144                                 echo "domain ${DNSDOMAIN}" > /etc/resolv.conf
145                                 echo "search ${DNSDOMAIN}" >> /etc/resolv.conf
146                         fi
147
148                         for i in ${IPV4DNS0} ${IPV4DNS1} ${IPV4DNS1}
149                         do
150                                 if [ -n "$i" ] && [ "$i" != 0.0.0.0 ]
151                                 then
152                                         echo "nameserver $i" >> /etc/resolv.conf
153                                 fi
154                         done
155                 fi
156
157                 # Check if we have a network device at all
158                 if ! ls /sys/class/net/"$interface" > /dev/null 2>&1 && \
159                    ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
160                    ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
161                    ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
162                    ! ls /sys/class/net/ra0 > /dev/null 2>&1
163                 then
164                         panic "No supported network device found, maybe a non-mainline driver is required."
165                 fi
166         done
167 }