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