Update 9990-select-eth-device.sh
[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
85                             # Check if NETDEV is a valid MAC address
86                             if echo "$NETDEV" | grep -Eq '^[0-9A-Fa-f]{2}[:-]([0-9A-Fa-f]{2}[:-]){4}[0-9A-Fa-f]{2}$'; then
87                               echo "NETDEV is a valid MAC address."
88
89                               # Retrieve the device name associated with the MAC address
90                               DEVICE_NAME=$(ip -o link | awk -v mac="$NETDEV" '$0 ~ mac{print substr($2, 1, length($2)-1)}')
91                               if [ -n "$DEVICE_NAME" ]; then
92                                 echo "Device name for MAC address $NETDEV is $DEVICE_NAME."
93                                 NETDEV="$DEVICE_NAME"
94                               fi
95                             else
96                               echo "NETDEV is not a valid MAC address. Assuming it is a device name."
97                               # Assign NETDEV directly to $NETDEV
98                               NETDEV="$NETDEV"
99                             fi
100                             echo "DEVICE=$NETDEV" >> /conf/param.conf
101                             echo "Found live-netdev parameter, forcing it to use network device $NETDEV."
102                             Wait_for_carrier "$NETDEV"
103                             return
104                             ;;
105                         esac
106                 done
107         else
108                 l_interfaces="$DEVICE"
109         fi
110
111         found_eth_dev=""
112         while true
113         do
114                 echo -n "Looking for a connected Ethernet interface ..."
115
116                 for interface in $l_interfaces
117                 do
118                         # ATTR{carrier} is not set if this is not done
119                         echo -n " $interface ?"
120                         ipconfig -c none -d $interface -t 1 >/dev/null 2>&1
121                         sleep 1
122                 done
123
124                 echo ''
125
126                 for step in 1 2 3 4 5
127                 do
128                         for interface in $l_interfaces
129                         do
130                                 ip link set $interface up
131                                 carrier=$(cat /sys/class/net/$interface/carrier \
132                                         2>/dev/null)
133                                 # link detected
134
135                                 case "${carrier}" in
136                                         1)
137                                                 echo "Connected $interface found"
138                                                 # inform initrd's init script :
139                                                 found_eth_dev="$found_eth_dev $interface"
140                                                 found_eth_dev="$(echo $found_eth_dev | sed -e "s/^[[:space:]]*//g")"
141                                                 ;;
142                                 esac
143                         done
144                         if [ -n "$found_eth_dev" ]
145                         then
146                                 echo "DEVICE='$found_eth_dev'" >> /conf/param.conf
147                                 return
148                         else
149                                 # wait a bit
150                                 sleep 1
151                         fi
152                 done
153         done
154 }