133d4b1ece44ec8675a6c89e361a09ddaf11fef3
[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         # Ensure all our net modules get loaded so we can actually compare MAC addresses...
28         udevadm trigger
29         udevadm settle
30
31         # Available Ethernet interfaces ?
32         l_interfaces=""
33
34         # See if we can derive the boot device
35         Device_from_bootif
36
37         if [ -z "$DEVICE" ]
38         then
39                 echo "Waiting for ethernet card(s) up... If this fails, maybe the ethernet card is not supported by the kernel `uname -r`?"
40                 while [ -z "$l_interfaces" ]
41                 do
42                         l_interfaces="$(cd /sys/class/net/ && ls -d * 2>/dev/null | grep -v "lo")"
43                 done
44
45                 if [ $(echo $l_interfaces | wc -w) -lt 2 ]
46                 then
47                         # only one interface : no choice
48                         echo "DEVICE=$l_interfaces" >> /conf/param.conf
49                         return
50                 fi
51
52                 # If user force to use specific device, write it
53                 for ARGUMENT in ${LIVE_BOOT_CMDLINE}
54                 do
55                         case "${ARGUMENT}" in
56                                 live-netdev=*)
57                                 NETDEV="${ARGUMENT#live-netdev=}"
58                                 # net device could be specified by MAC address
59                                 hex="[0-9A-Fa-f][0-9A-Fa-f]"
60                                 case "${NETDEV}" in
61                                     ${hex}:${hex}:${hex}:${hex}:${hex}:${hex})
62                                         # MAC address; record it and select later
63                                         netdev_mac_addr="${NETDEV}"
64                                         ;;
65                                     *)
66                                         # interface name
67                                         echo "DEVICE=$NETDEV" >> /conf/param.conf
68                                         echo "Found live-netdev parameter, forcing to to use network device $NETDEV."
69                                         return
70                                         ;;
71                                 esac
72                                 ;;
73                         esac
74                 done
75         else
76                 l_interfaces="$DEVICE"
77         fi
78
79         found_eth_dev=""
80         while true
81         do
82                 echo -n "Looking for a connected Ethernet interface ..."
83
84                 for interface in $l_interfaces
85                 do
86                         # ATTR{carrier} is not set if this is not done
87                         echo -n " $interface ?"
88                         ipconfig -c none -d $interface -t 1 >/dev/null 2>&1
89                 done
90
91                 echo ''
92
93                 for step in 1 2 3 4 5
94                 do
95                         for interface in $l_interfaces
96                         do
97                             if [ -z "$netdev_mac_addr" ]; then
98                                 carrier=$(cat /sys/class/net/$interface/carrier \
99                                         2>/dev/null)
100                                 # link detected
101
102                                 case "${carrier}" in
103                                         1)
104                                                 echo "Connected $interface found"
105                                                 # inform initrd's init script :
106                                                 found_eth_dev="$found_eth_dev $interface"
107                                                 ;;
108                                 esac
109                             else
110                                 # MAC addr given, check for that
111                                 mac_addr=$(ifconfig "$interface" \
112                                                   | grep HWaddr \
113                                                   | { read _ _ _ _ mac_addr; echo $mac_addr; })
114                                 if [ "$mac_addr" = "$netdev_mac_addr" ]; then
115                                     found_eth_dev="$interface"
116                                 fi
117                             fi
118                         done
119                         if [ -n "$found_eth_dev" ]
120                         then
121                                 echo "DEVICE='$found_eth_dev'" >> /conf/param.conf
122                                 return
123                         else
124                                 # wait a bit
125                                 sleep 1
126                         fi
127                 done
128         done
129 }