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