]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/40network/ifup.sh
network/ifup.sh: only use dhcp on unknown interfaces for ip=dhcp
[thirdparty/dracut.git] / modules.d / 40network / ifup.sh
CommitLineData
0ac9584d 1#!/bin/sh
db815843
PS
2#
3# We don't need to check for ip= errors here, that is handled by the
4# cmdline parser script
5#
957bc5c9
DY
6# without $2 means this is for real netroot case
7# or it is for manually bring up network ie. for kdump scp vmcore
fb59f4c9 8PATH=/usr/sbin:/usr/bin:/sbin:/bin
db815843 9
c9f1e3d1 10type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
990e945f 11type ip_to_var >/dev/null 2>&1 || . /lib/net-lib.sh
957bc5c9
DY
12
13# Huh? No $1?
14[ -z "$1" ] && exit 1
15
16# $netif reads easier than $1
17netif=$1
56d60c4b
KY
18use_bridge='false'
19use_vlan='false'
957bc5c9
DY
20
21# enslave this interface to bond?
d136ca4e
HH
22for i in /tmp/bond.*.info; do
23 [ -e "$i" ] || continue
24 unset bondslaves
25 unset bondname
26 . "$i"
957bc5c9
DY
27 for slave in $bondslaves ; do
28 if [ "$netif" = "$slave" ] ; then
29 netif=$bondname
d136ca4e 30 break 2
957bc5c9
DY
31 fi
32 done
d136ca4e 33done
957bc5c9 34
3baa150b
CW
35if [ -e /tmp/team.info ]; then
36 . /tmp/team.info
37 for slave in $teamslaves ; do
38 if [ "$netif" = "$slave" ] ; then
39 netif=$teammaster
40 fi
41 done
42fi
43
0c5ccb35
WC
44if [ -e /tmp/vlan.info ]; then
45 . /tmp/vlan.info
46 if [ "$netif" = "$phydevice" ]; then
47 if [ "$netif" = "$bondname" ] && [ -n "$DO_BOND_SETUP" ] ; then
48 : # We need to really setup bond (recursive call)
49 elif [ "$netif" = "$teammaster" ] && [ -n "$DO_TEAM_SETUP" ] ; then
50 : # We need to really setup team (recursive call)
51 else
52 netif="$vlanname"
53 use_vlan='true'
54 fi
55 fi
56fi
57
957bc5c9
DY
58# bridge this interface?
59if [ -e /tmp/bridge.info ]; then
60 . /tmp/bridge.info
2db6d711 61 for ethname in $bridgeslaves ; do
21928b97
AW
62 if [ "$netif" = "$ethname" ]; then
63 if [ "$netif" = "$bondname" ] && [ -n "$DO_BOND_SETUP" ] ; then
64 : # We need to really setup bond (recursive call)
a55f910c
WC
65 elif [ "$netif" = "$teammaster" ] && [ -n "$DO_TEAM_SETUP" ] ; then
66 : # We need to really setup team (recursive call)
0c5ccb35
WC
67 elif [ "$netif" = "$vlanname" ] && [ -n "$DO_VLAN_SETUP" ]; then
68 : # We need to really setup vlan (recursive call)
21928b97
AW
69 else
70 netif="$bridgename"
56d60c4b 71 use_bridge='true'
21928b97 72 fi
957bc5c9 73 fi
21928b97 74 done
957bc5c9
DY
75fi
76
957bc5c9
DY
77# disable manual ifup while netroot is set for simplifying our logic
78# in netroot case we prefer netroot to bringup $netif automaticlly
5861184e 79[ -n "$2" -a "$2" = "-m" ] && [ -z "$netroot" ] && manualup="$2"
7cca5efd 80
d61a423f
HH
81if [ -n "$manualup" ]; then
82 >/tmp/net.$netif.manualup
7cca5efd 83 rm -f /tmp/net.${netif}.did-setup
d61a423f 84else
43a85a73
HH
85 [ -e /tmp/net.${netif}.did-setup ] && exit 0
86 [ -e /sys/class/net/$netif/address ] && \
87 [ -e /tmp/net.$(cat /sys/class/net/$netif/address).did-setup ] && exit 0
d61a423f 88fi
7e9919b9 89
db815843 90# Run dhclient
7e9919b9 91do_dhcp() {
fb59f4c9 92 # dhclient-script will mark the netif up and generate the online
7e9919b9
DD
93 # event for nfsroot
94 # XXX add -V vendor class and option parsing per kernel
c84618d7 95
2448fbf1
HH
96 local _COUNT=0
97 local _timeout=$(getargs rd.net.timeout.dhcp=)
98 local _DHCPRETRY=$(getargs rd.net.dhcp.retry=)
99 _DHCPRETRY=${_DHCPRETRY:-1}
100
c84618d7
HH
101 [ -e /tmp/dhclient.$netif.pid ] && return 0
102
271cd19d 103 if ! iface_has_link $netif; then
2448fbf1 104 warn "No carrier detected on interface $netif"
271cd19d
HH
105 return 1
106 fi
2448fbf1
HH
107
108 while [ $_COUNT -lt $_DHCPRETRY ]; do
109 info "Starting dhcp for interface $netif"
110 dhclient "$@" \
111 ${_timeout:+-timeout $_timeout} \
93b9885a 112 -q \
2448fbf1
HH
113 -cf /etc/dhclient.conf \
114 -pf /tmp/dhclient.$netif.pid \
115 -lf /tmp/dhclient.$netif.lease \
116 $netif \
117 && return 0
118 _COUNT=$(($_COUNT+1))
119 [ $_COUNT -lt $_DHCPRETRY ] && sleep 1
120 done
121 warn "dhcp for interface $netif failed"
122 return 1
c98bcec8
HH
123}
124
125load_ipv6() {
28f3f537 126 [ -d /proc/sys/net/ipv6 ] && return
c98bcec8
HH
127 modprobe ipv6
128 i=0
129 while [ ! -d /proc/sys/net/ipv6 ]; do
cc02093d
HH
130 i=$(($i+1))
131 [ $i -gt 10 ] && break
132 sleep 0.1
c98bcec8
HH
133 done
134}
135
136do_ipv6auto() {
137 load_ipv6
c6c704fd
HH
138 echo 0 > /proc/sys/net/ipv6/conf/$netif/forwarding
139 echo 1 > /proc/sys/net/ipv6/conf/$netif/accept_ra
140 echo 1 > /proc/sys/net/ipv6/conf/$netif/accept_redirects
3bd7fba7 141 linkup $netif
f8b958dc 142 wait_for_ipv6_auto $netif
c98bcec8
HH
143
144 [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
8bf25df6
WC
145
146 return 0
db815843
PS
147}
148
149# Handle static ip configuration
150do_static() {
2c19a5fa 151 strglobin $ip '*:*:*' && load_ipv6
c98bcec8 152
5193198d
HH
153 if ! linkup $netif; then
154 warn "Could bring interface $netif up!"
155 return 1
156 fi
157
27a5aecf
HH
158 ip route get "$ip" | {
159 read a rest
160 if [ "$a" = "local" ]; then
161 warn "Not assigning $ip to interface $netif, cause it is already assigned!"
162 return 1
163 fi
164 return 0
165 } || return 1
166
79905784
HH
167 [ -n "$macaddr" ] && ip link set address $macaddr dev $netif
168 [ -n "$mtu" ] && ip link set mtu $mtu dev $netif
2c19a5fa 169 if strglobin $ip '*:*:*'; then
50b08e7b 170 # note no ip addr flush for ipv6
ffc68f35 171 ip addr add $ip/$mask ${srv:+peer $srv} dev $netif
61b4afb4 172 wait_for_ipv6_dad $netif
50b08e7b 173 else
9ab5ddf1
HH
174 if ! arping -f -q -D -c 2 -I $netif $ip; then
175 warn "Duplicate address detected for $ip for interface $netif."
176 return 1
177 fi
c6c704fd 178 ip addr flush dev $netif
ffc68f35 179 ip addr add $ip/$mask ${srv:+peer $srv} brd + dev $netif
50b08e7b 180 fi
db815843 181
7e50abfd 182 [ -n "$gw" ] && echo ip route replace default via $gw dev $netif > /tmp/net.$netif.gw
d92ca28b 183 [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
8bf25df6
WC
184
185 return 0
957bc5c9 186}
580bb541
PS
187
188# loopback is always handled the same way
189if [ "$netif" = "lo" ] ; then
190 ip link set lo up
191 ip addr add 127.0.0.1/8 dev lo
580bb541
PS
192 exit 0
193fi
194
96fb9c8d 195# start bond if needed
d136ca4e
HH
196if [ -e /tmp/bond.${netif}.info ]; then
197 . /tmp/bond.${netif}.info
96fb9c8d
VB
198
199 if [ "$netif" = "$bondname" ] && [ ! -e /tmp/net.$bondname.up ] ; then # We are master bond device
200 modprobe bonding
d136ca4e 201 echo "+$netif" > /sys/class/net/bonding_masters
96fb9c8d
VB
202 ip link set $netif down
203
204 # Stolen from ifup-eth
205 # add the bits to setup driver parameters here
206 for arg in $bondoptions ; do
207 key=${arg%%=*};
208 value=${arg##*=};
209 # %{value:0:1} is replaced with non-bash specific construct
210 if [ "${key}" = "arp_ip_target" -a "${#value}" != "0" -a "+${value%%+*}" != "+" ]; then
211 OLDIFS=$IFS;
212 IFS=',';
213 for arp_ip in $value; do
214 echo +$arp_ip > /sys/class/net/${netif}/bonding/$key
215 done
216 IFS=$OLDIFS;
217 else
218 echo $value > /sys/class/net/${netif}/bonding/$key
219 fi
220 done
221
3bd7fba7 222 linkup $netif
96fb9c8d
VB
223
224 for slave in $bondslaves ; do
225 ip link set $slave down
83c9ff7d 226 cat /sys/class/net/$slave/address > /tmp/net.${netif}.${slave}.hwaddr
cbf66c5f 227 echo "+$slave" > /sys/class/net/$bondname/bonding/slaves
3bd7fba7 228 linkup $slave
96fb9c8d
VB
229 done
230
231 # add the bits to setup the needed post enslavement parameters
232 for arg in $BONDING_OPTS ; do
233 key=${arg%%=*};
234 value=${arg##*=};
235 if [ "${key}" = "primary" ]; then
236 echo $value > /sys/class/net/${netif}/bonding/$key
237 fi
238 done
239 fi
240fi
241
3baa150b
CW
242if [ -e /tmp/team.info ]; then
243 . /tmp/team.info
244 if [ "$netif" = "$teammaster" ] && [ ! -e /tmp/net.$teammaster.up ] ; then
245 # We shall only bring up those _can_ come up
246 # in case of some slave is gone in active-backup mode
247 working_slaves=""
248 for slave in $teamslaves ; do
249 ip link set $slave up 2>/dev/null
250 if wait_for_if_up $slave; then
251 working_slaves+="$slave "
252 fi
253 done
254 # Do not add slaves now
2a12e974 255 teamd -d -U -n -N -t $teammaster -f /etc/teamd/$teammaster.conf
3baa150b
CW
256 for slave in $working_slaves; do
257 # team requires the slaves to be down before joining team
258 ip link set $slave down
259 teamdctl $teammaster port add $slave
260 done
261 ip link set $teammaster up
262 fi
263fi
96fb9c8d 264
580bb541 265# XXX need error handling like dhclient-script
7e9919b9 266
21928b97
AW
267if [ -e /tmp/bridge.info ]; then
268 . /tmp/bridge.info
beb097d9 269# start bridge if necessary
21928b97
AW
270 if [ "$netif" = "$bridgename" ] && [ ! -e /tmp/net.$bridgename.up ]; then
271 brctl addbr $bridgename
272 brctl setfd $bridgename 0
2db6d711 273 for ethname in $bridgeslaves ; do
21928b97 274 if [ "$ethname" = "$bondname" ] ; then
472edf82 275 DO_BOND_SETUP=yes ifup $bondname -m
a55f910c
WC
276 elif [ "$ethname" = "$teammaster" ] ; then
277 DO_TEAM_SETUP=yes ifup $teammaster -m
0c5ccb35
WC
278 elif [ "$ethname" = "$vlanname" ]; then
279 DO_VLAN_SETUP=yes ifup $vlanname -m
21928b97 280 else
3bd7fba7 281 linkup $ethname
21928b97 282 fi
21928b97
AW
283 brctl addif $bridgename $ethname
284 done
96fb9c8d 285 fi
beb097d9
WT
286fi
287
8eb81d48
AW
288get_vid() {
289 case "$1" in
290 vlan*)
39135af1 291 echo ${1#vlan}
8eb81d48
AW
292 ;;
293 *.*)
39135af1 294 echo ${1##*.}
8eb81d48
AW
295 ;;
296 esac
297}
298
299if [ "$netif" = "$vlanname" ] && [ ! -e /tmp/net.$vlanname.up ]; then
300 modprobe 8021q
472edf82
AW
301 if [ "$phydevice" = "$bondname" ] ; then
302 DO_BOND_SETUP=yes ifup $phydevice -m
a55f910c
WC
303 elif [ "$phydevice" = "$teammaster" ] ; then
304 DO_TEAM_SETUP=yes ifup $phydevice -m
472edf82 305 else
3bd7fba7 306 linkup "$phydevice"
472edf82 307 fi
39135af1 308 ip link add dev "$vlanname" link "$phydevice" type vlan id "$(get_vid $vlanname)"
0c5ccb35 309 ip link set "$vlanname" up
8eb81d48
AW
310fi
311
c9aa3cc9
HH
312# No ip lines default to dhcp
313ip=$(getarg ip)
314
315if [ -z "$ip" ]; then
1982098e
HH
316 namesrv=$(getargs nameserver)
317 for s in $namesrv; do
318 echo nameserver $s >> /tmp/net.$netif.resolv.conf
319 done
320
c9aa3cc9
HH
321 if [ "$netroot" = "dhcp6" ]; then
322 do_dhcp -6
323 else
324 do_dhcp -4
325 fi
326fi
327
56d60c4b 328
580bb541
PS
329# Specific configuration, spin through the kernel command line
330# looking for ip= lines
3e6d2b31
HH
331for p in $(getargs ip=); do
332 ip_to_var $p
38ba0d7a
HH
333 # skip ibft
334 [ "$autoconf" = "ibft" ] && continue
990e945f 335
c5f8b69a
HH
336 case "$dev" in
337 ??:??:??:??:??:??) # MAC address
338 _dev=$(iface_for_mac $dev)
339 [ -n "$_dev" ] && dev="$_dev"
340 ;;
341 ??-??-??-??-??-??) # MAC address in BOOTIF form
342 _dev=$(iface_for_mac $(fix_bootif $dev))
343 [ -n "$_dev" ] && dev="$_dev"
344 ;;
345 esac
346
580bb541 347 # If this option isn't directed at our interface, skip it
56d60c4b
KY
348 [ -n "$dev" ] && [ "$dev" != "$netif" ] && \
349 [ "$use_bridge" != 'true' ] && \
350 [ "$use_vlan" != 'true' ] && continue
580bb541 351
1982098e
HH
352 # setup nameserver
353 namesrv="$dns1 $dns2 $(getargs nameserver)"
354 for s in $namesrv; do
355 echo nameserver $s >> /tmp/net.$netif.resolv.conf
356 done
357
580bb541 358 # Store config for later use
1982098e 359 for i in ip srv gw mask hostname macaddr dns1 dns2; do
cc02093d 360 eval '[ "$'$i'" ] && echo '$i'="$'$i'"'
580bb541
PS
361 done > /tmp/net.$netif.override
362
93342718
HH
363 for autoopt in $(str_replace "$autoconf" "," " "); do
364 case $autoopt in
365 dhcp|on|any)
366 do_dhcp -4 ;;
367 dhcp6)
368 load_ipv6
369 do_dhcp -6 ;;
370 auto6)
371 do_ipv6auto ;;
372 *)
373 do_static ;;
374 esac
375 done
744c6593 376 ret=$?
c6c704fd 377
43a85a73
HH
378 > /tmp/net.${netif}.up
379
26fbe97b
HH
380 if [ -e /sys/class/net/${netif}/address ]; then
381 > /tmp/net.$(cat /sys/class/net/${netif}/address).up
382 fi
383
c6c704fd
HH
384 case $autoconf in
385 dhcp|on|any|dhcp6)
386 ;;
387 *)
744c6593 388 if [ $ret -eq 0 ]; then
c6c704fd
HH
389 setup_net $netif
390 source_hook initqueue/online $netif
391 if [ -z "$manualup" ]; then
392 /sbin/netroot $netif
393 fi
394 fi
395 ;;
396 esac
397
debf483d 398 exit 0
db815843 399done
debf483d 400
63f3bcd8
WC
401# netif isn't the top stack? Then we should exit here.
402# eg. netif is bond0. br0 is on top of it. dhcp br0 is correct but dhcp
403# bond0 doesn't make sense.
404if [ -n "$DO_BOND_SETUP" -o -n "$DO_TEAM_SETUP" -o -n "$DO_VLAN_SETUP" ]; then
405 exit 0
406fi
407
debf483d 408# no ip option directed at our interface?
43a85a73 409if [ ! -e /tmp/net.${netif}.up ]; then
2c7f7a33
HH
410 if getargs 'ip=dhcp6'; then
411 load_ipv6
412 do_dhcp -6
686f9675 413 elif getargs 'ip=dhcp'; then
2c7f7a33
HH
414 do_dhcp -4
415 fi
debf483d
HH
416fi
417
7e9919b9 418exit 0