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