]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/45ifcfg/write-ifcfg.sh
Specify strstr tightly, add strglob/strglobin.
[thirdparty/dracut.git] / modules.d / 45ifcfg / write-ifcfg.sh
1 #!/bin/sh
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4
5 # NFS root might have reached here before /tmp/net.ifaces was written
6 udevadm settle --timeout=30
7
8 if [ -e /tmp/bridge.info ]; then
9 . /tmp/bridge.info
10 fi
11
12 if [ -e /tmp/vlan.info ]; then
13 . /tmp/vlan.info
14 fi
15
16 mkdir -m 0755 -p /tmp/ifcfg/
17 mkdir -m 0755 -p /tmp/ifcfg-leases/
18
19 get_config_line_by_subchannel()
20 {
21 local CHANNEL
22 local line
23
24 CHANNELS="$1"
25 while read line; do
26 if strstr "$line" "$CHANNELS"; then
27 echo $line
28 return 0
29 fi
30 done < /etc/ccw.conf
31 return 1
32 }
33
34 print_s390() {
35 local _netif
36 local SUBCHANNELS
37 local OPTIONS
38 local NETTYPE
39 local CONFIG_LINE
40 local i
41 local channel
42 local OLD_IFS
43
44 _netif="$1"
45 # if we find ccw channel, then use those, instead of
46 # of the MAC
47 SUBCHANNELS=$({
48 for i in /sys/class/net/$_netif/device/cdev[0-9]*; do
49 [ -e $i ] || continue
50 channel=$(readlink -f $i)
51 echo -n "${channel##*/},"
52 done
53 })
54 [ -n "$SUBCHANNELS" ] || return 1
55
56 SUBCHANNELS=${SUBCHANNELS%,}
57 echo "SUBCHANNELS=\"${SUBCHANNELS}\""
58
59 CONFIG_LINE=$(get_config_line_by_subchannel $SUBCHANNELS)
60 [ $? -ne 0 -o -z "$CONFIG_LINE" ] && return 0
61
62 OLD_IFS=$IFS
63 IFS=","
64 set -- $CONFIG_LINE
65 IFS=$OLD_IFS
66 NETTYPE=$1
67 shift
68 SUBCHANNELS="$1"
69 OPTIONS=""
70 shift
71 while [ $# -gt 0 ]; do
72 case $1 in
73 *=*) OPTIONS="$OPTIONS $1";;
74 esac
75 shift
76 done
77 OPTIONS=${OPTIONS## }
78 echo "NETTYPE=\"${NETTYPE}\""
79 echo "OPTIONS=\"${OPTIONS}\""
80 return 0
81 }
82
83 for netup in /tmp/net.*.did-setup ; do
84 [ -f $netup ] || continue
85
86 netif=${netup%%.did-setup}
87 netif=${netif##*/net.}
88 strglobin "$netif" ":*:*:*:*:" && continue
89 [ -e /tmp/ifcfg/ifcfg-$netif ] && continue
90 unset bridge
91 unset bond
92 unset bondslaves
93 unset bondname
94 unset bondoptions
95 unset uuid
96 unset ip
97 unset gw
98 unset mtu
99 unset mask
100 unset macaddr
101 unset slave
102 unset ethname
103 [ -e /tmp/bond.${netif}.info ] && . /tmp/bond.${netif}.info
104
105 uuid=$(cat /proc/sys/kernel/random/uuid)
106 if [ "$netif" = "$bridgename" ]; then
107 bridge=yes
108 elif [ "$netif" = "$bondname" ]; then
109 # $netif can't be bridge and bond at the same time
110 bond=yes
111 fi
112 if [ "$netif" = "$vlanname" ]; then
113 vlan=yes
114 fi
115 [ -e /sys/class/net/$netif/address ] && \
116 cat /sys/class/net/$netif/address > /tmp/net.$netif.hwaddr
117 {
118 echo "# Generated by dracut initrd"
119 echo "DEVICE=\"$netif\""
120 echo "ONBOOT=yes"
121 echo "NETBOOT=yes"
122 echo "UUID=\"$uuid\""
123 if [ -f /tmp/dhclient.$netif.lease ]; then
124 [ -f /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
125 strstr "$(ip -6 addr show dev $netif)" 'inet6' && echo "IPV6INIT=yes"
126 if [ -f /tmp/net.$netif.has_ibft_config ]; then
127 echo "BOOTPROTO=ibft"
128 else
129 echo "BOOTPROTO=dhcp"
130 fi
131 cp /tmp/dhclient.$netif.lease /tmp/ifcfg-leases/dhclient-$uuid-$netif.lease
132 else
133 # If we've booted with static ip= lines, the override file is there
134 [ -e /tmp/net.$netif.override ] && . /tmp/net.$netif.override
135 if strglobin "$ip" '*:*:*'; then
136 echo "IPV6INIT=yes"
137 echo "IPV6_AUTOCONF=no"
138 echo "IPV6ADDR=\"$ip/$mask\""
139 else
140 if [ -f /tmp/net.$netif.has_ibft_config ]; then
141 echo "BOOTPROTO=ibft"
142 else
143 echo "BOOTPROTO=none"
144 echo "IPADDR=\"$ip\""
145 if strstr "$mask" "."; then
146 echo "NETMASK=\"$mask\""
147 else
148 echo "PREFIX=\"$mask\""
149 fi
150 fi
151 fi
152 if strglobin "$gw" '*:*:*'; then
153 echo "IPV6_DEFAULTGW=\"$gw\""
154 elif [ -n "$gw" ]; then
155 echo "GATEWAY=\"$gw\""
156 fi
157 fi
158 [ -n "$mtu" ] && echo "MTU=\"$mtu\""
159 } > /tmp/ifcfg/ifcfg-$netif
160
161 # bridge needs different things written to ifcfg
162 if [ -z "$bridge" ] && [ -z "$bond" ] && [ -z "$vlan" ]; then
163 # standard interface
164 {
165 [ -n "$macaddr" ] && echo "MACADDR=\"$macaddr\""
166 if ! print_s390 $netif; then
167 if [ -z "$macaddr" ] && \
168 ! is_persistent_ethernet_name "$netif" && \
169 [ -f /sys/class/net/$netif/addr_assign_type ] && \
170 [ "$(cat /sys/class/net/$netif/addr_assign_type)" = "0" ] && \
171 [ -f /sys/class/net/$netif/address ]; then
172 echo "HWADDR=\"$(cat /sys/class/net/$netif/address)\""
173 fi
174 fi
175 echo "TYPE=Ethernet"
176 echo "NAME=\"$netif\""
177 [ -n "$mtu" ] && echo "MTU=\"$mtu\""
178 } >> /tmp/ifcfg/ifcfg-$netif
179 fi
180
181 if [ -n "$vlan" ] ; then
182 {
183 echo "TYPE=Vlan"
184 echo "NAME=\"$netif\""
185 echo "VLAN=yes"
186 echo "PHYSDEV=\"$phydevice\""
187 } >> /tmp/ifcfg/ifcfg-$netif
188 fi
189
190 if [ -n "$bond" ] ; then
191 # bond interface
192 {
193 # This variable is an indicator of a bond interface for initscripts
194 echo "BONDING_OPTS=\"$bondoptions\""
195 echo "NAME=\"$netif\""
196 echo "TYPE=Bond"
197 } >> /tmp/ifcfg/ifcfg-$netif
198
199 for slave in $bondslaves ; do
200 # write separate ifcfg file for the raw eth interface
201 {
202 echo "# Generated by dracut initrd"
203 echo "DEVICE=\"$slave\""
204 echo "TYPE=Ethernet"
205 echo "ONBOOT=yes"
206 echo "NETBOOT=yes"
207 echo "HWADDR=\"$(cat /sys/class/net/$slave/address)\""
208 echo "SLAVE=yes"
209 echo "MASTER=\"$netif\""
210 echo "NAME=\"$slave\""
211 } >> /tmp/ifcfg/ifcfg-$slave
212 done
213 fi
214
215 if [ -n "$bridge" ] ; then
216 # bridge
217 {
218 echo "TYPE=Bridge"
219 echo "NAME=\"$netif\""
220 } >> /tmp/ifcfg/ifcfg-$netif
221 if [ "$ethname" = "$bondname" ] ; then
222 {
223 echo "# Generated by dracut initrd"
224 echo "DEVICE=\"$bondname\""
225 echo "ONBOOT=yes"
226 echo "NETBOOT=yes"
227 # This variable is an indicator of a bond interface for initscripts
228 echo "BONDING_OPTS=\"$bondoptions\""
229 echo "BRIDGE=\"$netif\""
230 echo "NAME=\"$bondname\""
231 } >> /tmp/ifcfg/ifcfg-$bondname
232 for slave in $bondslaves ; do
233 # write separate ifcfg file for the raw eth interface
234 {
235 echo "# Generated by dracut initrd"
236 echo "DEVICE=\"$slave\""
237 echo "TYPE=Ethernet"
238 echo "ONBOOT=yes"
239 echo "NETBOOT=yes"
240 echo "HWADDR=\"$(cat /sys/class/net/$slave/address)\""
241 echo "SLAVE=yes"
242 echo "MASTER=\"$bondname\""
243 echo "NAME=\"$slave\""
244 } >> /tmp/ifcfg/ifcfg-$slave
245 done
246 else
247 # write separate ifcfg file for the raw eth interface
248 {
249 echo "# Generated by dracut initrd"
250 echo "DEVICE=\"$ethname\""
251 echo "TYPE=Ethernet"
252 echo "ONBOOT=yes"
253 echo "NETBOOT=yes"
254 echo "HWADDR=\"$(cat /sys/class/net/$ethname/address)\""
255 echo "BRIDGE=\"$netif\""
256 echo "NAME=\"$ethname\""
257 } >> /tmp/ifcfg/ifcfg-$ethname
258 fi
259 fi
260 i=1
261 for ns in $(getargs nameserver); do
262 echo "DNS${i}=\"${ns}\"" >> /tmp/ifcfg/ifcfg-$netif
263 i=$((i+1))
264 done
265 done
266
267 # Pass network opts
268 mkdir -m 0755 -p /run/initramfs/state/etc/sysconfig/network-scripts
269 mkdir -m 0755 -p /run/initramfs/state/var/lib/dhclient
270 echo "files /etc/sysconfig/network-scripts" >> /run/initramfs/rwtab
271 echo "files /var/lib/dhclient" >> /run/initramfs/rwtab
272 {
273 cp /tmp/net.* /run/initramfs/
274 cp /tmp/net.$netif.resolv.conf /run/initramfs/state/etc/resolv.conf
275 copytree /tmp/ifcfg /run/initramfs/state/etc/sysconfig/network-scripts
276 cp /tmp/ifcfg-leases/* /run/initramfs/state/var/lib/dhclient
277 } > /dev/null 2>&1