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