]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/35network-legacy/ifup.sh
style: shfmt reformat
[thirdparty/dracut.git] / modules.d / 35network-legacy / 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
9a52c3fd
HH
10type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
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
18
24a78b26 19# loopback is always handled the same way
9a52c3fd 20if [ "$netif" = "lo" ]; then
24a78b26
HH
21 ip link set lo up
22 ip addr add 127.0.0.1/8 dev lo
23 exit 0
d61a423f 24fi
7e9919b9 25
4026cd3b
AK
26do_dhcp_parallel() {
27 # dhclient-script will mark the netif up and generate the online
28 # event for nfsroot
29 # XXX add -V vendor class and option parsing per kernel
30
31 [ -e /tmp/dhclient.$netif.pid ] && return 0
32
33 if ! iface_has_carrier $netif; then
34 warn "No carrier detected on interface $netif"
35 return 1
36 fi
37
38 bootintf=$(readlink $IFNETFILE)
9a52c3fd 39 if [ ! -z $bootintf ] && [ -e /tmp/dhclient.$bootintf.lease ]; then
4026cd3b 40 info "DHCP already succeeded for $bootintf, exiting for $netif"
9a52c3fd 41 return 1
4026cd3b
AK
42 fi
43
44 if [ ! -e /run/NetworkManager/conf.d/10-dracut-dhclient.conf ]; then
45 mkdir -p /run/NetworkManager/conf.d
46 echo '[main]' > /run/NetworkManager/conf.d/10-dracut-dhclient.conf
9a52c3fd 47 echo 'dhcp=dhclient' >> /run/NetworkManager/conf.d/10-dracut-dhclient.conf
4026cd3b
AK
48 fi
49
50 chmod +x /sbin/dhcp-multi.sh
51 /sbin/dhcp-multi.sh "$netif" "$DO_VLAN" "$@" &
52 return 0
53}
54
db815843 55# Run dhclient
7e9919b9 56do_dhcp() {
fb59f4c9 57 # dhclient-script will mark the netif up and generate the online
7e9919b9
DD
58 # event for nfsroot
59 # XXX add -V vendor class and option parsing per kernel
c84618d7 60
2448fbf1
HH
61 local _COUNT=0
62 local _timeout=$(getargs rd.net.timeout.dhcp=)
63 local _DHCPRETRY=$(getargs rd.net.dhcp.retry=)
64 _DHCPRETRY=${_DHCPRETRY:-1}
65
c84618d7
HH
66 [ -e /tmp/dhclient.$netif.pid ] && return 0
67
df95b100 68 if ! iface_has_carrier $netif; then
2448fbf1 69 warn "No carrier detected on interface $netif"
271cd19d
HH
70 return 1
71 fi
2448fbf1 72
0454dc24
LR
73 if [ ! -e /run/NetworkManager/conf.d/10-dracut-dhclient.conf ]; then
74 mkdir -p /run/NetworkManager/conf.d
75 echo '[main]' > /run/NetworkManager/conf.d/10-dracut-dhclient.conf
9a52c3fd 76 echo 'dhcp=dhclient' >> /run/NetworkManager/conf.d/10-dracut-dhclient.conf
0454dc24
LR
77 fi
78
2448fbf1
HH
79 while [ $_COUNT -lt $_DHCPRETRY ]; do
80 info "Starting dhcp for interface $netif"
81 dhclient "$@" \
9a52c3fd
HH
82 ${_timeout:+--timeout $_timeout} \
83 -q \
84 -1 \
85 -cf /etc/dhclient.conf \
86 -pf /tmp/dhclient.$netif.pid \
87 -lf /tmp/dhclient.$netif.lease \
88 $netif \
2448fbf1 89 && return 0
9a52c3fd 90 _COUNT=$(($_COUNT + 1))
2448fbf1
HH
91 [ $_COUNT -lt $_DHCPRETRY ] && sleep 1
92 done
93 warn "dhcp for interface $netif failed"
d0de58f2
JL
94 # nuke those files since we failed; we might retry dhcp again if it's e.g.
95 # `ip=dhcp,dhcp6` and we check for the PID file at the top
96 rm -f /tmp/dhclient.$netif.{pid,lease}
2448fbf1 97 return 1
c98bcec8
HH
98}
99
100load_ipv6() {
28f3f537 101 [ -d /proc/sys/net/ipv6 ] && return
c98bcec8
HH
102 modprobe ipv6
103 i=0
104 while [ ! -d /proc/sys/net/ipv6 ]; do
9a52c3fd 105 i=$(($i + 1))
cc02093d
HH
106 [ $i -gt 10 ] && break
107 sleep 0.1
c98bcec8
HH
108 done
109}
110
111do_ipv6auto() {
67354eeb 112 local ret
c98bcec8 113 load_ipv6
c6c704fd
HH
114 echo 0 > /proc/sys/net/ipv6/conf/$netif/forwarding
115 echo 1 > /proc/sys/net/ipv6/conf/$netif/accept_ra
116 echo 1 > /proc/sys/net/ipv6/conf/$netif/accept_redirects
3bd7fba7 117 linkup $netif
f8b958dc 118 wait_for_ipv6_auto $netif
67354eeb 119 ret=$?
c98bcec8
HH
120
121 [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
8bf25df6 122
67354eeb 123 return $ret
db815843
PS
124}
125
b12f8188
JJ
126do_ipv6link() {
127 local ret
128 load_ipv6
129 echo 0 > /proc/sys/net/ipv6/conf/$netif/forwarding
130 echo 0 > /proc/sys/net/ipv6/conf/$netif/accept_ra
131 echo 0 > /proc/sys/net/ipv6/conf/$netif/accept_redirects
132 linkup $netif
133
134 [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
135
136 return $ret
137}
138
db815843
PS
139# Handle static ip configuration
140do_static() {
2c19a5fa 141 strglobin $ip '*:*:*' && load_ipv6
c98bcec8 142
c574c3f5 143 if ! iface_has_carrier "$netif"; then
df95b100
HH
144 warn "No carrier detected on interface $netif"
145 return 1
146 elif ! linkup "$netif"; then
b519ae70 147 warn "Could not bring interface $netif up!"
5193198d
HH
148 return 1
149 fi
150
9a52c3fd 151 ip route get "$ip" 2> /dev/null | {
27a5aecf
HH
152 read a rest
153 if [ "$a" = "local" ]; then
154 warn "Not assigning $ip to interface $netif, cause it is already assigned!"
155 return 1
156 fi
157 return 0
158 } || return 1
159
79905784
HH
160 [ -n "$macaddr" ] && ip link set address $macaddr dev $netif
161 [ -n "$mtu" ] && ip link set mtu $mtu dev $netif
2c19a5fa 162 if strglobin $ip '*:*:*'; then
50b08e7b 163 # note no ip addr flush for ipv6
ffc68f35 164 ip addr add $ip/$mask ${srv:+peer $srv} dev $netif
4c3d99f9
AF
165 echo 0 > /proc/sys/net/ipv6/conf/$netif/forwarding
166 echo 1 > /proc/sys/net/ipv6/conf/$netif/accept_ra
167 echo 1 > /proc/sys/net/ipv6/conf/$netif/accept_redirects
61b4afb4 168 wait_for_ipv6_dad $netif
50b08e7b 169 else
5abd692f 170 if [ -z "$srv" ]; then
9a52c3fd
HH
171 if command -v arping2 > /dev/null; then
172 if arping2 -q -C 1 -c 2 -I $netif -0 $ip; then
5abd692f
HH
173 warn "Duplicate address detected for $ip for interface $netif."
174 return 1
175 fi
176 else
9a52c3fd 177 if ! arping -f -q -D -c 2 -I $netif $ip; then
5abd692f
HH
178 warn "Duplicate address detected for $ip for interface $netif."
179 return 1
180 fi
9853791d 181 fi
9ab5ddf1 182 fi
c6c704fd 183 ip addr flush dev $netif
ffc68f35 184 ip addr add $ip/$mask ${srv:+peer $srv} brd + dev $netif
50b08e7b 185 fi
db815843 186
7e50abfd 187 [ -n "$gw" ] && echo ip route replace default via $gw dev $netif > /tmp/net.$netif.gw
d92ca28b 188 [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
8bf25df6
WC
189
190 return 0
957bc5c9 191}
580bb541 192
24a78b26
HH
193get_vid() {
194 case "$1" in
9a52c3fd
HH
195 vlan*)
196 echo ${1#vlan}
197 ;;
198 *.*)
199 echo ${1##*.}
200 ;;
24a78b26
HH
201 esac
202}
203
204# check, if we need VLAN's for this interface
205if [ -z "$DO_VLAN_PHY" ] && [ -e /tmp/vlan.${netif}.phy ]; then
e7838a83 206 unset DO_VLAN
24a78b26
HH
207 NO_AUTO_DHCP=yes DO_VLAN_PHY=yes ifup "$netif"
208 modprobe -b -q 8021q
209
210 for i in /tmp/vlan.*.${netif}; do
211 [ -e "$i" ] || continue
0cf826a1
HH
212 unset vlanname
213 unset phydevice
214 . "$i"
24a78b26
HH
215 if [ -n "$vlanname" ]; then
216 linkup "$phydevice"
217 ip link add dev "$vlanname" link "$phydevice" type vlan id "$(get_vid $vlanname)"
218 ifup "$vlanname"
219 fi
220 done
580bb541
PS
221 exit 0
222fi
223
e7838a83
HH
224# Check, if interface is VLAN interface
225if ! [ -e /tmp/vlan.${netif}.phy ]; then
226 for i in /tmp/vlan.${netif}.*; do
227 [ -e "$i" ] || continue
228 export DO_VLAN=yes
229 break
230 done
231fi
232
24a78b26
HH
233# bridge this interface?
234if [ -z "$NO_BRIDGE_MASTER" ]; then
235 for i in /tmp/bridge.*.info; do
236 [ -e "$i" ] || continue
237 unset bridgeslaves
238 unset bridgename
239 . "$i"
9a52c3fd 240 for ethname in $bridgeslaves; do
24a78b26
HH
241 [ "$netif" != "$ethname" ] && continue
242
243 NO_BRIDGE_MASTER=yes NO_AUTO_DHCP=yes ifup $ethname
244 linkup $ethname
245 if [ ! -e /tmp/bridge.$bridgename.up ]; then
28549581
DM
246 ip link add name $bridgename type bridge
247 echo 0 > /sys/devices/virtual/net/$bridgename/bridge/forward_delay
24a78b26 248 > /tmp/bridge.$bridgename.up
96fb9c8d 249 fi
28549581 250 ip link set dev $ethname master $bridgename
24a78b26
HH
251 ifup $bridgename
252 exit 0
96fb9c8d 253 done
24a78b26
HH
254 done
255fi
96fb9c8d 256
24a78b26
HH
257# enslave this interface to bond?
258if [ -z "$NO_BOND_MASTER" ]; then
259 for i in /tmp/bond.*.info; do
260 [ -e "$i" ] || continue
261 unset bondslaves
262 unset bondname
263 . "$i"
9a52c3fd 264 for slave in $bondslaves; do
24a78b26 265 [ "$netif" != "$slave" ] && continue
96fb9c8d 266
24a78b26
HH
267 # already setup
268 [ -e /tmp/bond.$bondname.up ] && exit 0
269
270 # wait for all slaves to show up
9a52c3fd 271 for slave in $bondslaves; do
24a78b26
HH
272 # try to create the slave (maybe vlan or bridge)
273 NO_BOND_MASTER=yes NO_AUTO_DHCP=yes ifup $slave
274
9a52c3fd 275 if ! ip link show dev $slave > /dev/null 2>&1; then
24a78b26
HH
276 # wait for the last slave to show up
277 exit 0
278 fi
279 done
280
281 modprobe -q -b bonding
9a52c3fd 282 echo "+$bondname" > /sys/class/net/bonding_masters 2> /dev/null
24a78b26
HH
283 ip link set $bondname down
284
285 # Stolen from ifup-eth
286 # add the bits to setup driver parameters here
9a52c3fd
HH
287 for arg in $bondoptions; do
288 key=${arg%%=*}
289 value=${arg##*=}
24a78b26
HH
290 # %{value:0:1} is replaced with non-bash specific construct
291 if [ "${key}" = "arp_ip_target" -a "${#value}" != "0" -a "+${value%%+*}" != "+" ]; then
9a52c3fd
HH
292 OLDIFS=$IFS
293 IFS=','
24a78b26
HH
294 for arp_ip in $value; do
295 echo +$arp_ip > /sys/class/net/${bondname}/bonding/$key
296 done
9a52c3fd 297 IFS=$OLDIFS
24a78b26
HH
298 else
299 echo $value > /sys/class/net/${bondname}/bonding/$key
300 fi
301 done
302
303 linkup $bondname
304
9a52c3fd 305 for slave in $bondslaves; do
24a78b26
HH
306 cat /sys/class/net/$slave/address > /tmp/net.${bondname}.${slave}.hwaddr
307 ip link set $slave down
308 echo "+$slave" > /sys/class/net/$bondname/bonding/slaves
309 linkup $slave
310 done
311
292548be 312 # Set mtu on bond master
79926486 313 [ -n "$bondmtu" ] && ip link set mtu $bondmtu dev $bondname
292548be 314
24a78b26 315 # add the bits to setup the needed post enslavement parameters
9a52c3fd
HH
316 for arg in $bondoptions; do
317 key=${arg%%=*}
318 value=${arg##*=}
24a78b26
HH
319 if [ "${key}" = "primary" ]; then
320 echo $value > /sys/class/net/${bondname}/bonding/$key
321 fi
322 done
323
324 > /tmp/bond.$bondname.up
325
326 NO_BOND_MASTER=yes ifup $bondname
327 exit $?
96fb9c8d 328 done
24a78b26 329 done
96fb9c8d
VB
330fi
331
24a78b26
HH
332if [ -z "$NO_TEAM_MASTER" ]; then
333 for i in /tmp/team.*.info; do
334 [ -e "$i" ] || continue
335 unset teammaster
336 unset teamslaves
337 . "$i"
9a52c3fd 338 for slave in $teamslaves; do
24a78b26 339 [ "$netif" != "$slave" ] && continue
96fb9c8d 340
24a78b26 341 [ -e /tmp/team.$teammaster.up ] && exit 0
7e9919b9 342
24a78b26 343 # wait for all slaves to show up
9a52c3fd 344 for slave in $teamslaves; do
24a78b26 345 # try to create the slave (maybe vlan or bridge)
6ed73e37 346 NO_TEAM_MASTER=yes NO_AUTO_DHCP=yes ifup $slave
24a78b26 347
9a52c3fd 348 if ! ip link show dev $slave > /dev/null 2>&1; then
24a78b26
HH
349 # wait for the last slave to show up
350 exit 0
351 fi
352 done
353
9a52c3fd 354 if [ ! -e /tmp/team.$teammaster.up ]; then
24a78b26
HH
355 # We shall only bring up those _can_ come up
356 # in case of some slave is gone in active-backup mode
357 working_slaves=""
9a52c3fd
HH
358 for slave in $teamslaves; do
359 teamdctl ${teammaster} port present ${slave} 2> /dev/null \
041e49ee 360 && continue
9a52c3fd 361 ip link set dev $slave up 2> /dev/null
24a78b26 362 if wait_for_if_up $slave; then
bcabe0fe 363 working_slaves="$working_slaves$slave "
24a78b26
HH
364 fi
365 done
366 # Do not add slaves now
041e49ee 367 teamd -d -U -n -N -t $teammaster -f /etc/teamd/${teammaster}.conf
24a78b26
HH
368 for slave in $working_slaves; do
369 # team requires the slaves to be down before joining team
041e49ee
HH
370 ip link set dev $slave down
371 (
372 unset TEAM_PORT_CONFIG
373 _hwaddr=$(cat /sys/class/net/$slave/address)
374 _subchannels=$(iface_get_subchannels "$slave")
375 if [ -n "$_hwaddr" ] && [ -e "/etc/sysconfig/network-scripts/mac-${_hwaddr}.conf" ]; then
376 . "/etc/sysconfig/network-scripts/mac-${_hwaddr}.conf"
377 elif [ -n "$_subchannels" ] && [ -e "/etc/sysconfig/network-scripts/ccw-${_subchannels}.conf" ]; then
378 . "/etc/sysconfig/network-scripts/ccw-${_subchannels}.conf"
379 elif [ -e "/etc/sysconfig/network-scripts/ifcfg-${slave}" ]; then
380 . "/etc/sysconfig/network-scripts/ifcfg-${slave}"
381 fi
382
383 if [ -n "${TEAM_PORT_CONFIG}" ]; then
384 /usr/bin/teamdctl ${teammaster} port config update ${slave} "${TEAM_PORT_CONFIG}"
385 fi
386 )
24a78b26
HH
387 teamdctl $teammaster port add $slave
388 done
389
041e49ee 390 ip link set dev $teammaster up
24a78b26
HH
391
392 > /tmp/team.$teammaster.up
393 NO_TEAM_MASTER=yes ifup $teammaster
394 exit $?
21928b97 395 fi
21928b97 396 done
24a78b26 397 done
beb097d9
WT
398fi
399
24a78b26 400# all synthetic interfaces done.. now check if the interface is available
9a52c3fd 401if ! ip link show dev $netif > /dev/null 2>&1; then
24a78b26
HH
402 exit 1
403fi
8eb81d48 404
24a78b26
HH
405# disable manual ifup while netroot is set for simplifying our logic
406# in netroot case we prefer netroot to bringup $netif automaticlly
407[ -n "$2" -a "$2" = "-m" ] && [ -z "$netroot" ] && manualup="$2"
408
409if [ -n "$manualup" ]; then
9a52c3fd 410 > /tmp/net.$netif.manualup
24a78b26
HH
411 rm -f /tmp/net.${netif}.did-setup
412else
413 [ -e /tmp/net.${netif}.did-setup ] && exit 0
9a52c3fd
HH
414 [ -z "$DO_VLAN" ] \
415 && [ -e /sys/class/net/$netif/address ] \
416 && [ -e /tmp/net.$(cat /sys/class/net/$netif/address).did-setup ] && exit 0
8eb81d48
AW
417fi
418
580bb541
PS
419# Specific configuration, spin through the kernel command line
420# looking for ip= lines
3e6d2b31
HH
421for p in $(getargs ip=); do
422 ip_to_var $p
38ba0d7a
HH
423 # skip ibft
424 [ "$autoconf" = "ibft" ] && continue
990e945f 425
c5f8b69a 426 case "$dev" in
9a52c3fd 427 ??:??:??:??:??:??) # MAC address
c5f8b69a
HH
428 _dev=$(iface_for_mac $dev)
429 [ -n "$_dev" ] && dev="$_dev"
430 ;;
9a52c3fd 431 ??-??-??-??-??-??) # MAC address in BOOTIF form
c5f8b69a
HH
432 _dev=$(iface_for_mac $(fix_bootif $dev))
433 [ -n "$_dev" ] && dev="$_dev"
434 ;;
435 esac
436
580bb541 437 # If this option isn't directed at our interface, skip it
f6e3b59e
HH
438 if [ -n "$dev" ]; then
439 [ "$dev" != "$netif" ] && continue
440 else
441 iface_is_enslaved "$netif" && continue
442 fi
580bb541 443
73fb5e76
LN
444 # Store config for later use
445 for i in ip srv gw mask hostname macaddr mtu dns1 dns2; do
446 eval '[ "$'$i'" ] && echo '$i'="$'$i'"'
447 done > /tmp/net.$netif.override
448
93342718
HH
449 for autoopt in $(str_replace "$autoconf" "," " "); do
450 case $autoopt in
9a52c3fd
HH
451 dhcp | on | any)
452 do_dhcp -4
453 ;;
4026cd3b
AK
454 single-dhcp)
455 do_dhcp_parallel -4
9a52c3fd
HH
456 exit 0
457 ;;
93342718
HH
458 dhcp6)
459 load_ipv6
9a52c3fd
HH
460 do_dhcp -6
461 ;;
93342718 462 auto6)
9a52c3fd
HH
463 do_ipv6auto
464 ;;
67354eeb 465 either6)
9a52c3fd
HH
466 do_ipv6auto || do_dhcp -6
467 ;;
b12f8188 468 link6)
9a52c3fd
HH
469 do_ipv6link
470 ;;
93342718 471 *)
9a52c3fd
HH
472 do_static
473 ;;
93342718
HH
474 esac
475 done
744c6593 476 ret=$?
c6c704fd 477
cf376023
XP
478 # setup nameserver
479 for s in "$dns1" "$dns2" $(getargs nameserver); do
480 [ -n "$s" ] || continue
481 echo nameserver $s >> /tmp/net.$netif.resolv.conf
482 done
483
df95b100
HH
484 if [ $ret -eq 0 ]; then
485 > /tmp/net.${netif}.up
43a85a73 486
9a52c3fd 487 if [ -z "$DO_VLAN" ] && [ -e /sys/class/net/${netif}/address ]; then
df95b100
HH
488 > /tmp/net.$(cat /sys/class/net/${netif}/address).up
489 fi
26fbe97b 490
4985aa8c
JL
491 # and finally, finish interface set up if there isn't already a script
492 # to do so (which is the case in the dhcp path)
493 if [ ! -e $hookdir/initqueue/setup_net_$netif.sh ]; then
494 setup_net $netif
495 source_hook initqueue/online $netif
496 if [ -z "$manualup" ]; then
497 /sbin/netroot $netif
498 fi
499 fi
8c6ab479 500
9a52c3fd 501 if command -v wicked > /dev/null && [ -z "$manualup" ]; then
8c6ab479
TB
502 /sbin/netroot $netif
503 fi
504
df95b100
HH
505 exit $ret
506 fi
db815843 507done
debf483d
HH
508
509# no ip option directed at our interface?
24a78b26 510if [ -z "$NO_AUTO_DHCP" ] && [ ! -e /tmp/net.${netif}.up ]; then
3f5bf54f 511 ret=1
6cfdb5aa
HH
512 if [ -e /tmp/net.bootdev ]; then
513 BOOTDEV=$(cat /tmp/net.bootdev)
514 if [ "$netif" = "$BOOTDEV" ] || [ "$BOOTDEV" = "$(cat /sys/class/net/${netif}/address)" ]; then
6cfdb5aa 515 do_dhcp
3f5bf54f 516 ret=$?
6cfdb5aa
HH
517 fi
518 else
3f5bf54f
TB
519 # No ip lines, no bootdev -> default to dhcp
520 ip=$(getarg ip)
521
9a52c3fd 522 if getargs 'ip=dhcp6' > /dev/null || [ -z "$ip" -a "$netroot" = "dhcp6" ]; then
6cfdb5aa
HH
523 load_ipv6
524 do_dhcp -6
3f5bf54f 525 ret=$?
6cfdb5aa 526 fi
9a52c3fd 527 if getargs 'ip=dhcp' > /dev/null || [ -z "$ip" -a "$netroot" != "dhcp6" ]; then
6cfdb5aa 528 do_dhcp -4
3f5bf54f 529 ret=$?
6cfdb5aa 530 fi
2c7f7a33 531 fi
3f5bf54f
TB
532
533 for s in $(getargs nameserver); do
534 [ -n "$s" ] || continue
535 echo nameserver $s >> /tmp/net.$netif.resolv.conf
536 done
537
9a52c3fd
HH
538 if [ "$ret" -eq 0 ] && [ -n "$(ls /tmp/leaseinfo.${netif}* 2> /dev/null)" ]; then
539 > /tmp/net.${netif}.did-setup
540 if [ -e /sys/class/net/${netif}/address ]; then
541 > /tmp/net.$(cat /sys/class/net/${netif}/address).did-setup
542 fi
3f5bf54f 543 fi
debf483d
HH
544fi
545
7e9919b9 546exit 0