fix: enhance NetworkManager hotspot configuration and cleanup process

This commit is contained in:
Timothy Hofland
2026-03-16 08:58:36 +01:00
parent 12ff47207e
commit 63a33f4deb
2 changed files with 34 additions and 7 deletions

8
.vscode/settings.json vendored Normal file
View File

@ -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
}
}
}

View File

@ -71,10 +71,18 @@ cleanup_legacy_access_point_stack() {
fi 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() { configure_networkmanager_access_point() {
mkdir -p /etc/systemd/system /etc/NetworkManager/conf.d mkdir -p /etc/systemd/system /etc/NetworkManager/conf.d
cleanup_legacy_access_point_stack cleanup_legacy_access_point_stack
cleanup_networkmanager_access_point_stack
cat <<EOF > /etc/NetworkManager/conf.d/90-mpvj-wifi-powersave.conf cat <<EOF > /etc/NetworkManager/conf.d/90-mpvj-wifi-powersave.conf
[connection] [connection]
@ -94,12 +102,14 @@ EOF
nmcli device set wlan0 autoconnect yes || true nmcli device set wlan0 autoconnect yes || true
nmcli device disconnect wlan0 2>/dev/null || 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.interface-name wlan0
nmcli connection modify "$AP_CONNECTION_NAME" connection.autoconnect yes connection.autoconnect-priority 100 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.powersave 2 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" ipv6.method disabled 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" wifi-sec.key-mgmt wpa-psk wifi-sec.psk "$WIFI_PASS" 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 <<EOF > /etc/systemd/system/mpvj-nm-ap.service cat <<EOF > /etc/systemd/system/mpvj-nm-ap.service
[Unit] [Unit]
@ -110,7 +120,7 @@ Wants=NetworkManager.service
[Service] [Service]
Type=oneshot Type=oneshot
RemainAfterExit=yes 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' ExecStop=/usr/bin/bash -lc 'nmcli connection down "$AP_CONNECTION_NAME" 2>/dev/null || true'
[Install] [Install]
@ -119,12 +129,21 @@ EOF
systemctl daemon-reload systemctl daemon-reload
systemctl enable mpvj-nm-ap.service avahi-daemon.service 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() { configure_legacy_access_point_stack() {
mkdir -p /etc/hostapd /etc/systemd/system /etc/NetworkManager/conf.d mkdir -p /etc/hostapd /etc/systemd/system /etc/NetworkManager/conf.d
cleanup_networkmanager_access_point_stack
cat <<EOF > /etc/hostapd/hostapd.conf cat <<EOF > /etc/hostapd/hostapd.conf
country_code=$WIFI_COUNTRY country_code=$WIFI_COUNTRY
interface=wlan0 interface=wlan0