From 6b7284be3e18f680cdee9fb46ab7f784e9501079 Mon Sep 17 00:00:00 2001 From: ColorfulRhino <131405023+ColorfulRhino@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:33:30 +0200 Subject: [PATCH] bsp: firstrun: Use Netplan for setting fixed MACs Also remove `$BRANCH == dev` line since dev branch does not exist anymore --- .../systemd/network/00-armbian-readme.network | 2 +- .../bsp/common/usr/lib/armbian/armbian-common | 60 ++++++++++--------- .../common/usr/lib/armbian/armbian-firstrun | 7 +-- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/extensions/network/config-networkd/systemd/network/00-armbian-readme.network b/extensions/network/config-networkd/systemd/network/00-armbian-readme.network index 256431e455..8d51d51426 100644 --- a/extensions/network/config-networkd/systemd/network/00-armbian-readme.network +++ b/extensions/network/config-networkd/systemd/network/00-armbian-readme.network @@ -5,4 +5,4 @@ # The match section is just to ignore a warning due to empty file [Match] -MACAddress=NONE.ABCD.NONE # Will never match with anything \ No newline at end of file +MACAddress=NONE.ABCD.NONE # Will never match with anything diff --git a/packages/bsp/common/usr/lib/armbian/armbian-common b/packages/bsp/common/usr/lib/armbian/armbian-common index 2f1eb72943..0509a1ef32 100755 --- a/packages/bsp/common/usr/lib/armbian/armbian-common +++ b/packages/bsp/common/usr/lib/armbian/armbian-common @@ -9,6 +9,8 @@ # Functions: # # show_motd_warning +# generate_random_mac +# set_fixed_mac export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin my_name="${0##*/}" @@ -23,38 +25,42 @@ EOT chmod +x /etc/update-motd.d/90-warning } # show_motd_warning -get_random_mac () +# Generate a random MAC address +generate_random_mac () { -local prefixes=("02" "06" "0A" "0E") -local random=$(shuf -i 0-3 -n 1) -MACADDR=$(printf ${prefixes[$random]}':%02X:%02X:%02X:%02X:%02X\n' $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256]) + # These prefixes are 4 sets of Locally Administered Address Ranges which can be used on the network without fear of conflict + local prefixes=("02" "06" "0A" "0E") + local random=$(shuf -i 0-3 -n 1) + MACADDR=$(printf ${prefixes[$random]}':%02X:%02X:%02X:%02X:%02X\n' $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256] $[RANDOM%256]) } - -# set fixed IP address from first randomly assigned one. If nothing is detected, generate one. +# Set a fixed random MAC for each Ethernet interface set_fixed_mac () { - if [ -n "$(command -v nmcli)" ]; then - CONNECTION="$(nmcli -f UUID,ACTIVE,DEVICE,TYPE connection show --active | tail -n1)" - UUID=$(awk -F" " '/ethernet/ {print $1}' <<< "${CONNECTION}") - DEVNAME=$(awk -F" " '/ethernet/ {print $3}' <<< "${CONNECTION}") - else - DEVNAME=eth0 - fi + local netplan_config_file="/etc/netplan/20-eth-fixed-mac.yaml" - MACADDR=$(/sbin/ip link | grep -A1 ${DEVNAME} | awk -F" " '/ether / {print $2}') + # Initialize a new Netplan config file + cat <<- EOF > "${netplan_config_file}" + network: + version: 2 + ethernets: + EOF - [[ -z $MACADDR ]] && get_random_mac + # Iterate over each Ethernet interface + for iface in $(ip link show | awk '/^[0-9]+: [^:]+:/ {iface=$2; getline; if ($1 == "link/ether") print iface}' | tr -d ':') + do + generate_random_mac - if [[ -n "$(command -v nmcli)" && -n $UUID ]]; then - nmcli connection modify "$UUID" ethernet.cloned-mac-address "$MACADDR" - nmcli connection modify "$UUID" -ethernet.mac-address "" - nmcli connection down "$UUID" >/dev/null 2>&1 - nmcli connection up "$UUID" >/dev/null 2>&1 - elif [[ -f /etc/systemd/network/$DEVNAME.network ]]; then - if ! grep '^ *MACAddress=' /etc/systemd/network/$DEVNAME.network > /dev/null; then - sed -i "s/#MACAddress=/MACAddress=$MACADDR/g" /etc/systemd/network/$DEVNAME.network - fi - fi - return 0 -} # set fixed mac to the 1st active network adapter + # Append the MAC address configuration for the interface to the Netplan configuration file + cat <<- EOF >> "${netplan_config_file}" + $iface: + macaddress: $MACADDR + EOF + done + + # Change the file permissions according to https://netplan.readthedocs.io/en/stable/security/ + chmod 600 "${netplan_config_file}" + + # Apply the Netplan configuration + netplan apply +} diff --git a/packages/bsp/common/usr/lib/armbian/armbian-firstrun b/packages/bsp/common/usr/lib/armbian/armbian-firstrun index 4b7134b57b..21074019f5 100755 --- a/packages/bsp/common/usr/lib/armbian/armbian-firstrun +++ b/packages/bsp/common/usr/lib/armbian/armbian-firstrun @@ -77,9 +77,9 @@ case "$1" in # randomize mac in armbianEnv.txt if [[ -f /boot/armbianEnv.txt ]]; then - get_random_mac + generate_random_mac sed -i "s/^ethaddr=.*/ethaddr=$MACADDR/" /boot/armbianEnv.txt - get_random_mac + generate_random_mac sed -i "s/^eth1addr=.*/eth1addr=$MACADDR/" /boot/armbianEnv.txt fi @@ -99,7 +99,7 @@ case "$1" in sed -i "s/^PORT=.*/PORT=ttyS3/" /etc/default/ap6212 ;; "Orange Pi Zero"|"NanoPi Duo"|"Sunvell R69") - get_random_mac + generate_random_mac echo "options xradio_wlan macaddr=${MACADDR}" >/etc/modprobe.d/xradio_wlan.conf echo -e "\n### [firstrun] Use MAC address ${MACADDR} for Wi-Fi from now" >>${Log} cd /etc/network/ && ln -sf interfaces.network-manager interfaces @@ -129,7 +129,6 @@ case "$1" in # varios temporary hardware workarounds [[ $LINUXFAMILY == rk3399 || $LINUXFAMILY == rockchip64 ]] && [[ $BOARD != helios64 && $BOARD != khadas-edge ]] && set_fixed_mac - [[ $BRANCH == dev && $LINUXFAMILY == rockchip ]] && set_fixed_mac [[ $LINUXFAMILY == meson ]] && set_fixed_mac [[ $LINUXFAMILY == meson64 ]] && set_fixed_mac systemctl disable armbian-firstrun