From 63a33f4deb007297298e9d6e259e35dfce2c4dc9 Mon Sep 17 00:00:00 2001 From: Timothy Hofland Date: Mon, 16 Mar 2026 08:58:36 +0100 Subject: [PATCH] fix: enhance NetworkManager hotspot configuration and cleanup process --- .vscode/settings.json | 8 ++++++++ scripts/setup.sh | 33 ++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a4ca5a8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "chat.tools.terminal.autoApprove": { + "/^bash -n scripts/setup\\.sh && bash -n scripts/switch-to-ap\\.sh && bash -n scripts/switch-to-client\\.sh$/": { + "approve": true, + "matchCommandLine": true + } + } +} \ No newline at end of file diff --git a/scripts/setup.sh b/scripts/setup.sh index 025461e..0bc75e4 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -71,10 +71,18 @@ cleanup_legacy_access_point_stack() { fi } +cleanup_networkmanager_access_point_stack() { + rm -f /etc/systemd/system/mpvj-nm-ap.service + systemctl disable mpvj-nm-ap.service 2>/dev/null || true + systemctl stop mpvj-nm-ap.service 2>/dev/null || true + nmcli connection down "$AP_CONNECTION_NAME" 2>/dev/null || true +} + configure_networkmanager_access_point() { mkdir -p /etc/systemd/system /etc/NetworkManager/conf.d cleanup_legacy_access_point_stack + cleanup_networkmanager_access_point_stack cat < /etc/NetworkManager/conf.d/90-mpvj-wifi-powersave.conf [connection] @@ -94,12 +102,14 @@ EOF nmcli device set wlan0 autoconnect yes || true nmcli device disconnect wlan0 2>/dev/null || true - nmcli connection add type wifi ifname wlan0 con-name "$AP_CONNECTION_NAME" autoconnect yes ssid "$WIFI_SSID" + nmcli --wait 30 device wifi hotspot ifname wlan0 con-name "$AP_CONNECTION_NAME" ssid "$WIFI_SSID" band bg password "$WIFI_PASS" nmcli connection modify "$AP_CONNECTION_NAME" connection.interface-name wlan0 - nmcli connection modify "$AP_CONNECTION_NAME" connection.autoconnect yes connection.autoconnect-priority 100 - nmcli connection modify "$AP_CONNECTION_NAME" 802-11-wireless.mode ap 802-11-wireless.band bg 802-11-wireless.powersave 2 - nmcli connection modify "$AP_CONNECTION_NAME" ipv4.method shared ipv4.addresses "$AP_ADDRESS" ipv6.method disabled - nmcli connection modify "$AP_CONNECTION_NAME" wifi-sec.key-mgmt wpa-psk wifi-sec.psk "$WIFI_PASS" + nmcli connection modify "$AP_CONNECTION_NAME" connection.autoconnect yes connection.autoconnect-priority 100 connection.wait-device-timeout 30000 + nmcli connection modify "$AP_CONNECTION_NAME" 802-11-wireless.mode ap 802-11-wireless.band bg 802-11-wireless.channel 7 802-11-wireless.powersave 2 802-11-wireless.cloned-mac-address permanent + nmcli connection modify "$AP_CONNECTION_NAME" ipv4.method shared ipv4.addresses "$AP_ADDRESS" ipv4.shared-dhcp-range "$AP_DHCP_RANGE_START,$AP_DHCP_RANGE_END" ipv6.method disabled + nmcli connection modify "$AP_CONNECTION_NAME" 802-11-wireless-security.key-mgmt wpa-psk 802-11-wireless-security.psk "$WIFI_PASS" 802-11-wireless-security.proto rsn 802-11-wireless-security.pairwise ccmp 802-11-wireless-security.group ccmp 802-11-wireless-security.pmf 1 + nmcli connection modify "$AP_CONNECTION_NAME" remove 802-1x || true + nmcli connection down "$AP_CONNECTION_NAME" 2>/dev/null || true cat < /etc/systemd/system/mpvj-nm-ap.service [Unit] @@ -110,7 +120,7 @@ Wants=NetworkManager.service [Service] Type=oneshot RemainAfterExit=yes -ExecStart=/usr/bin/bash -lc 'nmcli radio wifi on || true; nmcli device set wlan0 managed yes || true; nmcli device set wlan0 autoconnect yes || true; nmcli connection up "$AP_CONNECTION_NAME" ifname wlan0 || (nmcli connection reload && nmcli connection up "$AP_CONNECTION_NAME" ifname wlan0)' +ExecStart=/usr/bin/bash -lc 'nmcli radio wifi on || true; nmcli device set wlan0 managed yes || true; nmcli device set wlan0 autoconnect yes || true; nmcli connection reload; nmcli --wait 30 connection up "$AP_CONNECTION_NAME" ifname wlan0' ExecStop=/usr/bin/bash -lc 'nmcli connection down "$AP_CONNECTION_NAME" 2>/dev/null || true' [Install] @@ -119,12 +129,21 @@ EOF systemctl daemon-reload systemctl enable mpvj-nm-ap.service avahi-daemon.service - systemctl restart mpvj-nm-ap.service + + if systemctl restart mpvj-nm-ap.service; then + echo "NetworkManager hotspot configured successfully." + else + echo "NetworkManager hotspot activation failed. Falling back to hostapd/dnsmasq." + cleanup_networkmanager_access_point_stack + configure_legacy_access_point_stack + fi } configure_legacy_access_point_stack() { mkdir -p /etc/hostapd /etc/systemd/system /etc/NetworkManager/conf.d + cleanup_networkmanager_access_point_stack + cat < /etc/hostapd/hostapd.conf country_code=$WIFI_COUNTRY interface=wlan0