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