]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/40network/ifup.sh
network/net-genrules.sh: react also on NAME
[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
3baa150b
CW
37if [ -e /tmp/team.info ]; then
38 . /tmp/team.info
39 for slave in $teamslaves ; do
40 if [ "$netif" = "$slave" ] ; then
41 netif=$teammaster
42 fi
43 done
44fi
45
0c5ccb35
WC
46if [ -e /tmp/vlan.info ]; then
47 . /tmp/vlan.info
48 if [ "$netif" = "$phydevice" ]; then
49 if [ "$netif" = "$bondname" ] && [ -n "$DO_BOND_SETUP" ] ; then
50 : # We need to really setup bond (recursive call)
51 elif [ "$netif" = "$teammaster" ] && [ -n "$DO_TEAM_SETUP" ] ; then
52 : # We need to really setup team (recursive call)
53 else
54 netif="$vlanname"
55 use_vlan='true'
56 fi
57 fi
58fi
59
957bc5c9
DY
60# bridge this interface?
61if [ -e /tmp/bridge.info ]; then
62 . /tmp/bridge.info
21928b97
AW
63 for ethname in $ethnames ; do
64 if [ "$netif" = "$ethname" ]; then
65 if [ "$netif" = "$bondname" ] && [ -n "$DO_BOND_SETUP" ] ; then
66 : # We need to really setup bond (recursive call)
a55f910c
WC
67 elif [ "$netif" = "$teammaster" ] && [ -n "$DO_TEAM_SETUP" ] ; then
68 : # We need to really setup team (recursive call)
0c5ccb35
WC
69 elif [ "$netif" = "$vlanname" ] && [ -n "$DO_VLAN_SETUP" ]; then
70 : # We need to really setup vlan (recursive call)
21928b97
AW
71 else
72 netif="$bridgename"
56d60c4b 73 use_bridge='true'
21928b97 74 fi
957bc5c9 75 fi
21928b97 76 done
957bc5c9
DY
77fi
78
957bc5c9
DY
79# disable manual ifup while netroot is set for simplifying our logic
80# in netroot case we prefer netroot to bringup $netif automaticlly
5861184e 81[ -n "$2" -a "$2" = "-m" ] && [ -z "$netroot" ] && manualup="$2"
957bc5c9
DY
82[ -z "$netroot" ] && [ -z "$manualup" ] && exit 0
83[ -n "$manualup" ] && >/tmp/net.$netif.manualup
7e9919b9 84
db815843 85# Run dhclient
7e9919b9 86do_dhcp() {
fb59f4c9 87 # dhclient-script will mark the netif up and generate the online
7e9919b9
DD
88 # event for nfsroot
89 # XXX add -V vendor class and option parsing per kernel
7c0aa2a2
HH
90 echo "Starting dhcp for interface $netif"
91 dhclient "$@" -1 -q -cf /etc/dhclient.conf -pf /tmp/dhclient.$netif.pid -lf /tmp/dhclient.$netif.lease $netif \
cc02093d 92 || echo "dhcp failed"
c98bcec8
HH
93}
94
95load_ipv6() {
96 modprobe ipv6
97 i=0
98 while [ ! -d /proc/sys/net/ipv6 ]; do
cc02093d
HH
99 i=$(($i+1))
100 [ $i -gt 10 ] && break
101 sleep 0.1
c98bcec8
HH
102 done
103}
104
105do_ipv6auto() {
106 load_ipv6
c6c704fd
HH
107 echo 0 > /proc/sys/net/ipv6/conf/$netif/forwarding
108 echo 1 > /proc/sys/net/ipv6/conf/$netif/accept_ra
109 echo 1 > /proc/sys/net/ipv6/conf/$netif/accept_redirects
3bd7fba7 110 linkup $netif
f8b958dc 111 wait_for_ipv6_auto $netif
c98bcec8
HH
112
113 [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
8bf25df6
WC
114
115 return 0
db815843
PS
116}
117
118# Handle static ip configuration
119do_static() {
c98bcec8
HH
120 strstr $ip '*:*:*' && load_ipv6
121
3bd7fba7 122 linkup $netif
79905784
HH
123 [ -n "$macaddr" ] && ip link set address $macaddr dev $netif
124 [ -n "$mtu" ] && ip link set mtu $mtu dev $netif
50b08e7b
WW
125 if strstr $ip '*:*:*'; then
126 # note no ip addr flush for ipv6
ffc68f35 127 ip addr add $ip/$mask ${srv:+peer $srv} dev $netif
50b08e7b 128 else
c6c704fd 129 ip addr flush dev $netif
ffc68f35 130 ip addr add $ip/$mask ${srv:+peer $srv} brd + dev $netif
50b08e7b 131 fi
db815843
PS
132
133 [ -n "$gw" ] && echo ip route add default via $gw dev $netif > /tmp/net.$netif.gw
d92ca28b 134 [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
8bf25df6 135
3fa59d17 136 > /tmp/setup_net_${netif}.ok
8bf25df6 137 return 0
957bc5c9 138}
580bb541
PS
139
140# loopback is always handled the same way
141if [ "$netif" = "lo" ] ; then
142 ip link set lo up
143 ip addr add 127.0.0.1/8 dev lo
580bb541
PS
144 exit 0
145fi
146
96fb9c8d 147# start bond if needed
d136ca4e
HH
148if [ -e /tmp/bond.${netif}.info ]; then
149 . /tmp/bond.${netif}.info
96fb9c8d
VB
150
151 if [ "$netif" = "$bondname" ] && [ ! -e /tmp/net.$bondname.up ] ; then # We are master bond device
152 modprobe bonding
d136ca4e 153 echo "+$netif" > /sys/class/net/bonding_masters
96fb9c8d
VB
154 ip link set $netif down
155
156 # Stolen from ifup-eth
157 # add the bits to setup driver parameters here
158 for arg in $bondoptions ; do
159 key=${arg%%=*};
160 value=${arg##*=};
161 # %{value:0:1} is replaced with non-bash specific construct
162 if [ "${key}" = "arp_ip_target" -a "${#value}" != "0" -a "+${value%%+*}" != "+" ]; then
163 OLDIFS=$IFS;
164 IFS=',';
165 for arp_ip in $value; do
166 echo +$arp_ip > /sys/class/net/${netif}/bonding/$key
167 done
168 IFS=$OLDIFS;
169 else
170 echo $value > /sys/class/net/${netif}/bonding/$key
171 fi
172 done
173
3bd7fba7 174 linkup $netif
96fb9c8d
VB
175
176 for slave in $bondslaves ; do
177 ip link set $slave down
cbf66c5f 178 echo "+$slave" > /sys/class/net/$bondname/bonding/slaves
3bd7fba7 179 linkup $slave
96fb9c8d
VB
180 done
181
182 # add the bits to setup the needed post enslavement parameters
183 for arg in $BONDING_OPTS ; do
184 key=${arg%%=*};
185 value=${arg##*=};
186 if [ "${key}" = "primary" ]; then
187 echo $value > /sys/class/net/${netif}/bonding/$key
188 fi
189 done
190 fi
191fi
192
3baa150b
CW
193if [ -e /tmp/team.info ]; then
194 . /tmp/team.info
195 if [ "$netif" = "$teammaster" ] && [ ! -e /tmp/net.$teammaster.up ] ; then
196 # We shall only bring up those _can_ come up
197 # in case of some slave is gone in active-backup mode
198 working_slaves=""
199 for slave in $teamslaves ; do
200 ip link set $slave up 2>/dev/null
201 if wait_for_if_up $slave; then
202 working_slaves+="$slave "
203 fi
204 done
205 # Do not add slaves now
a9f634f8 206 teamd -d -U -n -t $teammaster -f /etc/teamd/$teammaster.conf
3baa150b
CW
207 for slave in $working_slaves; do
208 # team requires the slaves to be down before joining team
209 ip link set $slave down
210 teamdctl $teammaster port add $slave
211 done
212 ip link set $teammaster up
213 fi
214fi
96fb9c8d 215
580bb541 216# XXX need error handling like dhclient-script
7e9919b9 217
21928b97
AW
218if [ -e /tmp/bridge.info ]; then
219 . /tmp/bridge.info
beb097d9 220# start bridge if necessary
21928b97
AW
221 if [ "$netif" = "$bridgename" ] && [ ! -e /tmp/net.$bridgename.up ]; then
222 brctl addbr $bridgename
223 brctl setfd $bridgename 0
224 for ethname in $ethnames ; do
225 if [ "$ethname" = "$bondname" ] ; then
472edf82 226 DO_BOND_SETUP=yes ifup $bondname -m
a55f910c
WC
227 elif [ "$ethname" = "$teammaster" ] ; then
228 DO_TEAM_SETUP=yes ifup $teammaster -m
0c5ccb35
WC
229 elif [ "$ethname" = "$vlanname" ]; then
230 DO_VLAN_SETUP=yes ifup $vlanname -m
21928b97 231 else
3bd7fba7 232 linkup $ethname
21928b97 233 fi
21928b97
AW
234 brctl addif $bridgename $ethname
235 done
96fb9c8d 236 fi
beb097d9
WT
237fi
238
8eb81d48
AW
239get_vid() {
240 case "$1" in
241 vlan*)
39135af1 242 echo ${1#vlan}
8eb81d48
AW
243 ;;
244 *.*)
39135af1 245 echo ${1##*.}
8eb81d48
AW
246 ;;
247 esac
248}
249
250if [ "$netif" = "$vlanname" ] && [ ! -e /tmp/net.$vlanname.up ]; then
251 modprobe 8021q
472edf82
AW
252 if [ "$phydevice" = "$bondname" ] ; then
253 DO_BOND_SETUP=yes ifup $phydevice -m
a55f910c
WC
254 elif [ "$phydevice" = "$teammaster" ] ; then
255 DO_TEAM_SETUP=yes ifup $phydevice -m
472edf82 256 else
3bd7fba7 257 linkup "$phydevice"
472edf82 258 fi
39135af1 259 ip link add dev "$vlanname" link "$phydevice" type vlan id "$(get_vid $vlanname)"
0c5ccb35 260 ip link set "$vlanname" up
8eb81d48
AW
261fi
262
a0be1edd
HH
263# setup nameserver
264namesrv=$(getargs nameserver)
265if [ -n "$namesrv" ] ; then
266 for s in $namesrv; do
267 echo nameserver $s
268 done
269fi >> /tmp/net.$netif.resolv.conf
270
c9aa3cc9
HH
271# No ip lines default to dhcp
272ip=$(getarg ip)
273
274if [ -z "$ip" ]; then
275 if [ "$netroot" = "dhcp6" ]; then
276 do_dhcp -6
277 else
278 do_dhcp -4
279 fi
280fi
281
56d60c4b 282
580bb541
PS
283# Specific configuration, spin through the kernel command line
284# looking for ip= lines
3e6d2b31
HH
285for p in $(getargs ip=); do
286 ip_to_var $p
38ba0d7a
HH
287 # skip ibft
288 [ "$autoconf" = "ibft" ] && continue
990e945f 289
c5f8b69a
HH
290 case "$dev" in
291 ??:??:??:??:??:??) # MAC address
292 _dev=$(iface_for_mac $dev)
293 [ -n "$_dev" ] && dev="$_dev"
294 ;;
295 ??-??-??-??-??-??) # MAC address in BOOTIF form
296 _dev=$(iface_for_mac $(fix_bootif $dev))
297 [ -n "$_dev" ] && dev="$_dev"
298 ;;
299 esac
300
580bb541 301 # If this option isn't directed at our interface, skip it
56d60c4b
KY
302 [ -n "$dev" ] && [ "$dev" != "$netif" ] && \
303 [ "$use_bridge" != 'true' ] && \
304 [ "$use_vlan" != 'true' ] && continue
580bb541
PS
305
306 # Store config for later use
990e945f 307 for i in ip srv gw mask hostname macaddr; do
cc02093d 308 eval '[ "$'$i'" ] && echo '$i'="$'$i'"'
580bb541
PS
309 done > /tmp/net.$netif.override
310
311 case $autoconf in
cc02093d
HH
312 dhcp|on|any)
313 do_dhcp -4 ;;
314 dhcp6)
315 do_dhcp -6 ;;
316 auto6)
317 do_ipv6auto ;;
3b403b32 318 *)
cc02093d 319 do_static ;;
db815843 320 esac
c6c704fd
HH
321
322 case $autoconf in
323 dhcp|on|any|dhcp6)
324 ;;
325 *)
326 if [ $? -eq 0 ]; then
327 setup_net $netif
328 source_hook initqueue/online $netif
329 if [ -z "$manualup" ]; then
330 /sbin/netroot $netif
331 fi
332 fi
333 ;;
334 esac
335
db815843
PS
336 break
337done
7e9919b9 338exit 0