Skip to content

Commit

Permalink
bsp: firstrun: Use Netplan for setting fixed MACs
Browse files Browse the repository at this point in the history
Also remove `$BRANCH == dev` line since dev branch does not exist anymore
  • Loading branch information
ColorfulRhino committed Jun 20, 2024
1 parent 3c8d41b commit 03aba6d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
MACAddress=NONE.ABCD.NONE # Will never match with anything
66 changes: 36 additions & 30 deletions packages/bsp/common/usr/lib/armbian/armbian-common
Original file line number Diff line number Diff line change
Expand Up @@ -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##*/}"
Expand All @@ -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

MACADDR=$(/sbin/ip link | grep -A1 ${DEVNAME} | awk -F" " '/ether / {print $2}')

[[ -z $MACADDR ]] && get_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
local netplan_config_file="/etc/netplan/20-eth-fixed-mac.yaml"

# Initialize a new Netplan config file
cat <<- EOF > "${netplan_config_file}"
network:
version: 2
ethernets:
EOF

# 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

# 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
}
7 changes: 3 additions & 4 deletions packages/bsp/common/usr/lib/armbian/armbian-firstrun
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 03aba6d

Please sign in to comment.