Patch by Steven Shiau <steven@nchc.org.tw>. The only changes so far are:
[live-boot-grml.git] / components / 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 ${LIVE_BOOT_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
30         # See if we can derive the boot device
31         Device_from_bootif
32
33         if [ -z "$DEVICE" ]
34         then
35                 echo "Waiting for ethernet card(s) up... If this fails, maybe the ethernet card is not supported by the kernel `uname -r`?"
36                 while [ -z "$l_interfaces" ]
37                 do
38                         l_interfaces="$(cd /sys/class/net/ && ls -d eth* 2>/dev/null)"
39                 done
40
41                 if [ $(echo $l_interfaces | wc -w) -lt 2 ]
42                 then
43                         # only one interface : no choice
44                         echo "DEVICE=$l_interfaces" >> /conf/param.conf
45                         return
46                 fi
47
48                 # If user force to use specific device, write it
49                 for ARGUMENT in ${LIVE_BOOT_CMDLINE}
50                 do
51                         case "${ARGUMENT}" in
52                                 live-netdev=*)
53                                 NETDEV="${ARGUMENT#live-netdev=}"
54                                 echo "DEVICE=$NETDEV" >> /conf/param.conf
55                                 echo "Found live-netdev parameter, forcing to to use network device $NETDEV."
56                                 return
57                                 ;;
58                         esac
59                 done
60         else
61                 l_interfaces="$DEVICE"
62         fi
63
64         found_eth_dev=""
65         while true
66         do
67                 echo -n "Looking for a connected Ethernet interface ..."
68
69                 for interface in $l_interfaces
70                 do
71                         # ATTR{carrier} is not set if this is not done
72                         echo -n " $interface ?"
73                         ipconfig -c none -d $interface -t 1 >/dev/null 2>&1
74                 done
75
76                 echo ''
77
78                 for step in 1 2 3 4 5
79                 do
80                         for interface in $l_interfaces
81                         do
82                                 carrier=$(cat /sys/class/net/$interface/carrier \
83                                         2>/dev/null)
84                                 # link detected
85
86                                 case "${carrier}" in
87                                         1)
88                                                 echo "Connected $interface found"
89                                                 # inform initrd's init script :
90                                                 found_eth_dev="$found_eth_dev $interface"
91                                                 ;;
92                                 esac
93                         done
94                         if [ -n "$found_eth_dev" ]
95                         then
96                                 echo "DEVICE='$found_eth_dev'" >> /conf/param.conf
97                                 return
98                         else
99                                 # wait a bit
100                                 sleep 1
101                         fi
102                 done
103         done
104 }