Drop nameserver handling from ip= boot parameter.
[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                                         ETHDEVICE="${device##*/},$ETHDEVICE" # use ethdevice
48                                         break
49                                 fi
50                         fi
51                 done
52         fi
53 }
54
55 get_ipconfig_para()
56 {
57         if [ $# != 1 ] ; then
58                 echo "Missin parameter for $0"
59                 return
60         fi
61         devname=$1
62         for ip in ${STATICIP} ; do
63                 case $ip in
64                         *:$devname:*)
65                         echo $ip
66                         return
67                         ;;
68                 esac
69         done
70         echo $devname
71 }
72
73 do_netsetup ()
74 {
75         modprobe -q af_packet # For DHCP
76
77         udevadm trigger
78         udevadm settle
79
80         [ -n "$ETHDEV_TIMEOUT" ] || ETHDEV_TIMEOUT=15
81         echo "Using timeout of $ETHDEV_TIMEOUT seconds for network configuration."
82
83         # Our modus operandi for getting a working network setup is this:
84         # * If ip=* is set, pass that to ipconfig and be done
85         # * Else, try dhcp on all devices in this order:
86         #   ethdevice= bootif= <all interfaces>
87
88         ALLDEVICES="$(cd /sys/class/net/ && ls -1 2>/dev/null | grep -v '^lo$' )"
89
90         # Turn on all interfaces before doing anything, to avoid timing problems
91         # during link negotiation.
92         echo "Net: Turning on all device links..."
93         for device in ${ALLDEVICES}; do
94                 ipconfig -c none -d $device -t 1 2>/dev/null >/dev/null
95         done
96
97                 # See if we can select the device from BOOTIF
98                 Device_from_bootif
99
100                 # if ethdevice was not specified on the kernel command line
101                 # make sure we try to get a working network configuration
102                 # for *every* present network device (except for loopback of course)
103                 if [ -z "$ETHDEVICE" ]
104                 then
105                         echo "If you want to boot from a specific device use bootoption ethdevice=..."
106                         ETHDEVICE="$ALLDEVICES"
107                 fi
108
109                 # split args of ethdevice=eth0,eth1 into "eth0 eth1"
110                 for device in $(echo $ETHDEVICE | sed 's/,/ /g')
111                 do
112                         devlist="$devlist $device"
113                 done
114
115                 for dev in $devlist ; do
116                         param="$(get_ipconfig_para $dev)"
117                         if [ -n "$NODHCP" ] && [ "$param" = "$dev" ] ; then
118                                 echo "Ignoring network device $dev due to nodhcp." | tee -a /boot.log
119                                 continue
120                         fi
121                         echo "Executing ipconfig -t $ETHDEV_TIMEOUT $param"
122                         ipconfig -t "$ETHDEV_TIMEOUT" "$param" | tee -a /netboot.config
123
124                         # if configuration of device worked we should have an assigned
125                         # IP address, if so let's use the device as $DEVICE for later usage.
126                         # simple and primitive approach which seems to work fine
127
128                         IPV4ADDR="0.0.0.0"
129                         if [ -e "/run/net-${device}.conf" ]; then
130                                 . /run/net-${device}.conf
131                         fi
132                         if [ "${IPV4ADDR}" != "0.0.0.0" ]; then
133                                 export DEVICE="$dev $DEVICE"
134                                 # break  # exit loop as we just use the irst
135                         fi
136                 done
137         unset devlist
138
139         for interface in ${DEVICE}
140         do
141                 # source relevant ipconfig output
142                 OLDHOSTNAME=${HOSTNAME}
143
144                 [ -e /run/net-${interface}.conf ] && . /run/net-${interface}.conf
145
146                 [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
147                 export HOSTNAME
148
149                 if [ -n "${interface}" ]
150                 then
151                         HWADDR="$(cat /sys/class/net/${interface}/address)"
152                 fi
153
154                 if [ ! -e "/etc/resolv.conf" ]
155                 then
156                         echo "Creating /etc/resolv.conf"
157
158                         if [ -n "${DNSDOMAIN}" ]
159                         then
160                                 echo "domain ${DNSDOMAIN}" > /etc/resolv.conf
161                                 echo "search ${DNSDOMAIN}" >> /etc/resolv.conf
162                         fi
163
164                         for i in ${IPV4DNS0} ${IPV4DNS1} ${IPV4DNS1} ${DNSSERVER1} ${DNSSERVER2}
165                         do
166                                 if [ -n "$i" ] && [ "$i" != 0.0.0.0 ]
167                                 then
168                                         echo "nameserver $i" >> /etc/resolv.conf
169                                 fi
170                         done
171                 fi
172
173                 # Check if we have a network device at all
174                 if ! ls /sys/class/net/"$interface" > /dev/null 2>&1 && \
175                    ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
176                    ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
177                    ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
178                    ! ls /sys/class/net/ra0 > /dev/null 2>&1
179                 then
180                         panic "No supported network device found, maybe a non-mainline driver is required."
181                 fi
182         done
183 }