Adding upstream version 3.0~a37.
[live-boot-grml.git] / scripts / boot / 9990-select-eth-device.sh
1 #!/bin/sh
2
3 Select_eth_device ()
4 {
5         # Boot type in initramfs's config
6         bootconf=$(egrep '^BOOT=' /conf/initramfs.conf | tail -1)
7
8         # can be superseded by command line (used by Debian-Live's netboot for example)
9         for ARGUMENT in ${_CMDLINE}
10         do
11                 case "${ARGUMENT}" in
12                         netboot=*)
13                                 NETBOOT="${ARGUMENT#netboot=}"
14                                 ;;
15                 esac
16         done
17
18         if [ "$bootconf" != "BOOT=nfs" ] && [ -z "$NETBOOT" ] && [ -z "$FETCH" ] && [ -z "$FTPFS" ] && [ -z "$HTTPFS" ]
19         then
20                 # Not a net boot : nothing to do
21                 return
22         fi
23
24         # we want to do some basic IP
25         modprobe -q af_packet
26
27         # Available Ethernet interfaces ?
28         l_interfaces=""
29         echo "Waiting for ethernet card(s) up... If this fails, maybe the ethernet card is not supported by the kernel `uname -r`?"
30         while [ -z "$l_interfaces" ]
31         do
32                 l_interfaces="$(cd /sys/class/net/ && ls -d eth* 2>/dev/null)"
33         done
34
35         if [ $(echo $l_interfaces | wc -w) -lt 2 ]
36         then
37                 # only one interface : no choice
38                 echo "DEVICE=$l_interfaces" >> /conf/param.conf
39                 return
40         fi
41
42         # If user force to use specific device, write it
43         for ARGUMENT in ${_CMDLINE}
44         do
45                 case "${ARGUMENT}" in
46                         live-netdev=*)
47                                 NETDEV="${ARGUMENT#live-netdev=}"
48                                 echo "DEVICE=$NETDEV" >> /conf/param.conf
49                                 echo "Found live-netdev parameter, forcing to to use network device $NETDEV."
50                                 return
51                                 ;;
52                 esac
53         done
54
55         found_eth_dev=""
56         while true
57         do
58                 echo -n "Looking for a connected Ethernet interface ..."
59
60                 for interface in $l_interfaces
61                 do
62                         # ATTR{carrier} is not set if this is not done
63                         echo -n " $interface ?"
64                         ipconfig -c none -d $interface -t 1 >/dev/null 2>&1
65                 done
66
67                 echo ''
68
69                 for step in 1 2 3 4 5
70                 do
71                         for interface in $l_interfaces
72                         do
73                                 carrier=$(cat /sys/class/net/$interface/carrier \
74                                         2>/dev/null)
75                                 # link detected
76
77                                 case "${carrier}" in
78                                         1)
79                                                 echo "Connected $interface found"
80                                                 # inform initrd's init script :
81                                                 found_eth_dev="$found_eth_dev $interface"
82                                                 ;;
83                                 esac
84                         done
85                         if [ -n "$found_eth_dev" ]
86                         then
87                                 echo "DEVICE='$found_eth_dev'" >> /conf/param.conf
88                                 return
89                         else
90                                 # wait a bit
91                                 sleep 1
92                         fi
93                 done
94         done
95 }