Releasing debian version 4.0.1-1.
[live-boot.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 # squeeze
115 [ -e /tmp/net-${interface}.conf ] && . /tmp/net-${interface}.conf
116
117 # wheezy
118 [ -e /run/net-${interface}.conf ] && . /run/net-${interface}.conf
119
120 if [ "$IPV4ADDR" != "0.0.0.0" ]
121 then
122 break
123 fi
124 done
125 fi
126
127 for interface in ${DEVICE}
128 do
129 # source relevant ipconfig output
130 OLDHOSTNAME=${HOSTNAME}
131
132 # squeeze
133 [ -e /tmp/net-${interface}.conf ] && . /tmp/net-${interface}.conf
134
135 # wheezy
136 [ -e /run/net-${interface}.conf ] && . /run/net-${interface}.conf
137
138 [ -z ${HOSTNAME} ] && HOSTNAME=${OLDHOSTNAME}
139 export HOSTNAME
140
141 if [ -n "${interface}" ]
142 then
143 HWADDR="$(cat /sys/class/net/${interface}/address)"
144 fi
145
146 if [ ! -e "/etc/resolv.conf" ]
147 then
148 echo "Creating /etc/resolv.conf"
149
150 if [ -n "${DNSDOMAIN}" ]
151 then
152 echo "domain ${DNSDOMAIN}" > /etc/resolv.conf
153 echo "search ${DNSDOMAIN}" >> /etc/resolv.conf
154 fi
155
156 for i in ${IPV4DNS0} ${IPV4DNS1} ${IPV4DNS1}
157 do
158 if [ -n "$i" ] && [ "$i" != 0.0.0.0 ]
159 then
160 echo "nameserver $i" >> /etc/resolv.conf
161 fi
162 done
163 fi
164
165 # Check if we have a network device at all
166 if ! ls /sys/class/net/"$interface" > /dev/null 2>&1 && \
167 ! ls /sys/class/net/eth0 > /dev/null 2>&1 && \
168 ! ls /sys/class/net/wlan0 > /dev/null 2>&1 && \
169 ! ls /sys/class/net/ath0 > /dev/null 2>&1 && \
170 ! ls /sys/class/net/ra0 > /dev/null 2>&1
171 then
172 panic "No supported network device found, maybe a non-mainline driver is required."
173 fi
174 done
175 }