]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/40network/ifup
Bridged network boot
[thirdparty/dracut.git] / modules.d / 40network / ifup
1 #!/bin/sh
2 #
3 # We don't need to check for ip= errors here, that is handled by the
4 # cmdline parser script
5 #
6
7 # Sadly there's no easy way to split ':' separated lines into variables
8 ip_to_var() {
9 local v=${1}:
10 set --
11 while [ -n "$v" ]; do
12 set -- "$@" "${v%%:*}"
13 v=${v#*:}
14 done
15
16 unset ip srv gw mask hostname dev autoconf
17 case $# in
18 0) autoconf="error" ;;
19 1) autoconf=$1 ;;
20 2) dev=$1; autoconf=$2 ;;
21 *) ip=$1; srv=$2; gw=$3; mask=$4; hostname=$5; dev=$6; autoconf=$7 ;;
22 esac
23 }
24
25 # Run dhclient
26 do_dhcp() {
27 # /sbin/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 dhclient -1 -q -cf /etc/dhclient.conf -pf /tmp/dhclient.$netif.pid -lf /tmp/dhclient.$netif.lease $netif
31 }
32
33 # Handle static ip configuration
34 do_static() {
35 {
36 echo ip link set $netif up
37 echo ip addr flush dev $netif
38 echo ip addr add $ip/$mask dev $netif
39 } > /tmp/net.$netif.up
40
41 [ -n "$gw" ] && echo ip route add default via $gw dev $netif > /tmp/net.$netif.gw
42 [ -n "$hostname" ] && echo hostname $hostname > /tmp/net.$netif.hostname
43
44 echo online > /sys/class/net/$netif/uevent
45 }
46
47 PATH=$PATH:/sbin:/usr/sbin
48
49 . /lib/dracut-lib.sh
50
51 if getarg rdnetdebug ; then
52 exec >/tmp/ifup.$1.$$.out
53 exec 2>>/tmp/ifup.$1.$$.out
54 set -x
55 fi
56
57 # Huh? No $1?
58 [ -z "$1" ] && exit 1
59
60 # $netif reads easier than $1
61 netif=$1
62
63 # bridge this interface?
64 if [ -e /tmp/bridge.info ]; then
65 . /tmp/bridge.info
66 if [ "$netif" = "$ethname" ]; then
67 netif="$bridgename"
68 fi
69 fi
70
71 # bail immediately if the interface is already up
72 # or we don't need the network
73 [ -f "/tmp/net.$netif.up" ] && exit 0
74 [ -f "/tmp/root.info" ] || exit 0
75 . /tmp/root.info
76 [ -z "$netroot" ] && exit 0
77
78 # loopback is always handled the same way
79 if [ "$netif" = "lo" ] ; then
80 ip link set lo up
81 ip addr add 127.0.0.1/8 dev lo
82 >/tmp/net.$netif.up
83 exit 0
84 fi
85
86 # XXX need error handling like dhclient-script
87
88 # start bridge if necessary
89 if [ "$netif" = "$bridgename" ] && [ ! -e /tmp/net.$bridgename.up ]; then
90 ip link set $ethname up
91 # Create bridge and add eth to bridge
92 brctl addbr $bridgename
93 brctl setfd $bridgename 0
94 brctl addif $bridgename $ethname
95 fi
96
97 # No ip lines default to dhcp
98 ip=$(getarg ip)
99 [ -z "$ip" ] && do_dhcp;
100
101 # Specific configuration, spin through the kernel command line
102 # looking for ip= lines
103 [ "$CMDLINE" ] || read CMDLINE </proc/cmdline;
104 for p in $CMDLINE; do
105 [ -n "${p%ip=*}" ] && continue
106 ip_to_var ${p#ip=}
107
108 # If this option isn't directed at our interface, skip it
109 [ -n "$dev" ] && [ "$dev" != "$netif" ] && continue
110
111 # Store config for later use
112 for i in ip srv gw mask hostname; do
113 eval '[ "$'$i'" ] && echo '$i'="$'$i'"'
114 done > /tmp/net.$netif.override
115
116 case $autoconf in
117 dhcp|on|any) do_dhcp ;;
118 *) do_static ;;
119 esac
120 break
121 done
122 exit 0