Skip to content

Commit ca1e125

Browse files
committed
[baremetal] Add dispatcher script to statically configure DHCP address
We have a feature request for nodes to be able to initially get assigned their IP addresses via infinite DHCP leases, but then to have those addresses statically configured on the node so that if the DHCP server is unavailable for any reason they don't lose their network configuration. This adds a dispatcher script for NetworkManager that does so. It looks at the DHCP assigned network settings and adds static configuration with the same values.
1 parent 824ddee commit ca1e125

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
mode: 0755
2+
path: "/etc/NetworkManager/dispatcher.d/30-static-dhcp"
3+
contents:
4+
inline: |
5+
#!/bin/bash
6+
set -ex -o pipefail
7+
8+
if [[ "{{ .NetworkType }}" == "OVNKubernetes" && "$CONNECTION_ID" == "Wired Connection" ]]
9+
then
10+
>&2 echo "Refusing to modify default connection."
11+
exit 0
12+
fi
13+
14+
if [ -z ${DHCP4_IP_ADDRESS:-} ]
15+
then
16+
>&2 echo "Not a DHCP4 address. Ignoring."
17+
exit 0
18+
fi
19+
20+
if [ ${DHCP4_DHCP_LEASE_TIME:-0} -lt 4294967295 ]
21+
then
22+
>&2 echo "Not an infinite DHCP4 lease. Ignoring."
23+
exit 0
24+
fi
25+
26+
IPS=($IP4_ADDRESS_0)
27+
CIDR=${IPS[0]}
28+
GATEWAY=${IPS[1]}
29+
30+
TYPE=$(nmcli --get-values connection.type connection show "$CONNECTION_ID")
31+
32+
if ! nmcli con show inf-lease-to-static
33+
then
34+
nmcli con add type "$TYPE" con-name inf-lease-to-static
35+
fi
36+
nmcli con mod inf-lease-to-static \
37+
conn.interface "$1" \
38+
connection.autoconnect yes \
39+
ipv4.addresses "$CIDR" \
40+
ipv4.method manual \
41+
ipv4.gateway "$GATEWAY" \
42+
ipv4.dns "$IP4_NAMESERVERS"
43+
44+
if [ -n "$IP4_DOMAINS" ]; then
45+
nmcli con mod inf-lease-to-static ipv4.dns-search "$IP4_DOMAINS"
46+
fi
47+
plus=''
48+
for i in $(seq 0 $(($IP4_NUM_ROUTES-1)) )
49+
do
50+
varname="IP4_ROUTE_$i"
51+
nmcli con mod inf-lease-to-static ${plus}ipv4.routes "${!varname}"
52+
plus='+'
53+
done
54+
55+
nmcli con up inf-lease-to-static
56+
57+
# Copy it from the OverlayFS mount to the persistent lowerdir
58+
cp "/etc/NetworkManager/system-connections-merged/inf-lease-to-static.nmconnection" /etc/NetworkManager/system-connections
59+
60+
if [ -n "${DHCP4_HOST_NAME:-}" ]
61+
then
62+
hostnamectl set-hostname --static --transient "$DHCP4_HOST_NAME"
63+
fi

0 commit comments

Comments
 (0)