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