Refactor Wifi Config & Backup (#4385)

* #4384 set wifi by wifi file
This commit is contained in:
/rootzoll 2024-01-30 18:05:16 +01:00 committed by GitHub
parent 6fdf6f747e
commit 60c58ec6df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 132 additions and 54 deletions

View file

@ -16,6 +16,7 @@
- Update: Channel Tools (chantools) v0.12.0 [details](https://github.com/lightninglabs/chantools/releases/tag/v0.12.0)
- Update: LNbits 0.11.3 [details](https://github.com/lnbits/lnbits/releases/tag/0.11.3)
- Update: Circuitbreaker v0.5.1 [details](https://github.com/lightningequipment/circuitbreaker/blob/master/README.md)
- Refactor: Wifi config with file on sd card
- Deprecated: Homer Dashboard (remove from SSH menus, config script will stay with possible future removal)
- Deprecated: Bitcoinminds (remove from SSH menus, config script will stay with possible future removal)
- Remove: ItchySats (unmaintained project / in consent with dev)

5
FAQ.md
View file

@ -552,10 +552,9 @@ You don't need a LAN port on your laptop as long as you can connect over WLAN to
A LAN cable is recommended because it reduces a possible source of error on the network connection side. But how to setup WLAN when you don't have a LAN-Router/Switch available see here:
Using [Raspberry Pi Imager: ](https://www.raspberrypi.com/software/)
https://raspibolt.org/guide/raspberry-pi/operating-system.html#configure-boot-options
After flashing the SDcard image to the SDcard, reconnect the SDcard with the laptop or PC. It should appear as a drive on your computer. Open a text editor (a simple text editor like Notepad or VSCode, not Word) and write the name of the WiFi (the SSID) into the first line and the WiFi password into the second line. Then store the file as with the name just "wifi" (no extension) to the SDcard drive. Close the text editor and eject the SDcard to boot it up in your RaspiBlitz. Watch the LCD on boot up for possible error messages.
Manually
The old legacy way:
https://github.com/raspibolt/raspibolt/blob/a21788c0518618d17093e3f447f68a53e4efa6e7/raspibolt/raspibolt_20_pi.md#prepare-wifi
### Can I directly connect the RaspiBlitz to my laptop?

View file

@ -143,6 +143,57 @@ if [ "${flagExists}" == "1" ]; then
exit 0
fi
# wifi config by file on sd card
wifiFileExists=$(sudo ls /boot/firmware/wifi | grep -c 'wifi')
wpaFileExists=$(sudo ls /boot/firmware/wpa_supplicant.conf | grep -c 'wpa_supplicant.conf')
if [ "${wifiFileExists}" == "1" ] || [ "${wpaFileExists}" == "1" ]; then
# set info
echo "Setting Wifi by file on sd card ..." >> ${logFile}
/home/admin/_cache.sh set message "setting wifi"
# File: wifi
# get first line as string from wifi file (NAME OF WIFI)
# get second line as string from wifi file (PASSWORD OF WIFI)
if [ "${wifiFileExists}" == "1" ]; then
echo "Getting data from file: /boot/firmware/wifi" >> ${logFile}
ssid=$(sudo sed -n '1p' /boot/firmware/wifi | tr -d '[:space:]')
password=$(sudo sed -n '2p' /boot/firmware/wifi | tr -d '[:space:]')
fi
# File: wpa_supplicant.conf (legacy way to set wifi)
# see: https://github.com/raspibolt/raspibolt/blob/a21788c0518618d17093e3f447f68a53e4efa6e7/raspibolt/raspibolt_20_pi.md#prepare-wifi
if [ "${wpaFileExists}" == "1" ]; then
echo "Getting data from file: /boot/firmware/wpa_supplicant.conf" >> ${logFile}
ssid=$(grep ssid "/boot/firmware/wpa_supplicant.conf" | awk -F'=' '{print $2}' | tr -d '"')
password=$(grep psk "/boot/firmware/wpa_supplicant.conf" | awk -F'=' '{print $2}' | tr -d '"')
fi
# set wifi
err=""
echo "Setting Wifi SSID(${ssid}) Password(${password})" >> ${logFile}
source <(/home/admin/config.scripts/internet.wifi.sh on ${ssid} ${password})
if [ "${err}" != "" ]; then
echo "Setting Wifi failed - edit or remove file /boot/firmware/wifi" >> ${logFile}
echo "error(${err})" >> ${logFile}
echo "Will shutdown in 1min ..." >> ${logFile}
/home/admin/_cache.sh set state "errorWIFI"
/home/admin/_cache.sh set message "${err}"
sleep 60
sudo shutdown now
exit 1
fi
# remove file
echo "Setting Wifi worked - removing file" >> ${logFile}
sudo rm /boot/firmware/wifi 2>/dev/null
sudo rm /boot/firmware/wpa_supplicant.conf 2>/dev/null
else
echo "No Wifi config by file on sd card." >> ${logFile}
fi
# when the provision did not ran thru without error (ask user for fresh sd card)
provisionFlagExists=$(sudo ls /home/admin/provision.flag | grep -c 'provision.flag')
if [ "${provisionFlagExists}" == "1" ]; then
@ -286,7 +337,7 @@ fi
####################################
# check if there is a WIFI configuration to backup or restore
if [ -f "/var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf" ]; then
if [ -d "/var/cache/raspiblitz/hdd-inspect/wifi" ]; then
echo "WIFI RESTORE from /var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf" >> $logFile
/home/admin/config.scripts/internet.wifi.sh backup-restore >> $logFile
else
@ -1056,6 +1107,9 @@ if [ "${btc_default_sync_initialblockdownload}" == "1" ]; then
/home/admin/_cache.sh focus btc_default_sync_progress 0
fi
# backup wifi settings
/home/admin/config.scripts/internet.wifi.sh backup-restore
# notify about (re)start if activated
source <(/home/admin/_cache.sh get hostname)
/home/admin/config.scripts/blitz.notify.sh send "RaspiBlitz '${hostname}' (re)started" >> $logFile

View file

@ -293,7 +293,7 @@ if [ "$1" = "status" ]; then
cp -a /mnt/hdd${subVolumeDir}/raspiblitz.conf /var/cache/raspiblitz/hdd-inspect/raspiblitz.conf
# make copy of WIFI config to RAMDISK (if available)
cp -a /mnt/hdd${subVolumeDir}/app-data/wpa_supplicant.conf /var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf 2>/dev/null
cp -a /mnt/hdd${subVolumeDir}/app-data/wifi /var/cache/raspiblitz/hdd-inspect/ 2>/dev/null
# Convert old ssh backup data structure (if needed)
oldDataExists=$(sudo ls /mnt/hdd${subVolumeDir}/ssh/ssh_host_rsa_key 2>/dev/null | grep -c "ssh_host_rsa_key")

View file

@ -10,7 +10,9 @@ if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
exit 1
fi
wifiIsSet=$(sudo cat /etc/wpa_supplicant/wpa_supplicant.conf 2>/dev/null| grep -c "network=")
# gather status information
wifiIsSet=$(nmcli connection show | grep -c "wifi")
[ ${wifiIsSet} -gt 1 ] && wifiIsSet=1
wifiLocalIP=$(ip addr | grep 'state UP' -A2 | grep -E -v 'docker0|veth' | grep -E -i '([wlan][0-9]$)' | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
connected=0
if [ ${#wifiLocalIP} -gt 0 ]; then
@ -18,68 +20,66 @@ if [ ${#wifiLocalIP} -gt 0 ]; then
fi
if [ "$1" == "status" ]; then
echo "activated=${wifiIsSet}"
echo "connected=${connected}"
echo "localip='${wifiLocalIP}'"
exit 0
fi
elif [ "$1" == "on" ]; then
if [ "$1" == "on" ]; then
# get and check parameters
ssid="$2"
password="$3"
if [ ${#ssid} -eq 0 ]; then
echo "err='no ssid given'"
exit 1
fi
if [ ${#password} -eq 0 ]; then
echo "err='no password given'"
exit 1
fi
wifiConfig="country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid=\"${ssid}\"
scan_ssid=1
psk=\"${password}\"
key_mgmt=WPA-PSK
}"
echo "${wifiConfig}" > "/home/admin/wpa_supplicant.conf"
sudo chown root:root /home/admin/wpa_supplicant.conf
sudo mv /home/admin/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
sudo chmod 755 /etc/wpa_supplicant/wpa_supplicant.conf
# activate wifi
echo "# activating wifi ... give 10 secs to get ready"
sudo nmcli radio wifi on
sleep 10
# activate new wifi settings
sudo wpa_cli -i wlan0 reconfigure 1>/dev/null
echo "# OK - changes should be active now - maybe reboot needed"
echo "# trying to connect to SSID(${ssid}) ..."
sudo nmcli device wifi connect "${ssid}" password "${password}"
errorCode=$?
if [ ${errorCode} -gt 0 ]; then
echo "err='error code ${errorCode}'"
exit 1
fi
echo "# OK - changes should be active now"
exit 0
fi
elif [ "$1" == "off" ]; then
if [ "$1" == "off" ]; then
wifiConfig="country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1"
echo "${wifiConfig}" > "/home/admin/wpa_supplicant.conf"
sudo chown root:root /home/admin/wpa_supplicant.conf
sudo mv /home/admin/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
sudo chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf
sudo rm /boot/wpa_supplicant.conf 2>/dev/null
# remove all wifi connection coinfigs
nmcli connection show | grep wifi | cut -d " " -f 1 | while read -r line ; do
echo "# deactivating wifi connection: ${line}"
sudo nmcli connection delete "${line}"
done
# turn wifi off
sudo nmcli radio wifi off
# delete any backups on HDD/SSD (new and legacy)
sudo rm /mnt/hdd/app-data/wifi/* 2>/dev/null
sudo rm /mnt/hdd/app-data/wpa_supplicant.conf 2>/dev/null
# activate new wifi settings
sudo wpa_cli -i wlan0 reconfigure 1>/dev/null
echo "# OK - changes should be active now - maybe reboot needed"
echo "# OK - WIFI is now off"
exit 0
fi
# https://github.com/rootzoll/raspiblitz/issues/560
# when calling this it will backup wpa_supplicant.conf to HDD (if WIFI is active)
# or when WIFI is inactive but a wpa_supplicant.conf exists restore this
elif [ "$1" == "backup-restore" ]; then
# when calling this it will backup the wifi config to HDD/SSD (if WIFI is active)
# or when WIFI is inactive but a backup on HDD/SSD exists restore this
if [ "$1" == "backup-restore" ]; then
# print wifi state
echo "wifiIsSet=${wifiIsSet}"
@ -88,21 +88,29 @@ elif [ "$1" == "backup-restore" ]; then
hddBackupLocationAvailable=0
if [ -d /mnt/hdd/app-data ]; then
hddBackupLocationAvailable=1
sudo mkdir /mnt/hdd/app-data/wifi 2>/dev/null
fi
echo "hddBackupLocationAvailable=${hddBackupLocationAvailable}"
hddRestoreConfigAvailable=$(sudo ls /mnt/hdd/app-data/wpa_supplicant.conf 2>/dev/null | grep -c "wpa_supplicant.conf")
hddRestoreConfigAvailable=0
if [ ${hddBackupLocationAvailable} -eq 1 ] && [ "$(ls -A /mnt/hdd/app-data/wifi)" ]; then
# the directory /mnt/hdd/app-data/wifi contains files.
hddRestoreConfigAvailable=1
fi
echo "hddRestoreConfigAvailable=${hddRestoreConfigAvailable}"
# check if mem copy of wifi config is available (for restore only)
# this should be available if a backup on HDD exists and HDD is not mounted yet but was inspected by datadrive script
memRestoreConfigAvailable=$(sudo ls /var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf 2>/dev/null | grep -c "wpa_supplicant.conf")
memRestoreConfigAvailable=0
if [ -d /var/cache/raspiblitz/hdd-inspect/wifi ] && [ "$(ls -A /var/cache/raspiblitz/hdd-inspect/wifi)" ]; then
memRestoreConfigAvailable=1
fi
echo "memRestoreConfigAvailable=${memRestoreConfigAvailable}"
if [ ${wifiIsSet} -eq 1 ]; then
# BACKUP latest wifi settings to HDD if available
if [ ${hddBackupLocationAvailable} -eq 1 ]; then
sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /mnt/hdd/app-data/wpa_supplicant.conf
sudo cp -a /etc/NetworkManager/system-connections/* /mnt/hdd/app-data/wifi
echo "wifiRestore=0"
echo "wifiBackup=1"
else
@ -112,18 +120,24 @@ elif [ "$1" == "backup-restore" ]; then
exit 0
elif [ ${hddRestoreConfigAvailable} -eq 1 ]; then
# RESTORE backuped wifi settings from HDD to RaspiBlitz
sudo cp /mnt/hdd/app-data/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
echo "# restoring old wifi settings from HDD ... wait 4 secounds to connect"
sudo wpa_cli -i wlan0 reconfigure 1>/dev/null
echo "# restoring old wifi settings from HDD ... wait 15 secounds to connect"
sudo cp -a /mnt/hdd/app-data/wifi/* /etc/NetworkManager/system-connections/
sudo chmod 600 /etc/NetworkManager/system-connections/*
sudo nmcli radio wifi on
sleep 10
sudo systemctl restart NetworkManager
sleep 4
echo "wifiRestore=1"
echo "wifiBackup=0"
exit 0
elif [ ${memRestoreConfigAvailable} -eq 1 ]; then
# RESTORE backuped wifi settings from MEMCOPY to RaspiBlitz
sudo cp /var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
echo "# restoring old wifi settings from MEMCOPY ... wait 4 secounds to connect"
sudo wpa_cli -i wlan0 reconfigure 1>/dev/null
echo "# restoring old wifi settings from MEMCOPY ... wait 15 secounds to connect"
sudo cp -a /var/cache/raspiblitz/hdd-inspect/wifi/* /etc/NetworkManager/system-connections/
sudo chmod 600 /etc/NetworkManager/system-connections/*
sudo nmcli radio wifi on
sleep 10
sudo systemctl restart NetworkManager
sleep 4
echo "wifiRestore=1"
echo "wifiBackup=0"
@ -134,7 +148,8 @@ elif [ "$1" == "backup-restore" ]; then
echo "wifiBackup=0"
exit 0
fi
else
echo "err='parameter not known - run with -help'"
fi
# error case
echo "err='parameter not known - run with -help'"
exit 1

View file

@ -320,6 +320,15 @@ Detailed Error Message:
${contentString}
" 7 35
elif [ "${eventID}" == "errorWIFI" ]; then
# contentString --> detail error message
dialog --backtitle "${backtitle}" --cr-wrap --infobox "PROBLEM: Failed WIFI config
${contentString}
edit or remove file 'wifi'
Shutting down ...
" 7 35
elif [ "${eventID}" == "errorNetwork" ]; then
dialog --backtitle "${backtitle}" --cr-wrap --infobox "