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