Merge branch 'revert-ef866fdb' into 'master'
[live-boot-grml.git] / components / 9990-select-eth-device.sh
1 #!/bin/sh
2
3 Wait_for_carrier ()
4 {
5         # $1 = network device
6         echo -n "Waiting for link to come up on $1... "
7         ip link set $1 up
8         for step in $(seq 1 15)
9         do
10                 carrier=$(cat /sys/class/net/$1/carrier \
11                 2>/dev/null)
12                 case "${carrier}" in
13                         1)
14                         echo -e "\nLink is up"
15                         return
16                         ;;
17                         *)
18                         # Counter
19                         echo -n "$step "
20                         ;;
21                 esac
22                 sleep 1
23         done
24         echo -e "\nError - carrier not detected on $1."
25         ip link set $1 down
26 }
27
28 Select_eth_device ()
29 {
30         # Boot type in initramfs's config
31         bootconf=$(egrep '^BOOT=' /conf/initramfs.conf | tail -1)
32
33         # can be superseded by command line (used by Debian-Live's netboot for example)
34         for ARGUMENT in ${LIVE_BOOT_CMDLINE}
35         do
36                 case "${ARGUMENT}" in
37                         netboot=*)
38                                 NETBOOT="${ARGUMENT#netboot=}"
39                                 ;;
40                 esac
41         done
42
43         if [ "$bootconf" != "BOOT=nfs" ] && [ -z "$NETBOOT" ] && [ -z "$FETCH" ] && [ -z "$FTPFS" ] && [ -z "$HTTPFS" ]
44         then
45                 # Not a net boot : nothing to do
46                 return
47         fi
48
49         # we want to do some basic IP
50         modprobe -q af_packet
51
52         # Ensure all our net modules get loaded so we can actually compare MAC addresses...
53         udevadm trigger
54         udevadm settle
55
56         # Available Ethernet interfaces ?
57         l_interfaces=""
58
59         # See if we can derive the boot device
60         Device_from_bootif
61
62         if [ -z "$DEVICE" ]
63         then
64                 echo "Waiting for ethernet card(s) up... If this fails, maybe the ethernet card is not supported by the kernel `uname -r`?"
65                 while [ -z "$l_interfaces" ]
66                 do
67                         l_interfaces="$(cd /sys/class/net/ && ls -d * 2>/dev/null | grep -v "lo")"
68                 done
69
70                 if [ $(echo $l_interfaces | wc -w) -lt 2 ]
71                 then
72                         # only one interface : no choice
73                         echo "DEVICE=$l_interfaces" >> /conf/param.conf
74                         Wait_for_carrier $l_interfaces
75                         return
76                 fi
77
78                 # If user force to use specific device, write it
79                 for ARGUMENT in ${LIVE_BOOT_CMDLINE}
80                 do
81                         case "${ARGUMENT}" in
82                                 live-netdev=*)
83                                 NETDEV="${ARGUMENT#live-netdev=}"
84                                 echo "DEVICE=$NETDEV" >> /conf/param.conf
85                                 echo "Found live-netdev parameter, forcing to to use network device $NETDEV."
86                                 Wait_for_carrier $NETDEV
87                                 return
88                                 ;;
89                         esac
90                 done
91         else
92                 l_interfaces="$DEVICE"
93         fi
94
95         found_eth_dev=""
96         while true
97         do
98                 echo -n "Looking for a connected Ethernet interface ..."
99
100                 for interface in $l_interfaces
101                 do
102                         # ATTR{carrier} is not set if this is not done
103                         echo -n " $interface ?"
104                         ipconfig -c none -d $interface -t 1 >/dev/null 2>&1
105                         sleep 1
106                 done
107
108                 echo ''
109
110                 for step in 1 2 3 4 5
111                 do
112                         for interface in $l_interfaces
113                         do
114                                 ip link set $interface up
115                                 carrier=$(cat /sys/class/net/$interface/carrier \
116                                         2>/dev/null)
117                                 # link detected
118
119                                 case "${carrier}" in
120                                         1)
121                                                 echo "Connected $interface found"
122                                                 # inform initrd's init script :
123                                                 found_eth_dev="$found_eth_dev $interface"
124                                                 found_eth_dev="$(echo $found_eth_dev | sed -e "s/^[[:space:]]*//g")"
125                                                 ;;
126                                 esac
127                         done
128                         if [ -n "$found_eth_dev" ]
129                         then
130                                 echo "DEVICE='$found_eth_dev'" >> /conf/param.conf
131                                 return
132                         else
133                                 # wait a bit
134                                 sleep 1
135                         fi
136                 done
137         done
138 }