summaryrefslogtreecommitdiffstats
path: root/modules.d/systemd-networkd-ext/hooks/configure-dhcp-for-newroot.sh
blob: b980372970f0975d7f67cc393b07a5ba6614b5e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
#
# This hook creates a networkd configuration for the NEWROOT
# * DHCP configuration for the boot interface
# * Set static hostname via /etc/hostname
# This uses the configuration file generated by the parse-kcl-for-networkd.sh
# hook Note: on systemd v219, UseDomains is bugged and does not seem to do
# anything thus that option is complemented with the KCL's domain to hopefully
# garantee one of these will take effect.

. "/run/openslx/network.conf"

NETWORKD_DIR="/etc/systemd/network"
mkdir -p "${NEWROOT}/${NETWORKD_DIR}"

# First, copy over every configuration made so far,
# excluding the physical interface if bridged
cp "${NETWORKD_DIR}/${SLX_BRIDGE}"* "${NEWROOT}/${NETWORKD_DIR}"

# Now determine which network configuration we are running
# VLANs are already configured to DHCP, so nothing to do
if [ -z "${SLX_VLAN_ID}" ]; then
	# In both bridged and non-bridged mode, we need to adjust
	# the configuration to DHCP for stage4
	MATCH_LINE=
	MATCH_LINE_EXTRA=
	MAIN_NET_CONF=
	if [ -n "${SLX_BRIDGE}" ]; then
		MATCH_LINE="Name=${SLX_BRIDGE}"
		MAIN_NETIF_CONF="${NETWORKD_DIR}/${SLX_BRIDGE}.network"
	elif [ -n "${SLX_PXE_MAC}" ]; then
		MATCH_LINE="MACAddress=${SLX_PXE_MAC}"
		MATCH_LINE_EXTRA="Path=pci*"
		MAIN_NETIF_CONF="${NETWORKD_DIR}/${SLX_PXE_NETIF}.network"
	else
		emergency_shell "Could not determine network configuration mode!"
	fi
	# backup current network config for later reference before overwritting it
	if [ -e "${MAIN_NETIF_CONF}" ]; then
		mv "${NEWROOT}/${MAIN_NETIF_CONF}" "${NEWROOT}/${MAIN_NETIF_CONF}.stage3"
	else
		emergency_shell "Failed to find main networkd configuration: ${MAIN_NETIF_CONF}"
	fi
	# Now generate a DHCP configuration for the main interface	
	cat <<-EOF > "${NEWROOT}/${MAIN_NETIF_CONF}"
		[Match]
		${MATCH_LINE}
		${MATCH_LINE_EXTRA}

		[Network]
		DHCP=ipv4
		LinkLocalAddressing=no
		IPv6AcceptRA=no

		[DHCP]
		UseDNS=true
		UseDomains=true
		UseHostname=true
		CriticalConnection=true
	EOF
fi

# Workaround: UseHostname seems broken, so manually set the hostname for now...
[ -n "${SLX_HOSTNAME}" ] && echo "${SLX_HOSTNAME}" > $NEWROOT/etc/hostname