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