summaryrefslogtreecommitdiffstats
path: root/modules.d/slx-network/hooks/parse-ipxe-network-kcl.sh
blob: 56abb680fd0378f382ec5b174fc152435d975968 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
#
# TODO VLAN support

command -v getarg >/dev/null || . /lib/dracut-lib.sh

# static names for the boot interface and its bridge
declare -rg BOOTIF_NAME="boot0"
declare -rg BRIDGE_NAME="br0"
declare -rg RUNTIME_CONF="/run/openslx/network.conf"
mkdir -p "${RUNTIME_CONF%/*}"
# Get all the ip-related arguments from the KCL
parse_kernel_command_line() {
	## KCL "BOOTIF": MAC address of the interface that DHCP'ed during PXE
	declare -g BOOTIF="$(getarg BOOTIF=)"
	# Remove the hardware type prefix of BOOTIF if it has 20 chars to get
	# the plain MAC address. The hardware type prefix has length 3, e.g. "01-".
	if [ -n "${BOOTIF}" ] && [ ${#BOOTIF} -eq 20 ]; then
		BOOTIF="${BOOTIF#???}"
		BOOTIF="${BOOTIF//-/:}"
	fi
	readonly BOOTIF

	## KCL "ip": is expected in following format (syslinux IPAPPEND3):
	declare -rg IPCONF="$(getarg ip=)"
	# <CLIENT_IP>:<PXE_SERVER_IP>:<GATEWAY_IP>:<NETMASK>
	declare -g CLIENT_IP=
	declare -g SERVER_IP=
	declare -g GATEWAY_IP=
	declare -g NETMASK=
	read -r CLIENT_IP SERVER_IP GATEWAY_IP NETMASK \
		<<< $( awk -F: '{print $1" "$2" "$3" "$4}' <<< "${IPCONF}" )
	readonly CLIENT_IP SERVER_IP GATEWAY_IP NETMASK

	# Calculate routing prefix from netmask
	declare -rg ROUTING_PREFIX="$(ipcalc -s -p "$CLIENT_IP" "$NETMASK")"
	
	# KCL "hostname"
	declare -rg HOSTNAME="$(getarg hostname=)"
	[ -n "$HOSTNAME" ] && echo "$HOSTNAME" > /proc/sys/kernel/hostname

	# KCL "dns"
	declare -rg DNS="$(getarg dns=)"

	# KCL "domain"
	declare -rg DOMAIN="$(getarg domain=)"

	# VLAN tag
	declare -rg VLAN="$(getarg vlan=)"
	if [ -n "$VLAN" ]; then
		modprobe 8021q || warn "VLAN mode detected but failed to load 8021q!"
	fi

	# Bridged mode?
	grep -wqE 'bridged' /proc/cmdline && declare -rg BRIDGED="y"
}

save_network_config() {
	cat <<-EOF > "${RUNTIME_CONF}"
		SLX_PXE_CLIENT_IP='${CLIENT_IP}'
		SLX_PXE_SERVER_IP='${SERVER_IP}'
		SLX_PXE_GATEWAY='${GATEWAY_IP}'
		SLX_PXE_NETMASK='${NETMASK}'
		SLX_PXE_MAC='${BOOTIF}'
		SLX_PXE_NETIF='${BOOTIF_NAME}'
		SLX_PXE_DNS='${DNS}'
		SLX_PXE_HOSTNAME='${HOSTNAME}'
		SLX_PXE_DOMAIN='${DOMAIN}'
		SLX_BRIDGE='${BRIDGED:+${BRIDGE_NAME}}'
		SLX_VLAN_ID='${VLAN}'
	EOF
}

# Create udev rule to rename the PXE boot interface to BOOTIF_NAME
create_udev_bootif_name_rule() {
	if [ -z "${BOOTIF}" ]; then
		echo "No BOOTIF set, was it present in the kernel command line?"
		return 1
	fi
  # priority 70 < 80-net-name-slot.rules.
	echo 'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="'${BOOTIF}'", NAME="'${BOOTIF_NAME}'"' > /etc/udev/rules.d/70-pxe-boot-interface.rules
}

## MAIN ##
# Get IP config from KCL
parse_kernel_command_line

# Save network config for later use
save_network_config &

# Create the udev rule to rename the boot interface to the declared BOOTIF_NAME
create_udev_bootif_name_rule &

/sbin/initqueue --settled /usr/local/bin/setup-bootif-network
/sbin/initqueue --finished [ -e "/.network" ]