6ab8bfe40ad0c7d67fc5c89d6d38335d8d729a30
[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
7
8 PREREQ="blacklist udev"
9
10 prereqs()
11 {
12         echo "$PREREQ"
13 }
14
15 case $1 in
16 # get pre-requisites
17 prereqs)
18         prereqs
19         exit 0
20         ;;
21 esac
22
23 # Boot type in initramfs's config
24 bootconf=$(egrep '^BOOT=' /conf/initramfs.conf | tail -1)
25
26 # can be superseded by command line (used by Debian-Live's netboot for example)
27 for ARGUMENT in $(cat /proc/cmdline); do
28     case "${ARGUMENT}" in
29         netboot=*)
30         NETBOOT="${ARGUMENT#netboot=}"
31         ;;
32     esac
33 done
34
35 if [ "$bootconf" != "BOOT=nfs" ] && [ "$NETBOOT" = "" ]; then
36     # Not a net boot : nothing to do
37     exit 0
38 fi
39
40 # we want to do some basic IP
41 modprobe -q af_packet
42
43 # Available Ethernet interfaces ?
44 l_interfaces=$(cd /sys/class/net/ && ls -d eth* 2>/dev/null)
45
46 if [ $(echo $l_interfaces | wc -w) -lt 2 ]; then
47     # only one interface : no choice
48     echo "DEVICE=$l_interfaces" >> /conf/param.conf
49     exit 0
50 fi
51
52 while true; do
53         echo -n "Looking for a connected Ethernet interface ..."
54
55         for interface in $l_interfaces; do
56                 # ATTR{carrier} is not set if this is not done
57                 echo -n " $interface ?"
58                 ipconfig -c none -d $interface -t 1 >/dev/null 2>&1
59         done
60         echo ''
61
62         for step in 1 2 3 4 5; do
63                 for interface in $l_interfaces; do
64                         carrier=$(cat /sys/class/net/$interface/carrier \
65                                   2>/dev/null)
66                         # link detected
67                         if [ "$carrier" = 1 ]; then
68                                 echo " found $interface."
69                                 # inform initrd's init script :
70                                 echo "DEVICE=$interface" >> /conf/param.conf
71                                 exit 0
72                         fi
73                 done
74                 # wait a bit
75                 sleep 1
76         done
77 done