diff options
Diffstat (limited to 'builder/modules.d/systemd-networkd-ext/hooks/parse-kcl-for-networkd.sh')
-rw-r--r-- | builder/modules.d/systemd-networkd-ext/hooks/parse-kcl-for-networkd.sh | 188 |
1 files changed, 119 insertions, 69 deletions
diff --git a/builder/modules.d/systemd-networkd-ext/hooks/parse-kcl-for-networkd.sh b/builder/modules.d/systemd-networkd-ext/hooks/parse-kcl-for-networkd.sh index cdf080a3..0f9c698f 100644 --- a/builder/modules.d/systemd-networkd-ext/hooks/parse-kcl-for-networkd.sh +++ b/builder/modules.d/systemd-networkd-ext/hooks/parse-kcl-for-networkd.sh @@ -1,98 +1,148 @@ #!/bin/bash # -# This script was mostly stolen from 40network/parse-ip-opts.sh. Its -# actions are adapted to write .network files to /etc/systemd/network -# in the initramfs instead o +# This script was mostly stolen from 40network/parse-ip-opts.sh. It was +# adapted to generate .network files for systemd-networkd using the IP +# configuration from PXE/Syslinux in addition to domain, dns and hostname command -v getarg >/dev/null || . /lib/dracut-lib.sh -main_work() { - local IPCONF="$(getarg ip=)" - local BOOTIF="$(getarg BOOTIF=)" +# static names for the boot interface and its bridge +# TODO could be configurable from the KCL +declare -rg BOOTIF_NAME="boot0" +declare -rg BRIDGE_NAME="br0" +#declare -rg BRIDGED="bridged" - if [ -z "${IPCONF}" ] && [ -z "${BOOTIF}" ]; then - # No ip= argument(s) given by PXE, TODO try dhcp - return; - fi +# 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-". + [ -n "${BOOTIF}" ] && [ ${#BOOTIF} -eq 20 ] && BOOTIF="${BOOTIF#???}" + BOOTIF="$(tr '-' ':' <<< $BOOTIF)" + readonly BOOTIF - # ip= is expected in following format (syslinux IPAPPEND3): + ## KCL "ip": is expected in following format (syslinux IPAPPEND3): + declare -rg IPCONF="$(getarg ip=)" # <CLIENT_IP>:<PXE_SERVER_IP>:<GATEWAY_IP>:<NETMASK> - local CLIENT_IP= - local SERVER_IP= - local GATEWAY_IP= - local 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 - # BOOTIF= contains the MAC address of the boot interface - # and a hardware type prefix of length 2, e.g. "01-" - # So if BOOTIF has 20 chars, then we got the prefix. - # This needs to be stripped to get the MAC address only - [ -n "${BOOTIF}" ] && [ ${#BOOTIF} -eq 20 ] && BOOTIF="${BOOTIF#???}" - BOOTIF="$(tr '-' ':' <<< $BOOTIF)" - - # TODO sanity checks - - # Taken from parse-ip-opts.sh - # Convert the netmask to CIDR notation + # Taken from parse-ip-opts.sh: Convert the netmask to CIDR notation + declare -g CIDR if [[ "x$NETMASK" =~ ^x[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then CIDR=$(mask2cidr "$NETMASK") elif [ -n "$NETMASK" -a "x${NETMASK//[0-9]/}" = 'x' ]; then # The mask is already a prefix length (uint), so validate it [[ "x$CLIENT_IP" == x*:*:* && "$NETMASK" -le 128 || "$NETMASK" -le 32 ]] && CIDR=$NETMASK fi + readonly CIDR + # KCL "hostname" + declare -rg HOSTNAME="$(getarg hostname=)" + [ -n "$HOSTNAME" ] && echo "$HOSTNAME" > /proc/sys/kernel/hostname - # Generate network and link file for networkd - NETWORK_FILE=/etc/systemd/network/01-boot-lan.network - mkdir -p $(dirname $NETWORK_FILE) - echo '[Match]' > $NETWORK_FILE - echo 'Name=bootnet' >> $NETWORK_FILE - # Hardcode bootnet as our boot interface - [ -n "$BOOTIF" ] && echo "MACAddress=$BOOTIF" >> $NETWORK_FILE - echo '[Network]' >> $NETWORK_FILE - echo 'Bridge=br0' >> $NETWORK_FILE - - ## Boot interface has 'bootnet' as dedicated name - LINK_FILE=/etc/systemd/network/00-boot-lan.link - echo '[Match]' > $LINK_FILE - [ -n "$BOOTIF" ] && echo "MACAddress=$BOOTIF" >> $LINK_FILE - echo '[Link]' >> $LINK_FILE - echo "Name=bootnet" >> $LINK_FILE - - # HACK pro bridge setup - BRIDGE_DEV_CONF=/etc/systemd/network/10-bridge-lan.netdev - echo '[NetDev]' > $BRIDGE_DEV_CONF - echo 'Name=br0' >> $BRIDGE_DEV_CONF - echo 'Kind=bridge' >> $BRIDGE_DEV_CONF - - BRIDGE_IF_CONF=/etc/systemd/network/11-bridge-lan.network - echo '[Match]' > $BRIDGE_IF_CONF - echo "Name=br0" >> $BRIDGE_IF_CONF - echo '[Network]' >> $BRIDGE_IF_CONF - [ -n "$CLIENT_IP" ] && echo "Address=${CLIENT_IP}/${CIDR:-24}" >> $BRIDGE_IF_CONF - [ -n "$GATEWAY_IP" ] && echo "Gateway=${GATEWAY_IP}" >> $BRIDGE_IF_CONF + # KCL "dns" + declare -rg DNS="$(getarg dns=)" + # KCL "domain" + declare -rg DOMAIN="$(getarg domain=)" +} +# Helper to echo a mac address matching [Match] section +print_match_mac() { + [ $# -ne 1 ] && echo "No MAC given!" && return 1 + echo '[Match]' + echo "MACAddress=${1}" +} +# Helper to echo the static configuration, including +# DNS and Domain if given per KCL +print_static_ip_conf() { + [ $# -ne 2 ] && echo "Need 2 args: <ip/cidr> <gateway>. Given: $@" && return 1 + echo "Address=${1}" + echo "Gateway=${2}" + [ -n "${DNS}" ] && echo "DNS=${DNS}" + [ -n "${DOMAIN}" ] && echo "Domains=${DOMAIN}" +} + +# Create udev rule to rename the PXE boot interface to BOOTIF_NAME +create_bootif_udev_name_rule() { + [ -z "${BOOTIF}" ] && echo "No BOOTIF set, was it present in the kernel command line?" && return 1 + # priority 70 < 80-net-name-slot.rules. + echo 'SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="'${BOOTIF}'\ + ", NAME="'${BOOTIF_NAME}'"' > /etc/udev/rules.d/70-pxe-boot-interface.rules + +} + +## Generate network and link file(s) for networkd +# Checks if an IP configuration was forwarded by +generate_networkd_config() { + mkdir -p "/etc/systemd/network" + local BOOTIF_NETWORK_CONF="/etc/systemd/network/01-boot-lan.network" + print_match_mac "${BOOTIF}" > "${BOOTIF_NETWORK_CONF}" + echo '[Network]' >> "${BOOTIF_NETWORK_CONF}" + if [ -n "${BRIDGED}" ]; then + echo "Bridge=${BRIDGE_NAME}" >> "${BOOTIF_NETWORK_CONF}" + elif [ -n "${CLIENT_IP}" -a -n "${GATEWAY_IP}" ]; then + print_static_ip_conf "${CLIENT_IP}/${CIDR:-24}" "${GATEWAY_IP}" >> "${BOOTIF_NETWORK_CONF}" + else + echo "DHCP=ipv4" >> "${BRIDGE_IF_CONF}" + fi + # we're done unless running in bridged mode + [ -n "${BRIDGED}" ] || return 0 + + # bridge setup + local BRIDGE_DEV_CONF="/etc/systemd/network/10-bridge-lan.netdev" + echo '[NetDev]' > "${BRIDGE_DEV_CONF}" + echo "Name=${BRIDGE_NAME}" >> "${BRIDGE_DEV_CONF}" + echo 'Kind=bridge' >> "${BRIDGE_DEV_CONF}" + + local BRIDGE_NETWORK_CONF="/etc/systemd/network/11-bridge-lan.network" + echo '[Match]' > "${BRIDGE_NETWORK_CONF}" + echo "Name=${BRIDGE_NAME}" >> "${BRIDGE_NETWORK_CONF}" + echo '[Network]' >> "${BRIDGE_NETWORK_CONF}" + if [ -n "${CLIENT_IP}" -a -n "${GATEWAY_IP}" ]; then + print_static_ip_conf "${CLIENT_IP}/${CIDR:-24}" "${GATEWAY_IP}" >> "${BRIDGE_NETWORK_CONF}" + else + # bad/no IP info, fallback to DHCP + echo "DHCP=ipv4" >> "${BRIDGE_NETWORK_CONF}" + fi + return 0 } # from parse-ip-opts.sh # Takes a netmask and outputs the corresponding CIDR # e.g. # mask2cidr 255.255.255.0 # returns: 24 -function mask2cidr() { - local -i bits=0 - for octet in ${1//./ }; do - for i in {0..8}; do - [ "$octet" -eq $(( 256 - (1 << i) )) ] && bits+=$((8-i)) && break - done - [ $i -eq 8 -a "$octet" -ne 0 ] && warn "Bad netmask $mask" && return - [ $i -gt 0 ] && break - done - echo $bits +mask2cidr() { + local -i bits=0 + for octet in ${1//./ }; do + for i in {0..8}; do + [ "$octet" -eq $(( 256 - (1 << i) )) ] && bits+=$((8-i)) && break + done + [ $i -eq 8 -a "$octet" -ne 0 ] && warn "Bad netmask $mask" && return + [ $i -gt 0 ] && break + done + echo $bits } -main_work +## MAIN ## +# Get IP config from KCL +parse_kernel_command_line + +# Create the udev rule to rename the boot interface to the declared BOOTIF_NAME +rename_boot_if -# HACK to wait for network during udev loop... -touch /lib/dracut/need-initqueue -/sbin/initqueue --finished /bin/busybox timeout -t 10 /lib/systemd/systemd-networkd-wait-online -i br0 +# Generate config files for networkd +generate_networkd_config +# Make dracut wait for network during the udev loop (initqueue) to make +# sure we have network access in the pre-mount hook as it is needed +# to get configurations and the root filesystem +NETIF= +[ -n "${BOOTIF_NAME}" ] && NETIF="${BOOTIF_NAME}" +[ -n "${BRIDGED}" ] && [ -n "${BRIDGE_NAME}" ] && NETIF="${BRIDGE_NAME}" +/sbin/initqueue --finished /bin/busybox timeout -t 10 /lib/systemd/systemd-networkd-wait-online -i "${NETIF}" |