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