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