f08e1332e8cd12c93becc642b9795f21750901da
[live-boot-grml.git] / debian / patches / 27_support_static_ip.dpatch
1 #! /bin/sh /usr/share/dpatch/dpatch-run
2 ## 27_support_staticip.dpatch by  <mru@grml.org>
3 ##
4 ## All lines beginning with `## DP:' are a description of the patch.
5 ## DP: No description.
6
7 @DPATCH@
8
9 diff --git a/scripts/live b/scripts/live
10 index 5681362..bf20cce 100755
11 --- a/scripts/live
12 +++ b/scripts/live
13 @@ -91,9 +91,8 @@ Arguments ()
14                                 ;;
15
16                         ethdevice=*)
17 -                               DEVICE="${ARGUMENT#ethdevice=}"
18 -                               ETHDEVICE="${DEVICE}"
19 -                               export DEVICE ETHDEVICE
20 +                               ETHDEVICE="${ARGUMENT#ethdevice=}"
21 +                               export ETHDEVICE
22                                 ;;
23
24                         ethdevice-timeout=*)
25 @@ -150,13 +149,9 @@ Arguments ()
26                                 ;;
27
28                         ip=*)
29 -                               STATICIP="${ARGUMENT#ip=}"
30 -
31 -                               if [ -z "${STATICIP}" ]
32 -                               then
33 -                                       STATICIP="frommedia"
34 -                               fi
35 -
36 +                               # copy complete ip=args into staticip, and
37 +                               # keep multiple uses.
38 +                               STATICIP="${STATICIP} ${ARGUMENT}"
39                                 export STATICIP
40                                 ;;
41
42 @@ -598,6 +593,24 @@ copy_live_to ()
43         return 0
44  }
45
46 +get_ipconfig_para()
47 +{
48 +       if [ $# != 1 ] ; then
49 +               echo "Missin parameter for $0"
50 +               return
51 +       fi
52 +       devname=$1
53 +       for ip in ${STATICIP} ; do
54 +               case $ip in
55 +                       *:$devname:*)
56 +                       echo $ip
57 +                       return
58 +                       ;;
59 +               esac
60 +       done
61 +       echo $devname
62 +}
63 +
64  do_netsetup ()
65  {
66         modprobe -q af_packet # For DHCP
67 @@ -608,9 +621,19 @@ do_netsetup ()
68         [ -n "$ETHDEV_TIMEOUT" ] || ETHDEV_TIMEOUT=15
69         echo "Using timeout of $ETHDEV_TIMEOUT seconds for network configuration."
70
71 -       if [ -z "${NETBOOT}" ] && [ -z "${FETCH}" ] && \
72 -          [ -z "${HTTPFS}" ] && [ -z "${FTPFS}" ]
73 -       then
74 +       # Our modus operandi for getting a working network setup is this:
75 +       # * If ip=* is set, pass that to ipconfig and be done
76 +       # * Else, try dhcp on all devices in this order:
77 +       #   ethdevice= bootif= <all interfaces>
78 +
79 +       ALLDEVICES="$(cd /sys/class/net/ && ls -1 2>/dev/null | grep -v '^lo$' )"
80 +
81 +       # Turn on all interfaces before doing anything, to avoid timing problems
82 +       # during link negotiation.
83 +       echo "Net: Turning on all device links..."
84 +       for device in ${ALLDEVICES}; do
85 +               ipconfig -c none -d $device -t 1 2>/dev/null >/dev/null
86 +       done
87
88
89         # support for Syslinux IPAPPEND parameter
90 @@ -653,7 +676,7 @@ do_netsetup ()
91
92                                 if [ "$bootif_mac" = "$current_mac" ]
93                                 then
94 -                                       DEVICE=${device##*/}
95 +                                       ETHDEVICE="${device##*/},$ETHDEVICE" # use ethdevice
96                                         break
97                                 fi
98                         fi
99 @@ -665,12 +688,7 @@ do_netsetup ()
100         # for *every* present network device (except for loopback of course)
101         if [ -z "$ETHDEVICE" ] ; then
102                 echo "If you want to boot from a specific device use bootoption ethdevice=..."
103 -               for device in /sys/class/net/*; do
104 -                       dev=${device##*/} ;
105 -                       if [ "$dev" != "lo" ] ; then
106 -                               ETHDEVICE="$ETHDEVICE $dev"
107 -                       fi
108 -               done
109 +        ETHDEVICE="$ALLDEVICES"
110         fi
111
112         # split args of ethdevice=eth0,eth1 into "eth0 eth1"
113 @@ -678,38 +696,27 @@ do_netsetup ()
114                 devlist="$devlist $device"
115         done
116
117 -       # this is tricky (and ugly) because ipconfig sometimes just hangs/runs into
118 -       # an endless loop; iff execution fails give it two further tries, that's
119 -       # why we use '$devlist $devlist $devlist' for the other for loop
120 -       for dev in $devlist $devlist $devlist ; do
121 -               echo "Executing ipconfig -t $ETHDEV_TIMEOUT $dev"
122 -               ipconfig -t "$ETHDEV_TIMEOUT" $dev | tee -a /netboot.config &
123 -               jobid=$!
124 -               sleep "$ETHDEV_TIMEOUT" ; sleep 1
125 -               if [ -r /proc/"$jobid"/status ] ; then
126 -                       echo "Killing job $jobid for device $dev as ipconfig ran into recursion..."
127 -                       kill -9 $jobid
128 +       for dev in $devlist ; do
129 +               param="$(get_ipconfig_para $dev)"
130 +               if [ -n "$NODHCP" ] && [ "$param" = "$dev" ] ; then
131 +                       echo "Ignoring network device $dev due to nodhcp." | tee -a /live-boot.log
132 +                       continue
133                 fi
134 +               echo "Executing ipconfig -t $ETHDEV_TIMEOUT $param"
135 +               ipconfig -t "$ETHDEV_TIMEOUT" "$param" | tee -a /netboot.config
136
137                 # if configuration of device worked we should have an assigned
138                 # IP address, iff so let's use the according as $DEVICE for later usage
139                 # simple and primitive approach which seems to work fine
140 -               if ifconfig $dev | grep -q 'inet.*addr:' ; then
141 -                       export DEVICE="$dev"
142 -                       break
143 +               IPV4ADDR="0.0.0.0"
144 +               . /tmp/net-${device}.conf
145 +               if [ "${IPV4ADDR}" != "0.0.0.0" ]; then
146 +                       export DEVICE="$dev $DEVICE"
147 +                       # break  # exit loop as we just use the irst
148                 fi
149 -       done
150
151 -       else
152 -               for interface in ${DEVICE}; do
153 -                       ipconfig -t "$ETHDEV_TIMEOUT" ${interface} | tee /netboot-${interface}.config
154 -                       [ -e /tmp/net-${interface}.conf ] && . /tmp/net-${interface}.conf
155 -                       if [ "$IPV4ADDR" != "0.0.0.0" ]
156 -                       then
157 -                               break
158 -                       fi
159 -               done
160 -       fi
161 +       done
162 +       unset devlist
163
164         for interface in ${DEVICE}; do
165                 # source relevant ipconfig output