432388083a2480018b3621aa4d46028d12c60715
[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" ] && [ "$NETBOOT" = "" ]; then
34         # Not a net boot : nothing to do
35         exit 0
36 fi
37
38 # be sure this has been run (*should* be done by scripts/init-premount/udev)
39 if [ -x /sbin/udevadm ]
40 then
41         # lenny
42         udevadm trigger
43         udevadm settle
44 else
45         # etch
46         udevtrigger
47         udevsettle
48 fi
49
50 # we want to do some basic IP
51 modprobe -q af_packet
52
53 # Available Ethernet interfaces ?
54 l_interfaces=$(cd /sys/class/net/ && ls -d eth* 2>/dev/null)
55
56 if [ $(echo $l_interfaces | wc -w) -lt 2 ]; then
57         # only one interface : no choice
58         echo "DEVICE=$l_interfaces" >> /conf/param.conf
59         exit 0
60 fi
61
62 while true; do
63         echo -n "Looking for a connected Ethernet interface ..."
64
65         for interface in $l_interfaces; do
66                 # ATTR{carrier} is not set if this is not done
67                 echo -n " $interface ?"
68                 ipconfig -c none -d $interface -t 1 >/dev/null 2>&1
69         done
70
71         echo ''
72
73         for step in 1 2 3 4 5; do
74                 for interface in $l_interfaces; do
75                         carrier=$(cat /sys/class/net/$interface/carrier \
76                                 2>/dev/null)
77                         # link detected
78                         if [ "$carrier" = 1 ]; then
79                                 echo " found $interface."
80                                 # inform initrd's init script :
81                                 echo "DEVICE=$interface" >> /conf/param.conf
82                                 exit 0
83                         fi
84                 done
85                 # wait a bit
86                 sleep 1
87         done
88 done