4258fbae7ba9faeb7c451e30b96ca3a3b50c9740
[live-boot-grml.git] / scripts / init-premount / select_eth_device
1 #!/bin/sh
2
3 # Original script by Andreas Teuchert <ant+dl@hsg-kl.de>
4 # Modified by Frédéric Boiteux <fboiteux@calistel.com>
5
6 PREREQ="blacklist udev"
7
8 prereqs()
9 {
10         echo "$PREREQ"
11 }
12
13 case $1 in
14 # get pre-requisites
15 prereqs)
16         prereqs
17         exit 0
18         ;;
19 esac
20
21 # Boot type in initramfs's config
22 bootconf=$(egrep '^BOOT=' /conf/initramfs.conf | tail -1)
23
24 # can be superseded by command line (used by Debian-Live's netboot for example)
25 for ARGUMENT in $(cat /proc/cmdline); do
26         case "${ARGUMENT}" in
27                 netboot=*)
28                         NETBOOT="${ARGUMENT#netboot=}"
29                         ;;
30         esac
31 done
32
33 if [ "$bootconf" != "BOOT=nfs" ] &&
34    [ "$NETBOOT" = "" ] &&
35    [ "$FETCH" = "" ] &&
36    [ "$FTPFS" = "" ] &&
37    [ "$HTTPFS" = "" ]
38 then
39     # Not a net boot : nothing to do
40     exit 0
41 fi
42
43 # be sure this has been run (*should* be done by scripts/init-premount/udev)
44 udevadm trigger
45 udevadm settle
46
47 # we want to do some basic IP
48 modprobe -q af_packet
49
50 # Available Ethernet interfaces ?
51 l_interfaces=""
52 echo "Waiting for ethernet card(s) up... If this fails, maybe the ethernet card is not supported by the kernel `uname -r`?"
53 while [ -z "$l_interfaces" ]; do
54   l_interfaces="$(cd /sys/class/net/ && ls -d eth* 2>/dev/null)"
55 done
56
57 if [ $(echo $l_interfaces | wc -w) -lt 2 ]; then
58         # only one interface : no choice
59         echo "DEVICE=$l_interfaces" >> /conf/param.conf
60         exit 0
61 fi
62
63 # If user force to use specific device, write it
64 for ARGUMENT in $(cat /proc/cmdline); do
65     case "${ARGUMENT}" in
66         live-netdev=*)
67         NETDEV="${ARGUMENT#live-netdev=}"
68         echo "DEVICE=$NETDEV" >> /conf/param.conf
69         echo "Found live-netdev parameter in /proc/cmdline. Force to use network device $NETDEV."
70         exit 0
71         ;;
72     esac
73 done
74
75 while true; do
76         echo -n "Looking for a connected Ethernet interface ..."
77
78         for interface in $l_interfaces; do
79                 # ATTR{carrier} is not set if this is not done
80                 echo -n " $interface ?"
81                 ipconfig -c none -d $interface -t 1 >/dev/null 2>&1
82         done
83
84         echo ''
85
86         for step in 1 2 3 4 5; do
87                 for interface in $l_interfaces; do
88                         carrier=$(cat /sys/class/net/$interface/carrier \
89                                 2>/dev/null)
90                         # link detected
91                         if [ "$carrier" = 1 ]; then
92                                 echo " found $interface."
93                                 # inform initrd's init script :
94                                 echo "DEVICE=$interface" >> /conf/param.conf
95                                 exit 0
96                         fi
97                 done
98                 # wait a bit
99                 sleep 1
100         done
101 done