Skip to content

Commit

Permalink
chore: extend headless_nm for armbian based images
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Wendel <[email protected]>
  • Loading branch information
KwadFan committed Aug 4, 2024
1 parent a3a82d9 commit 027f4ba
Showing 1 changed file with 56 additions and 13 deletions.
69 changes: 56 additions & 13 deletions src/modules/headless_nm/filesystem/usr/local/bin/headless_nm
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,51 @@
set -e

# Global Vars
#CMDLINE="/boot/firmware/cmdline.txt"
DISTRO=""
CMDLINE="/boot/firmware/cmdline.txt"
ARMBIANENV="/boot/armbianEnv.txt"
UUID=""
PSK_HASH=""
KEYMGMT="wpa-psk"
CONNFILE=/etc/NetworkManager/system-connections/preconfigured.nmconnection
SETUPFILE="/boot/firmware/headless_nm.txt"
SETUPFILE="$(find /boot -name "headless_nm.txt")"
REGDOMAIN=""
CFG80211="/etc/modprobe.d/cfg80211.conf"


# functions
log() {
local msg
msg="${1}"
logger -t headless_nm "${msg}"
}

get_distro() {
local armbian rpi
armbian="$(find /boot -name "$(basename ${ARMBIANENV})")"
rpi="$(find /boot -name "$(basename ${CMDLINE})")"
log "Determine distribution ..."
if [[ -n "${armbian}" ]]; then
DISTRO="armbian"
elif [[ -n "${rpi}" ]]; then
DISTRO="rpi"
else
DISTRO=""
log "Could not determine distribution ... Exiting!"
exit 1
fi
if [[ -n "${DISTRO}" ]]; then
log "Using setup profile: ${DISTRO} ..."
fi
}

get_config() {
# shellcheck disable=SC1090
source "${SETUPFILE}"
}

gen_uuid() {
log "Generating UUID for connection..."
UUID="$(uuid -v4)"
declare -r UUID
}
Expand All @@ -55,7 +83,7 @@ type=wifi
[wifi]
mode=infrastructure
ssid=${SSID}
hidden=${HIDDEN}
hidden=${HIDDEN,,}
[ipv4]
method=auto
[ipv6]
Expand All @@ -81,29 +109,44 @@ gen_psk() {
}

set_regdomain() {
log "Set registration domain to '${REGDOMAIN}' ..."
# Shameless borrowed from
# https://github.com/RPi-Distro/raspi-config/blob/d22643ade3851c42648f3676e577a622c34af49a/raspi-config#L830C3-L832C5
if ! ischroot; then
iw reg set "$REGDOMAIN"
iw reg set "${REGDOMAIN}"
fi
# set permanent
# Shameless borrowed from
# https://github.com/RPi-Distro/raspi-config/blob/d22643ade3851c42648f3676e577a622c34af49a/raspi-config#L822C3-L825C15
sed -i \
-e "s/\s*cfg80211.ieee80211_regdom=\S*//" \
-e "s/\(.*\)/\1 cfg80211.ieee80211_regdom=$REGDOMAIN/" \
"${CMDLINE}"
case "${DISTRO}" in
"rpi")
# Shameless borrowed from
# https://github.com/RPi-Distro/raspi-config/blob/d22643ade3851c42648f3676e577a622c34af49a/raspi-config#L822C3-L825C15
sed -i \
-e "s/\s*cfg80211.ieee80211_regdom=\S*//" \
-e "s/\(.*\)/\1 cfg80211.ieee80211_regdom=$REGDOMAIN/" \
"${CMDLINE}"
;;
"armbian")
if [[ -f "${CFG80211}" ]]; then
rm -f "${CFG80211}"
fi
echo "options cfg80211 ieee80211_regdom=${REGDOMAIN}" > "${CFG80211}"
;;
esac
}

# MAIN
main() {
# Determine base distribution
get_distro

# Check setup file is present
if [[ -f "${SETUPFILE}" ]]; then
printf "Configuration file found in %s, continuing...\n" "${SETUPFILE}"
log "Configuration file found in ${SETUPFILE}, continuing...\n"
else
printf "Configuration file not found in %s, Exiting...\n" "${SETUPFILE}"
log "Configuration file not found! Nothing to do, exiting...\n"
exit 0
fi

# Generate UUID
gen_uuid

Expand Down Expand Up @@ -131,7 +174,7 @@ main() {
PSK_HASH=""
PASSWORD=""
SSID=""
printf "Configuration successful written...\n"
log "Configuration successful written ..."
rm -f "${SETUPFILE}"
}

Expand Down

0 comments on commit 027f4ba

Please sign in to comment.