]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/45ifcfg/write-ifcfg.sh
ifcfg/write-ifcfg.sh: use PREFIX for prefix netmask form
[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 # Don't write anything if we don't know our bootdev
8 [ -f /tmp/net.ifaces ] || return 1
9
10 read IFACES < /tmp/net.ifaces
11
12 if [ -e /tmp/bond.info ]; then
13 . /tmp/bond.info
14 fi
15
16 if [ -e /tmp/bridge.info ]; then
17 . /tmp/bridge.info
18 fi
19
20 mkdir -m 0755 -p /tmp/ifcfg/
21 mkdir -m 0755 -p /tmp/ifcfg-leases/
22
23 for netif in $IFACES ; do
24 # bridge?
25 unset bridge
26 unset bond
27 uuid=$(cat /proc/sys/kernel/random/uuid)
28 if [ "$netif" = "$bridgename" ]; then
29 bridge=yes
30 elif [ "$netif" = "$bondname" ]; then
31 # $netif can't be bridge and bond at the same time
32 bond=yes
33 fi
34 cat /sys/class/net/$netif/address > /tmp/net.$netif.hwaddr
35 {
36 echo "# Generated by dracut initrd"
37 echo "DEVICE=$netif"
38 echo "ONBOOT=yes"
39 echo "NETBOOT=yes"
40 echo "UUID=$uuid"
41 [ -n "$macaddr" ] && echo "MACADDR=$macaddr"
42 [ -n "$mtu" ] && echo "MTU=$mtu"
43 if [ -f /tmp/net.$netif.lease ]; then
44 strstr "$ip" '*:*:*' &&
45 echo "DHCPV6C=yes"
46 echo "BOOTPROTO=dhcp"
47 cp /tmp/net.$netif.lease /tmp/ifcfg-leases/dhclient-$uuid-$netif.lease
48 else
49 echo "BOOTPROTO=none"
50 # If we've booted with static ip= lines, the override file is there
51 [ -e /tmp/net.$netif.override ] && . /tmp/net.$netif.override
52 echo "IPADDR=$ip"
53 if strstr "$mask" "."; then
54 echo "NETMASK=$mask"
55 else
56 echo "PREFIX=$mask"
57 fi
58 [ -n "$gw" ] && echo "GATEWAY=$gw"
59 fi
60 } > /tmp/ifcfg/ifcfg-$netif
61
62 # bridge needs different things written to ifcfg
63 if [ -z "$bridge" ] && [ -z "$bond" ]; then
64 # standard interface
65 {
66 echo "HWADDR=$(cat /sys/class/net/$netif/address)"
67 echo "TYPE=Ethernet"
68 echo "NAME=\"Boot Disk\""
69 [ -n "$mtu" ] && echo "MTU=$mtu"
70 } >> /tmp/ifcfg/ifcfg-$netif
71 fi
72
73 if [ -n "$bond" ] ; then
74 # bond interface
75 {
76 # This variable is an indicator of a bond interface for initscripts
77 echo "BONDING_OPTS=\"$bondoptions\""
78 echo "NAME=\"Boot Disk\""
79 } >> /tmp/ifcfg/ifcfg-$netif
80
81 for slave in $bondslaves ; do
82 # Set ONBOOT=no to prevent initscripts from trying to setup already bonded physical interface
83 # write separate ifcfg file for the raw eth interface
84 {
85 echo "# Generated by dracut initrd"
86 echo "DEVICE=$slave"
87 echo "TYPE=Ethernet"
88 echo "ONBOOT=no"
89 echo "NETBOOT=yes"
90 echo "HWADDR=$(cat /sys/class/net/$slave/address)"
91 echo "SLAVE=yes"
92 echo "MASTER=$netif"
93 echo "NAME=$slave"
94 } >> /tmp/ifcfg/ifcfg-$slave
95 done
96 fi
97
98 if [ -n "$bridge" ] ; then
99 # bridge
100 {
101 echo "TYPE=Bridge"
102 echo "NAME=\"Boot Disk\""
103 } >> /tmp/ifcfg/ifcfg-$netif
104 if [ "$ethname" = "$bondname" ] ; then
105 {
106 # Set ONBOOT=no to prevent initscripts from trying to setup already bridged bond interface
107 echo "# Generated by dracut initrd"
108 echo "DEVICE=$bondname"
109 echo "ONBOOT=no"
110 echo "NETBOOT=yes"
111 # This variable is an indicator of a bond interface for initscripts
112 echo "BONDING_OPTS=\"$bondoptions\""
113 echo "BRIDGE=$netif"
114 echo "NAME=\"$bondname\""
115 } >> /tmp/ifcfg/ifcfg-$bondname
116 for slave in $bondslaves ; do
117 # write separate ifcfg file for the raw eth interface
118 # Set ONBOOT=no to prevent initscripts from trying to setup already bridged physical interface
119 {
120 echo "# Generated by dracut initrd"
121 echo "DEVICE=$slave"
122 echo "TYPE=Ethernet"
123 echo "ONBOOT=no"
124 echo "NETBOOT=yes"
125 echo "HWADDR=$(cat /sys/class/net/$slave/address)"
126 echo "SLAVE=yes"
127 echo "MASTER=$bondname"
128 echo "NAME=$slave"
129 } >> /tmp/ifcfg/ifcfg-$slave
130 done
131 else
132 # write separate ifcfg file for the raw eth interface
133 {
134 echo "# Generated by dracut initrd"
135 echo "DEVICE=$ethname"
136 echo "TYPE=Ethernet"
137 echo "ONBOOT=no"
138 echo "NETBOOT=yes"
139 echo "HWADDR=$(cat /sys/class/net/$ethname/address)"
140 echo "BRIDGE=$netif"
141 echo "NAME=$ethname"
142 } >> /tmp/ifcfg/ifcfg-$ethname
143 fi
144 fi
145 i=1
146 for ns in $(getargs nameserver); do
147 echo "DNS${i}=${ns}" >> /tmp/ifcfg/ifcfg-$netif
148 i=$((i+1))
149 done
150 done
151
152 # Pass network opts
153 mkdir -m 0755 -p /run/initramfs/state/etc/sysconfig/network-scripts
154 mkdir -m 0755 -p /run/initramfs/state/var/lib/dhclient
155 echo "files /etc/sysconfig/network-scripts" >> /run/initramfs/rwtab
156 echo "files /var/lib/dhclient" >> /run/initramfs/rwtab
157 {
158 cp /tmp/net.* /run/initramfs/
159 cp /tmp/net.$netif.resolv.conf /run/initramfs/state/etc/resolv.conf
160 cp -a -t /run/initramfs/state/etc/sysconfig/network-scripts/ /tmp/ifcfg/*
161 cp /tmp/ifcfg-leases/* /run/initramfs/state/var/lib/dhclient
162 } > /dev/null 2>&1