change "while read x" to cope with EOF without newline
[dracut.git] / modules.d / 45ifcfg / write-ifcfg.sh
blobfa7db2730b75d981503926b443e0125b0b2ed981
1 #!/bin/sh
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
6 udevadm settle --timeout=30
8 if [ -e /tmp/bridge.info ]; then
9 . /tmp/bridge.info
12 if [ -e /tmp/vlan.info ]; then
13 . /tmp/vlan.info
16 mkdir -m 0755 -p /tmp/ifcfg/
17 mkdir -m 0755 -p /tmp/ifcfg-leases/
19 get_config_line_by_subchannel()
21 local CHANNEL
22 local line
24 CHANNELS="$1"
25 while read line || [ -n "$line" ]; do
26 if strstr "$line" "$CHANNELS"; then
27 echo $line
28 return 0
30 done < /etc/ccw.conf
31 return 1
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
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
54 [ -n "$SUBCHANNELS" ] || return 1
56 SUBCHANNELS=${SUBCHANNELS%,}
57 echo "SUBCHANNELS=\"${SUBCHANNELS}\""
59 CONFIG_LINE=$(get_config_line_by_subchannel $SUBCHANNELS)
60 [ $? -ne 0 -o -z "$CONFIG_LINE" ] && return 0
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
83 hw_bind() {
84 local _netif="$1"
85 local _macaddr="$2"
87 [ -n "$_macaddr" ] \
88 && echo "MACADDR=\"$_macaddr\""
90 print_s390 "$_netif" \
91 && return 0
93 [ -n "$_macaddr" ] && return 0
95 is_persistent_ethernet_name "$_netif" && return 0
97 [ -f "/sys/class/net/$_netif/addr_assign_type" ] \
98 && [ "$(cat "/sys/class/net/$_netif/addr_assign_type")" != "0" ] \
99 && return 1
101 [ -f "/sys/class/net/$_netif/address" ] \
102 || return 1
104 echo "HWADDR=\"$(cat /sys/class/net/$_netif/address)\""
107 interface_bind() {
108 local _netif="$1"
109 local _macaddr="$2"
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
117 echo "DEVICE=\"$_netif\""
120 for netup in /tmp/net.*.did-setup ; do
121 [ -f $netup ] || continue
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 uuid
133 unset ip
134 unset gw
135 unset mtu
136 unset mask
137 unset macaddr
138 unset slave
139 unset ethname
140 unset vlan
142 [ -e /tmp/bond.${netif}.info ] && . /tmp/bond.${netif}.info
144 uuid=$(cat /proc/sys/kernel/random/uuid)
145 if [ "$netif" = "$bridgename" ]; then
146 bridge=yes
147 elif [ "$netif" = "$bondname" ]; then
148 # $netif can't be bridge and bond at the same time
149 bond=yes
151 if [ "$netif" = "$vlanname" ]; then
152 vlan=yes
156 echo "# Generated by dracut initrd"
157 echo "NAME=\"$netif\""
158 interface_bind "$netif" "$macaddr"
159 echo "ONBOOT=yes"
160 echo "NETBOOT=yes"
161 echo "UUID=\"$uuid\""
162 strstr "$(ip -6 addr show dev $netif)" 'inet6' && echo "IPV6INIT=yes"
163 if [ -f /tmp/dhclient.$netif.lease ]; then
164 [ -f /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
165 if [ -f /tmp/net.$netif.has_ibft_config ]; then
166 echo "BOOTPROTO=ibft"
167 else
168 echo "BOOTPROTO=dhcp"
170 cp /tmp/dhclient.$netif.lease /tmp/ifcfg-leases/dhclient-$uuid-$netif.lease
171 else
172 # If we've booted with static ip= lines, the override file is there
173 [ -e /tmp/net.$netif.override ] && . /tmp/net.$netif.override
174 if strglobin "$ip" '*:*:*'; then
175 echo "IPV6INIT=yes"
176 echo "IPV6_AUTOCONF=no"
177 echo "IPV6ADDR=\"$ip/$mask\""
178 else
179 if [ -f /tmp/net.$netif.has_ibft_config ]; then
180 echo "BOOTPROTO=ibft"
181 else
182 echo "BOOTPROTO=none"
183 echo "IPADDR=\"$ip\""
184 if strstr "$mask" "."; then
185 echo "NETMASK=\"$mask\""
186 else
187 echo "PREFIX=\"$mask\""
191 if strglobin "$gw" '*:*:*'; then
192 echo "IPV6_DEFAULTGW=\"$gw\""
193 elif [ -n "$gw" ]; then
194 echo "GATEWAY=\"$gw\""
197 [ -n "$mtu" ] && echo "MTU=\"$mtu\""
198 } > /tmp/ifcfg/ifcfg-$netif
200 # bridge needs different things written to ifcfg
201 if [ -z "$bridge" ] && [ -z "$bond" ] && [ -z "$vlan" ]; then
202 # standard interface
204 echo "TYPE=Ethernet"
205 [ -n "$mtu" ] && echo "MTU=\"$mtu\""
206 } >> /tmp/ifcfg/ifcfg-$netif
209 if [ -n "$vlan" ] ; then
211 echo "TYPE=Vlan"
212 echo "NAME=\"$netif\""
213 echo "VLAN=yes"
214 echo "PHYSDEV=\"$phydevice\""
215 } >> /tmp/ifcfg/ifcfg-$netif
218 if [ -n "$bond" ] ; then
219 # bond interface
221 # This variable is an indicator of a bond interface for initscripts
222 echo "BONDING_OPTS=\"$bondoptions\""
223 echo "NAME=\"$netif\""
224 echo "TYPE=Bond"
225 } >> /tmp/ifcfg/ifcfg-$netif
227 for slave in $bondslaves ; do
228 # write separate ifcfg file for the raw eth interface
230 echo "# Generated by dracut initrd"
231 echo "NAME=\"$slave\""
232 echo "TYPE=Ethernet"
233 echo "ONBOOT=yes"
234 echo "NETBOOT=yes"
235 echo "SLAVE=yes"
236 echo "MASTER=\"$netif\""
237 echo "UUID=\"$(cat /proc/sys/kernel/random/uuid)\""
238 unset macaddr
239 [ -e /tmp/net.$slave.override ] && . /tmp/net.$slave.override
240 interface_bind "$slave" "$macaddr"
241 ) >> /tmp/ifcfg/ifcfg-$slave
242 done
245 if [ -n "$bridge" ] ; then
246 # bridge
248 echo "TYPE=Bridge"
249 echo "NAME=\"$netif\""
250 } >> /tmp/ifcfg/ifcfg-$netif
251 for slave in $bridgeslaves ; do
252 # write separate ifcfg file for the raw eth interface
254 echo "# Generated by dracut initrd"
255 echo "NAME=\"$slave\""
256 echo "TYPE=Ethernet"
257 echo "ONBOOT=yes"
258 echo "NETBOOT=yes"
259 echo "BRIDGE=\"$bridgename\""
260 echo "UUID=\"$(cat /proc/sys/kernel/random/uuid)\""
261 unset macaddr
262 [ -e /tmp/net.$slave.override ] && . /tmp/net.$slave.override
263 interface_bind "$slave" "$macaddr"
264 ) >> /tmp/ifcfg/ifcfg-$slave
265 done
268 for ns in $(getargs nameserver); do
269 echo "DNS${i}=\"${ns}\"" >> /tmp/ifcfg/ifcfg-$netif
270 i=$((i+1))
271 done
273 [ -f /tmp/net.route6."$netif" ] && cp /tmp/net.route6."$netif" /tmp/ifcfg/route6-"$netif"
274 [ -f /tmp/net.route."$netif" ] && cp /tmp/net.route."$netif" /tmp/ifcfg/route-"$netif"
275 done
277 # Pass network opts
278 mkdir -m 0755 -p /run/initramfs/state/etc/sysconfig/network-scripts
279 mkdir -m 0755 -p /run/initramfs/state/var/lib/dhclient
280 echo "files /etc/sysconfig/network-scripts" >> /run/initramfs/rwtab
281 echo "files /var/lib/dhclient" >> /run/initramfs/rwtab
283 cp /tmp/net.* /run/initramfs/
284 cp /tmp/net.$netif.resolv.conf /run/initramfs/state/etc/resolv.conf
285 copytree /tmp/ifcfg /run/initramfs/state/etc/sysconfig/network-scripts
286 cp /tmp/ifcfg-leases/* /run/initramfs/state/var/lib/dhclient
287 } > /dev/null 2>&1