]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/40network/net-lib.sh
add mtu and macaddr to ip=
[thirdparty/dracut.git] / modules.d / 40network / net-lib.sh
1 #!/bin/sh
2
3 get_ip() {
4 local iface="$1" ip=""
5 ip=$(ip -o -f inet addr show $iface)
6 ip=${ip%%/*}
7 ip=${ip##* }
8 }
9
10 iface_for_remote_addr() {
11 set -- $(ip -o route get to $1)
12 echo $5
13 }
14
15 iface_for_mac() {
16 local interface="" mac="$(echo $1 | tr '[:upper:]' '[:lower:]')"
17 for interface in /sys/class/net/*; do
18 if [ $(cat $interface/address) = "$mac" ]; then
19 echo ${interface##*/}
20 fi
21 done
22 }
23
24 iface_has_link() {
25 local interface="$1" flags=""
26 [ -n "$interface" ] || return 2
27 interface="/sys/class/net/$interface"
28 [ -d "$interface" ] || return 2
29 flags=$(cat $interface/flags)
30 echo $(($flags|0x41)) > $interface/flags # 0x41: IFF_UP|IFF_RUNNING
31 [ "$(cat $interface/carrier)" = 1 ] || return 1
32 # XXX Do we need to reset the flags here? anaconda never bothered..
33 }
34
35 all_ifaces_up() {
36 local iface="" IFACES=""
37 [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
38 for iface in $IFACES; do
39 [ -e /tmp/net.$iface.up ] || return 1
40 done
41 }
42
43 get_netroot_ip() {
44 local prefix="" server="" rest=""
45 splitsep "$1" ":" prefix server rest
46 case $server in
47 [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*) echo "$server"; return 0 ;;
48 esac
49 return 1
50 }
51
52 ip_is_local() {
53 strstr "$(ip route get $1 2>/dev/null)" " via "
54 }
55
56 ifdown() {
57 local netif="$1"
58 # ip down/flush ensures that routing info goes away as well
59 ip link set $netif down
60 ip addr flush dev $netif
61 echo "#empty" > /etc/resolv.conf
62 # TODO: send "offline" uevent?
63 }
64
65 setup_net() {
66 local netif="$1" f="" gw_ip="" netroot_ip="" iface="" IFACES=""
67 [ -e /tmp/net.$netif.up ] || return 1
68 [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
69 [ -z "$IFACES" ] && IFACES="$netif"
70 for iface in $IFACES ; do
71 . /tmp/net.$iface.up
72 done
73 # run the scripts written by ifup
74 [ -e /tmp/net.$netif.gw ] && . /tmp/net.$netif.gw
75 [ -e /tmp/net.$netif.hostname ] && . /tmp/net.$netif.hostname
76 [ -e /tmp/net.$netif.override ] && . /tmp/net.$netif.override
77 [ -e /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
78 # set up resolv.conf
79 [ -e /tmp/net.$netif.resolv.conf ] && \
80 cp -f /tmp/net.$netif.resolv.conf /etc/resolv.conf
81
82 # Handle STP Timeout: arping the default gateway.
83 # (or the root server, if a) it's local or b) there's no gateway.)
84 # Note: This assumes that if no router is present the
85 # root server is on the same subnet.
86
87 # Get DHCP-provided router IP, or the cmdline-provided "gw=" argument
88 [ -n "$new_routers" ] && gw_ip=${new_routers%%,*}
89 [ -n "$gw" ] && gw_ip=$gw
90
91 # Get the "netroot" IP (if there's an IP address in there)
92 netroot_ip=$(get_netroot_ip $netroot)
93
94 # try netroot if it's local (or there's no gateway)
95 if ip_is_local $netroot_ip || [ -z "$gw_ip" ]; then
96 dest="$netroot_ip"
97 else
98 dest="$gw_ip"
99 fi
100 if [ -n "$dest" ] && ! arping -q -f -w 60 -I $netif $dest ; then
101 info "Resolving $dest via ARP on $netif failed"
102 fi
103 }
104
105 set_ifname() {
106 local name="$1" mac="$2" num=0 n=""
107 # if it's already set, return the existing name
108 for n in $(getargs ifname=); do
109 strstr "$n" "$mac" && echo ${n%%:*} && return
110 done
111 # otherwise, pick a new name and use that
112 while [ -e /sys/class/$name$num ]; do num=$(($num+1)); done
113 echo "ifname=$name$num:$mac" >> /etc/cmdline.d/45-ifname.conf
114 echo "$name$num"
115 }
116
117 ibft_to_cmdline() {
118 local iface="" mac="" dev=""
119 local dhcp="" ip="" gw="" mask="" hostname=""
120 modprobe -q iscsi_ibft
121 (
122 for iface in /sys/firmware/ibft/ethernet*; do
123 [ -e ${iface}/mac ] || continue
124 mac=$(read a < ${iface}/mac; echo $a)
125 [ -z "$ifname_mac" ] && continue
126 dev=$(set_ifname ibft $ifname_mac)
127 dhcp=$(read a < ${iface}/dhcp; echo $a)
128 if [ -n "$dhcp" ]; then
129 echo "ip=$dev:dhcp"
130 else
131 ip=$(read a < ${iface}/ip-addr; echo $a)
132 gw=$(read a < ${iface}/gateway; echo $a)
133 mask=$(read a < ${iface}/subnet-mask; echo $a)
134 hostname=$(read a < ${iface}/hostname; echo $a)
135 echo "ip=$ip::$gw:$mask:$hostname:$dev:none"
136 fi
137 done
138 ) >> /etc/cmdline.d/40-ibft.conf
139 # reread cmdline
140 unset CMDLINE
141 }
142
143 parse_iscsi_root()
144 {
145 local v
146 v=${1#iscsi:}
147
148 # extract authentication info
149 case "$v" in
150 *@*:*:*:*:*)
151 authinfo=${v%%@*}
152 v=${v#*@}
153 # allow empty authinfo to allow having an @ in iscsi_target_name like this:
154 # netroot=iscsi:@192.168.1.100::3260::iqn.2009-01.com.example:testdi@sk
155 if [ -n "$authinfo" ]; then
156 OLDIFS="$IFS"
157 IFS=:
158 set $authinfo
159 IFS="$OLDIFS"
160 if [ $# -gt 4 ]; then
161 warn "Wrong authentication info in iscsi: parameter!"
162 return 1
163 fi
164 iscsi_username=$1
165 iscsi_password=$2
166 if [ $# -gt 2 ]; then
167 iscsi_in_username=$3
168 iscsi_in_password=$4
169 fi
170 fi
171 ;;
172 esac
173
174 # extract target ip
175 case "$v" in
176 [[]*[]]:*)
177 iscsi_target_ip=${v#[[]}
178 iscsi_target_ip=${iscsi_target_ip%%[]]*}
179 v=${v#[[]$iscsi_target_ip[]]:}
180 ;;
181 *)
182 iscsi_target_ip=${v%%[:]*}
183 v=${v#$iscsi_target_ip:}
184 ;;
185 esac
186
187 # extract target name
188 case "$v" in
189 *:iqn.*)
190 iscsi_target_name=iqn.${v##*:iqn.}
191 v=${v%:iqn.*}:
192 ;;
193 *:eui.*)
194 iscsi_target_name=iqn.${v##*:eui.}
195 v=${v%:iqn.*}:
196 ;;
197 *:naa.*)
198 iscsi_target_name=iqn.${v##*:naa.}
199 v=${v%:iqn.*}:
200 ;;
201 *)
202 warn "Invalid iscii target name, should begin with 'iqn.' or 'eui.' or 'naa.'"
203 return 1
204 ;;
205 esac
206
207 # parse the rest
208 OLDIFS="$IFS"
209 IFS=:
210 set $v
211 IFS="$OLDIFS"
212
213 iscsi_protocol=$1; shift # ignored
214 iscsi_target_port=$1; shift
215 if [ $# -eq 3 ]; then
216 iscsi_iface_name=$1; shift
217 fi
218 if [ $# -eq 2 ]; then
219 iscsi_netdev_name=$1; shift
220 fi
221 iscsi_lun=$1; shift
222 if [ $# -ne 0 ]; then
223 warn "Invalid parameter in iscsi: parameter!"
224 return 1
225 fi
226 }
227
228 ip_to_var() {
229 local v=${1}:
230 local i
231 set --
232 while [ -n "$v" ]; do
233 if [ "${v#\[*:*:*\]:}" != "$v" ]; then
234 # handle IPv6 address
235 i="${v%%\]:*}"
236 i="${i##\[}"
237 set -- "$@" "$i"
238 v=${v#\[$i\]:}
239 else
240 set -- "$@" "${v%%:*}"
241 v=${v#*:}
242 fi
243 done
244
245 unset ip srv gw mask hostname dev autoconf macaddr mtu
246 case $# in
247 0) autoconf="error" ;;
248 1) autoconf=$1 ;;
249 2) dev=$1; autoconf=$2 ;;
250 3) dev=$1; autoconf=$2; mtu=$3 ;;
251 4) dev=$1; autoconf=$2; mtu=$3; macaddr=$4 ;;
252 *) ip=$1; srv=$2; gw=$3; mask=$4; hostname=$5; dev=$6; autoconf=$7; mtu=$8; macaddr=$9 ;;
253 esac
254 }