]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/40network/ifup.sh
network/ifup.sh:do_static(): error out, if interface could not be brought up
[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
79905784
HH
158 [ -n "$macaddr" ] && ip link set address $macaddr dev $netif
159 [ -n "$mtu" ] && ip link set mtu $mtu dev $netif
2c19a5fa 160 if strglobin $ip '*:*:*'; then
50b08e7b 161 # note no ip addr flush for ipv6
ffc68f35 162 ip addr add $ip/$mask ${srv:+peer $srv} dev $netif
61b4afb4 163 wait_for_ipv6_dad $netif
50b08e7b 164 else
9ab5ddf1
HH
165 if ! arping -f -q -D -c 2 -I $netif $ip; then
166 warn "Duplicate address detected for $ip for interface $netif."
167 return 1
168 fi
c6c704fd 169 ip addr flush dev $netif
ffc68f35 170 ip addr add $ip/$mask ${srv:+peer $srv} brd + dev $netif
50b08e7b 171 fi
db815843 172
7e50abfd 173 [ -n "$gw" ] && echo ip route replace default via $gw dev $netif > /tmp/net.$netif.gw
d92ca28b 174 [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
8bf25df6
WC
175
176 return 0
957bc5c9 177}
580bb541
PS
178
179# loopback is always handled the same way
180if [ "$netif" = "lo" ] ; then
181 ip link set lo up
182 ip addr add 127.0.0.1/8 dev lo
580bb541
PS
183 exit 0
184fi
185
96fb9c8d 186# start bond if needed
d136ca4e
HH
187if [ -e /tmp/bond.${netif}.info ]; then
188 . /tmp/bond.${netif}.info
96fb9c8d
VB
189
190 if [ "$netif" = "$bondname" ] && [ ! -e /tmp/net.$bondname.up ] ; then # We are master bond device
191 modprobe bonding
d136ca4e 192 echo "+$netif" > /sys/class/net/bonding_masters
96fb9c8d
VB
193 ip link set $netif down
194
195 # Stolen from ifup-eth
196 # add the bits to setup driver parameters here
197 for arg in $bondoptions ; do
198 key=${arg%%=*};
199 value=${arg##*=};
200 # %{value:0:1} is replaced with non-bash specific construct
201 if [ "${key}" = "arp_ip_target" -a "${#value}" != "0" -a "+${value%%+*}" != "+" ]; then
202 OLDIFS=$IFS;
203 IFS=',';
204 for arp_ip in $value; do
205 echo +$arp_ip > /sys/class/net/${netif}/bonding/$key
206 done
207 IFS=$OLDIFS;
208 else
209 echo $value > /sys/class/net/${netif}/bonding/$key
210 fi
211 done
212
3bd7fba7 213 linkup $netif
96fb9c8d
VB
214
215 for slave in $bondslaves ; do
216 ip link set $slave down
83c9ff7d 217 cat /sys/class/net/$slave/address > /tmp/net.${netif}.${slave}.hwaddr
cbf66c5f 218 echo "+$slave" > /sys/class/net/$bondname/bonding/slaves
3bd7fba7 219 linkup $slave
96fb9c8d
VB
220 done
221
222 # add the bits to setup the needed post enslavement parameters
223 for arg in $BONDING_OPTS ; do
224 key=${arg%%=*};
225 value=${arg##*=};
226 if [ "${key}" = "primary" ]; then
227 echo $value > /sys/class/net/${netif}/bonding/$key
228 fi
229 done
230 fi
231fi
232
3baa150b
CW
233if [ -e /tmp/team.info ]; then
234 . /tmp/team.info
235 if [ "$netif" = "$teammaster" ] && [ ! -e /tmp/net.$teammaster.up ] ; then
236 # We shall only bring up those _can_ come up
237 # in case of some slave is gone in active-backup mode
238 working_slaves=""
239 for slave in $teamslaves ; do
240 ip link set $slave up 2>/dev/null
241 if wait_for_if_up $slave; then
242 working_slaves+="$slave "
243 fi
244 done
245 # Do not add slaves now
2a12e974 246 teamd -d -U -n -N -t $teammaster -f /etc/teamd/$teammaster.conf
3baa150b
CW
247 for slave in $working_slaves; do
248 # team requires the slaves to be down before joining team
249 ip link set $slave down
250 teamdctl $teammaster port add $slave
251 done
252 ip link set $teammaster up
253 fi
254fi
96fb9c8d 255
580bb541 256# XXX need error handling like dhclient-script
7e9919b9 257
21928b97
AW
258if [ -e /tmp/bridge.info ]; then
259 . /tmp/bridge.info
beb097d9 260# start bridge if necessary
21928b97
AW
261 if [ "$netif" = "$bridgename" ] && [ ! -e /tmp/net.$bridgename.up ]; then
262 brctl addbr $bridgename
263 brctl setfd $bridgename 0
2db6d711 264 for ethname in $bridgeslaves ; do
21928b97 265 if [ "$ethname" = "$bondname" ] ; then
472edf82 266 DO_BOND_SETUP=yes ifup $bondname -m
a55f910c
WC
267 elif [ "$ethname" = "$teammaster" ] ; then
268 DO_TEAM_SETUP=yes ifup $teammaster -m
0c5ccb35
WC
269 elif [ "$ethname" = "$vlanname" ]; then
270 DO_VLAN_SETUP=yes ifup $vlanname -m
21928b97 271 else
3bd7fba7 272 linkup $ethname
21928b97 273 fi
21928b97
AW
274 brctl addif $bridgename $ethname
275 done
96fb9c8d 276 fi
beb097d9
WT
277fi
278
8eb81d48
AW
279get_vid() {
280 case "$1" in
281 vlan*)
39135af1 282 echo ${1#vlan}
8eb81d48
AW
283 ;;
284 *.*)
39135af1 285 echo ${1##*.}
8eb81d48
AW
286 ;;
287 esac
288}
289
290if [ "$netif" = "$vlanname" ] && [ ! -e /tmp/net.$vlanname.up ]; then
291 modprobe 8021q
472edf82
AW
292 if [ "$phydevice" = "$bondname" ] ; then
293 DO_BOND_SETUP=yes ifup $phydevice -m
a55f910c
WC
294 elif [ "$phydevice" = "$teammaster" ] ; then
295 DO_TEAM_SETUP=yes ifup $phydevice -m
472edf82 296 else
3bd7fba7 297 linkup "$phydevice"
472edf82 298 fi
39135af1 299 ip link add dev "$vlanname" link "$phydevice" type vlan id "$(get_vid $vlanname)"
0c5ccb35 300 ip link set "$vlanname" up
8eb81d48
AW
301fi
302
c9aa3cc9
HH
303# No ip lines default to dhcp
304ip=$(getarg ip)
305
306if [ -z "$ip" ]; then
1982098e
HH
307 namesrv=$(getargs nameserver)
308 for s in $namesrv; do
309 echo nameserver $s >> /tmp/net.$netif.resolv.conf
310 done
311
c9aa3cc9
HH
312 if [ "$netroot" = "dhcp6" ]; then
313 do_dhcp -6
314 else
315 do_dhcp -4
316 fi
317fi
318
56d60c4b 319
580bb541
PS
320# Specific configuration, spin through the kernel command line
321# looking for ip= lines
3e6d2b31
HH
322for p in $(getargs ip=); do
323 ip_to_var $p
38ba0d7a
HH
324 # skip ibft
325 [ "$autoconf" = "ibft" ] && continue
990e945f 326
c5f8b69a
HH
327 case "$dev" in
328 ??:??:??:??:??:??) # MAC address
329 _dev=$(iface_for_mac $dev)
330 [ -n "$_dev" ] && dev="$_dev"
331 ;;
332 ??-??-??-??-??-??) # MAC address in BOOTIF form
333 _dev=$(iface_for_mac $(fix_bootif $dev))
334 [ -n "$_dev" ] && dev="$_dev"
335 ;;
336 esac
337
580bb541 338 # If this option isn't directed at our interface, skip it
56d60c4b
KY
339 [ -n "$dev" ] && [ "$dev" != "$netif" ] && \
340 [ "$use_bridge" != 'true' ] && \
341 [ "$use_vlan" != 'true' ] && continue
580bb541 342
1982098e
HH
343 # setup nameserver
344 namesrv="$dns1 $dns2 $(getargs nameserver)"
345 for s in $namesrv; do
346 echo nameserver $s >> /tmp/net.$netif.resolv.conf
347 done
348
580bb541 349 # Store config for later use
1982098e 350 for i in ip srv gw mask hostname macaddr dns1 dns2; do
cc02093d 351 eval '[ "$'$i'" ] && echo '$i'="$'$i'"'
580bb541
PS
352 done > /tmp/net.$netif.override
353
93342718
HH
354 for autoopt in $(str_replace "$autoconf" "," " "); do
355 case $autoopt in
356 dhcp|on|any)
357 do_dhcp -4 ;;
358 dhcp6)
359 load_ipv6
360 do_dhcp -6 ;;
361 auto6)
362 do_ipv6auto ;;
363 *)
364 do_static ;;
365 esac
366 done
c6c704fd 367
43a85a73
HH
368 > /tmp/net.${netif}.up
369
c6c704fd
HH
370 case $autoconf in
371 dhcp|on|any|dhcp6)
372 ;;
373 *)
374 if [ $? -eq 0 ]; then
375 setup_net $netif
376 source_hook initqueue/online $netif
377 if [ -z "$manualup" ]; then
378 /sbin/netroot $netif
379 fi
380 fi
381 ;;
382 esac
383
debf483d 384 exit 0
db815843 385done
debf483d 386
63f3bcd8
WC
387# netif isn't the top stack? Then we should exit here.
388# eg. netif is bond0. br0 is on top of it. dhcp br0 is correct but dhcp
389# bond0 doesn't make sense.
390if [ -n "$DO_BOND_SETUP" -o -n "$DO_TEAM_SETUP" -o -n "$DO_VLAN_SETUP" ]; then
391 exit 0
392fi
393
debf483d 394# no ip option directed at our interface?
43a85a73 395if [ ! -e /tmp/net.${netif}.up ]; then
2c7f7a33
HH
396 if getargs 'ip=dhcp6'; then
397 load_ipv6
398 do_dhcp -6
399 else
400 do_dhcp -4
401 fi
debf483d
HH
402fi
403
7e9919b9 404exit 0