From c5b7ab895dde5b0fe43c1c8e55c8b86a3d5ced92 Mon Sep 17 00:00:00 2001 From: Guy Sheffer Date: Fri, 9 Aug 2024 22:15:54 +0300 Subject: [PATCH] Add support for network manager and wpa-supplicant in network module #219 https://github.com/raspberrypi/bookworm-feedback/issues/72 --- src/modules/network/config | 8 ++++- .../network-manager/boot/wifi.nmconnection | 24 +++++++++++++ .../copy-network-manager-config@.service | 21 ++++++++++++ .../custompios/copy-network-manager-config | 8 +++++ .../boot/custompios-wpa-supplicant.txt | 0 src/modules/network/start_chroot_script | 34 ++++++++++++------- 6 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 src/modules/network/filesystem/network-manager/boot/wifi.nmconnection create mode 100644 src/modules/network/filesystem/network-manager/root/etc/systemd/system/copy-network-manager-config@.service create mode 100755 src/modules/network/filesystem/network-manager/root/opt/custompios/copy-network-manager-config rename src/modules/network/filesystem/{ => wpa-supplicant}/boot/custompios-wpa-supplicant.txt (100%) diff --git a/src/modules/network/config b/src/modules/network/config index 1504d394..9b8923fd 100755 --- a/src/modules/network/config +++ b/src/modules/network/config @@ -10,4 +10,10 @@ # on reboots # udev - creates a udev rules that should affect all wifi devices. -[ -n "$NETWORK_PWRSAVE_TYPE" ] || NETWORK_PWRSAVE_TYPE=udev \ No newline at end of file +[ -n "$NETWORK_PWRSAVE_TYPE" ] || NETWORK_PWRSAVE_TYPE=udev + +# Enable WPA-Supplicant boot folder support (pre rpios bookworm) +[ -n "$NETWORK_WPA_SUPPLICANT" ] || NETWORK_WPA_SUPPLICANT=no + +# Enable Network Manager boot folder support (bookworm) +[ -n "$NETWORK_NETWORK_MANAGER" ] || NETWORK_NETWORK_MANAGER=yes \ No newline at end of file diff --git a/src/modules/network/filesystem/network-manager/boot/wifi.nmconnection b/src/modules/network/filesystem/network-manager/boot/wifi.nmconnection new file mode 100644 index 00000000..afca98b3 --- /dev/null +++ b/src/modules/network/filesystem/network-manager/boot/wifi.nmconnection @@ -0,0 +1,24 @@ +# Uncomment everything below this line and set your ssid and password +# [connection] +# id=wifi +# uuid=593819b8-135a-4a3e-9611-c36cdeadbeef +# type=wifi +# interface-name=wlan0 + +# [wifi] +# mode=infrastructure +# ssid=set your wifi ssid here + +# [wifi-security] +# auth-alg=open +# key-mgmt=wpa-psk +# psk=set your password here + +# [ipv4] +# method=auto + +# [ipv6] +# addr-gen-mode=default +# method=auto + +# [proxy] diff --git a/src/modules/network/filesystem/network-manager/root/etc/systemd/system/copy-network-manager-config@.service b/src/modules/network/filesystem/network-manager/root/etc/systemd/system/copy-network-manager-config@.service new file mode 100644 index 00000000..d5c52923 --- /dev/null +++ b/src/modules/network/filesystem/network-manager/root/etc/systemd/system/copy-network-manager-config@.service @@ -0,0 +1,21 @@ +#### Enable network manager configuration from boot +#### +#### Written by Guy Sheffer +#### Copyright 2024 +#### https://github.com/guysoft/CustomPiOS +#### +#### This File is distributed under GPLv3 + +[Unit] +Description=persistent setup on %I +Wants=network-pre.target +Before=network-pre.target +BindsTo=sys-subsystem-net-devices-%i.device +After=sys-subsystem-net-devices-%i.device + +[Service] +ExecStart=/opt/custompios/copy-network-manager-config %I +Type=oneshot + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/src/modules/network/filesystem/network-manager/root/opt/custompios/copy-network-manager-config b/src/modules/network/filesystem/network-manager/root/opt/custompios/copy-network-manager-config new file mode 100755 index 00000000..3b2ec777 --- /dev/null +++ b/src/modules/network/filesystem/network-manager/root/opt/custompios/copy-network-manager-config @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +( + echo "# DO NOT EDIT THIS FILE" + echo "# Edit /boot/firmware/wifi.nmconnection and it will be copied here" + cat /boot/firmware/wifi.nmconnection +) > /etc/NetworkManager/system-connections/wifi.nmconnection +chmod 600 /etc/NetworkManager/system-connections/wifi.nmconnection +echo Done \ No newline at end of file diff --git a/src/modules/network/filesystem/boot/custompios-wpa-supplicant.txt b/src/modules/network/filesystem/wpa-supplicant/boot/custompios-wpa-supplicant.txt similarity index 100% rename from src/modules/network/filesystem/boot/custompios-wpa-supplicant.txt rename to src/modules/network/filesystem/wpa-supplicant/boot/custompios-wpa-supplicant.txt diff --git a/src/modules/network/start_chroot_script b/src/modules/network/start_chroot_script index c2be6a0c..2b086a08 100755 --- a/src/modules/network/start_chroot_script +++ b/src/modules/network/start_chroot_script @@ -12,29 +12,39 @@ export LC_ALL=C source /common.sh install_cleanup_trap -unpack /filesystem/boot /"${BASE_BOOT_MOUNT_PATH}" +if [ "${NETWORK_WPA_SUPPLICANT}" == "yes" ]; then + unpack /filesystem/wpa-supplicant/boot /"${BASE_BOOT_MOUNT_PATH}" + DIST_NETWORK_FILE=/"${BASE_BOOT_MOUNT_PATH}"/${DIST_NAME,,}-wpa-supplicant.txt -DIST_NETWORK_FILE=/"${BASE_BOOT_MOUNT_PATH}"/${DIST_NAME,,}-wpa-supplicant.txt + # allow configuring multiple wifi networks via /boot/DIST_NAME-wpa-supplicant.txt + mv /"${BASE_BOOT_MOUNT_PATH}"/custompios-wpa-supplicant.txt ${DIST_NETWORK_FILE} -# allow configuring multiple wifi networks via /boot/DIST_NAME-wpa-supplicant.txt -mv /"${BASE_BOOT_MOUNT_PATH}"/custompios-wpa-supplicant.txt ${DIST_NETWORK_FILE} + if [ "${BASE_DISTRO}" == "ubuntu" ] || [ "${BASE_DISTRO}" == "armbian" ]; then + echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev" > /etc/wpa_supplicant/wpa_supplicant.conf + echo "update_config=1" >> /etc/wpa_supplicant/wpa_supplicant.conf + fi -if [ "${BASE_DISTRO}" == "ubuntu" ] || [ "${BASE_DISTRO}" == "armbian" ]; then - echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev" > /etc/wpa_supplicant/wpa_supplicant.conf - echo "update_config=1" >> /etc/wpa_supplicant/wpa_supplicant.conf + cat /etc/wpa_supplicant/wpa_supplicant.conf >> ${DIST_NETWORK_FILE} # append distributed conf to our own + rm -f /etc/wpa_supplicant/wpa_supplicant.conf # remove distributed conf + + # create symlink + ln -s "${DIST_NETWORK_FILE}" /etc/wpa_supplicant/wpa_supplicant.conf fi -cat /etc/wpa_supplicant/wpa_supplicant.conf >> ${DIST_NETWORK_FILE} # append distributed conf to our own -rm -f /etc/wpa_supplicant/wpa_supplicant.conf # remove distributed conf +if [ "${NETWORK_NETWORK_MANAGER}" == "yes" ]; then + unpack filesystem/network-manager/root / root + unpack filesystem/network-manager/boot /"${BASE_BOOT_MOUNT_PATH}" + + # mv /"${BASE_BOOT_MOUNT_PATH}"/wifi.nmconnection ${DIST_NETWORK_FILE} + + systemctl_if_exists enable copy-network-manager-config@wlan0.service +fi if [ "${BASE_DISTRO}" == "raspbian" ]; then # Workaround rfkill not unblocking on boot rm /var/lib/systemd/rfkill/* fi -# create symlink -ln -s "${DIST_NETWORK_FILE}" /etc/wpa_supplicant/wpa_supplicant.conf - # copy /etc/wpa_supplicant/ifupdown.sh to /etc/ifplugd/action.d/ifupdown - for wlan auto reconnect [ -f /etc/ifplugd/action.d/ifupdown ] && mv /etc/ifplugd/action.d/ifupdown /etc/ifplugd/action.d/ifupdown.original [ -f /etc/wpa_supplicant/ifupdown.sh ] && ln -s /etc/wpa_supplicant/ifupdown.sh /etc/ifplugd/action.d/ifupdown