mirror of
https://github.com/armbian/build
synced 2025-09-24 19:47:06 +07:00
bsp: firstrun: Use Netplan for setting fixed MACs
Also remove `$BRANCH == dev` line since dev branch does not exist anymore
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user