|
| 1 | +#!/usr/bin/env bash |
| 2 | +#### headless_nm |
| 3 | +#### |
| 4 | +#### Written by Stephan Wendel aka KwadFan <[email protected]> |
| 5 | +#### Copyright 2024 - till today |
| 6 | +#### https://github.com/mainsail-crew/MainsailOS |
| 7 | +#### |
| 8 | +#### This File is distributed under GPLv3 |
| 9 | +#### |
| 10 | + |
| 11 | +#### Description: |
| 12 | +#### This is a wrapper for network manager to be able to setup |
| 13 | +#### Wifi connections on headless SBC. |
| 14 | +#### Takes setup from a file in /boot (/boot/firmware) and recreates |
| 15 | +#### Raspberrys 'preconfigured.nmconnection' |
| 16 | + |
| 17 | +#### Copyright hint: This file contains code snippets from |
| 18 | +#### Raspberrys raspi-config therefore it applies also there license |
| 19 | +#### and copyrights! |
| 20 | +#### See https://github.com/RPi-Distro/raspi-config/blob/bookworm/LICENSE |
| 21 | + |
| 22 | +#### It also reuses parts of raspberry-sys-mods |
| 23 | +#### See https://github.com/RPi-Distro/raspberrypi-sys-mods/blob/bookworm/usr/lib/raspberrypi-sys-mods/imager_custom |
| 24 | + |
| 25 | +set -e |
| 26 | + |
| 27 | +# Global Vars |
| 28 | +DISTRO="" |
| 29 | +CMDLINE="/boot/firmware/cmdline.txt" |
| 30 | +ARMBIANENV="/boot/armbianEnv.txt" |
| 31 | +UUID="" |
| 32 | +PSK_HASH="" |
| 33 | +KEYMGMT="wpa-psk" |
| 34 | +CONNFILE=/etc/NetworkManager/system-connections/preconfigured.nmconnection |
| 35 | +SETUPFILE="$(find /boot -name "headless_nm.txt")" |
| 36 | +REGDOMAIN="" |
| 37 | +CFG80211="/etc/modprobe.d/cfg80211.conf" |
| 38 | + |
| 39 | + |
| 40 | +# functions |
| 41 | +log() { |
| 42 | + local msg |
| 43 | + msg="${1}" |
| 44 | + logger -t headless_nm "${msg}" |
| 45 | +} |
| 46 | + |
| 47 | +get_distro() { |
| 48 | + local armbian rpi |
| 49 | + armbian="$(find /boot -name "$(basename ${ARMBIANENV})")" |
| 50 | + rpi="$(find /boot -name "$(basename ${CMDLINE})")" |
| 51 | + log "Determine distribution ..." |
| 52 | + if [[ -n "${armbian}" ]]; then |
| 53 | + DISTRO="armbian" |
| 54 | + elif [[ -n "${rpi}" ]]; then |
| 55 | + DISTRO="rpi" |
| 56 | + else |
| 57 | + DISTRO="" |
| 58 | + log "Could not determine distribution ... Exiting!" |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + if [[ -n "${DISTRO}" ]]; then |
| 62 | + log "Using setup profile: ${DISTRO} ..." |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +get_config() { |
| 67 | + # shellcheck disable=SC1090 |
| 68 | + source "${SETUPFILE}" |
| 69 | +} |
| 70 | + |
| 71 | +gen_uuid() { |
| 72 | + log "Generating UUID for connection..." |
| 73 | + UUID="$(uuid -v4)" |
| 74 | + declare -r UUID |
| 75 | +} |
| 76 | + |
| 77 | +gen_nmconnection() { |
| 78 | +cat <<- EOF >"${CONNFILE}" |
| 79 | +[connection] |
| 80 | +id=preconfigured |
| 81 | +uuid=${UUID} |
| 82 | +type=wifi |
| 83 | +[wifi] |
| 84 | +mode=infrastructure |
| 85 | +ssid=${SSID} |
| 86 | +hidden=${HIDDEN,,} |
| 87 | +[ipv4] |
| 88 | +method=auto |
| 89 | +[ipv6] |
| 90 | +addr-gen-mode=default |
| 91 | +method=auto |
| 92 | +[proxy] |
| 93 | +EOF |
| 94 | +} |
| 95 | + |
| 96 | +gen_keymgmt() { |
| 97 | + PSK_HASH="$(gen_psk)" |
| 98 | + if [[ -n "${PASSWORD}" ]]; then |
| 99 | + cat <<- EOF >>${CONNFILE} |
| 100 | +[wifi-security] |
| 101 | +key-mgmt=${KEYMGMT} |
| 102 | +psk=${PSK_HASH} |
| 103 | +EOF |
| 104 | +fi |
| 105 | +} |
| 106 | + |
| 107 | +gen_psk() { |
| 108 | + wpa_passphrase "${SSID}" "${PASSWORD}" | sed -n "/^.*[ \t]psk.*/s/[ \t]psk=//p" |
| 109 | +} |
| 110 | + |
| 111 | +set_regdomain() { |
| 112 | + log "Set registration domain to '${REGDOMAIN}' ..." |
| 113 | + # Shameless borrowed from |
| 114 | + # https://github.com/RPi-Distro/raspi-config/blob/d22643ade3851c42648f3676e577a622c34af49a/raspi-config#L830C3-L832C5 |
| 115 | + if ! ischroot; then |
| 116 | + iw reg set "${REGDOMAIN}" |
| 117 | + fi |
| 118 | + # set permanent |
| 119 | + case "${DISTRO}" in |
| 120 | + "rpi") |
| 121 | + # Shameless borrowed from |
| 122 | + # https://github.com/RPi-Distro/raspi-config/blob/d22643ade3851c42648f3676e577a622c34af49a/raspi-config#L822C3-L825C15 |
| 123 | + sed -i \ |
| 124 | + -e "s/\s*cfg80211.ieee80211_regdom=\S*//" \ |
| 125 | + -e "s/\(.*\)/\1 cfg80211.ieee80211_regdom=$REGDOMAIN/" \ |
| 126 | + "${CMDLINE}" |
| 127 | + ;; |
| 128 | + "armbian") |
| 129 | + if [[ -f "${CFG80211}" ]]; then |
| 130 | + rm -f "${CFG80211}" |
| 131 | + fi |
| 132 | + echo "options cfg80211 ieee80211_regdom=${REGDOMAIN}" > "${CFG80211}" |
| 133 | + ;; |
| 134 | + esac |
| 135 | +} |
| 136 | + |
| 137 | +# MAIN |
| 138 | +main() { |
| 139 | + # Determine base distribution |
| 140 | + get_distro |
| 141 | + |
| 142 | + # Check setup file is present |
| 143 | + if [[ -f "${SETUPFILE}" ]]; then |
| 144 | + log "Configuration file found in ${SETUPFILE}, continuing...\n" |
| 145 | + else |
| 146 | + log "Configuration file not found! Nothing to do, exiting...\n" |
| 147 | + exit 0 |
| 148 | + fi |
| 149 | + |
| 150 | + # Generate UUID |
| 151 | + gen_uuid |
| 152 | + |
| 153 | + # Remove preconfiguration if present |
| 154 | + if [[ -f "${CONNFILE}" ]]; then |
| 155 | + rm -f "${CONNFILE}" |
| 156 | + fi |
| 157 | + |
| 158 | + # Read config file |
| 159 | + get_config |
| 160 | + |
| 161 | + # Generate preconfigured.nmconnection file |
| 162 | + gen_nmconnection |
| 163 | + |
| 164 | + # Add key management and psk to preconfigured.nmconnection file |
| 165 | + gen_keymgmt |
| 166 | + |
| 167 | + # Set REGDOMAIN |
| 168 | + set_regdomain |
| 169 | + |
| 170 | + # NetworkManager only accepts 0600 permissions for configs |
| 171 | + chmod 0600 "${CONNFILE}" |
| 172 | + |
| 173 | + # Cleanup |
| 174 | + PSK_HASH="" |
| 175 | + PASSWORD="" |
| 176 | + SSID="" |
| 177 | + log "Configuration successful written ..." |
| 178 | + rm -f "${SETUPFILE}" |
| 179 | +} |
| 180 | + |
| 181 | +if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then |
| 182 | + main |
| 183 | +fi |
0 commit comments