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