Hacking ARM image for WiFi config

Preconfigure ARM WiFi

The utility script preconfigures the sd-card to connect to WiFi at first boot.

After you have written the installer image to your sd-card, run this script to configure a connection to your wifi

Save the script on your system as e.g. wifi-hack.sh.

The script takes 3 mandatory arguments and you can opt to write a static address to the network configuration.

<blockdev> <ssid> <passphrase> [ <cidr> <gateway> <dns> ] .

With 3 arguments the wlan is setup using DHCP - with 6 arguments the wlan is configured for static addressing.

Sample command line - adapt the configuration to your use case and ensure no conflicts using the IP address

sudo bash wifi-hack.sh /dev/mmcblk0 my-ssid my-passphrase 192.168.1.10/24 192.168.1.1 192.168.1.1

The script

#!/usr/bin/env bash
#
# Script to preconfigure ARM installer for WiFi connection
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# @root - root.nix.dk
#

set -e
scriptname=$0
tmproot="/tmp/root"
usage(){
    echo "Run script as root using su or sudo"
	echo "Usage: sudo ${scriptname} /dev/mmcblkX your-ssid your-passphrase [ ip.x.y.z/24 gw.x.y.z ns.x.y.x ]"
	exit 1	
}

# run as root
if ! [[ $(whoami) == "root" ]] ; then
	usage
fi

# check if arg1 is block device
if ! [[ -b $"$1" ]]; then
    echo "'$1' is not a block devicce"
    exit 1
fi

# mount partition
echo Mount sd-card root partition
mkdir -p "${tmproot}"
mount "$1"p2 "${tmproot}"

# create wlan0 network
# if we have 3 arguments - use dhcp
if [[ $# -eq 3 ]] ; then
cat << EOF >> "${tmproot}/etc/systemd/network/wlan0.network"
[Match]
Name=wlan0
[Network]
DHCP=yes
EOF
# if we have 6 arguments - static ip
elif [[ $# -eq 6 ]]; then
cat << EOF >> "${tmproot}/etc/systemd/network/wlan0.network"
[Match]
Name=wlan0
[Network]
Address=$4
Gateway=$5
DNS=$6
EOF
# argument count must be either 3 or 6
else
	usage
fi

# create wpa_supplicant.conf
wpa_passphrase "$2" "$3" > "${tmproot}/etc/wpa_supplicant/wpa_supplicant-wlan0.conf"

# create symlink for wlan0 service
ln -sf "/usr/lib/systemd/system/wpa_supplicant0.service" \
      "${tmproot}/etc/systemd/system/multi-user.target.wants/wpa_supplicant@wlan0.service"

# unmount and clean up
echo Unmounting
umount ${tmproot}
echo Cleaning up
rmdir ${tmproot}
echo Done

Source: https://root.nix.dk/utility-scripts/arch-arm-image-wifi-hack

2 Likes

What is the main application of this script? Is it for headless ARM installs?
Our calamares installer live environment currently uses NetworkManager applet to connect to wifi during install and the users will have wifi working on first iboot.

It is for ARM - especially headless - and if you use the Zero W - you either connect monitor and keyboard because it has only WiFi or you preconfigure wlan.

Raspberry Pi people has created rpi-imager - which allows to configure the wireless when flashing Raspberry Pi OS.

I thought instead of using nmap to scan for an open port 22 or having to connect peripherals - I do what any programmer would do - If *nix user has repetetive tasks it must be scripted.

And by reading up on wpa_supplicant I figured out I could script connecting to my WiFi without ever having connected the device to network.

2 Likes

Just out of curiosity, is this a DHCP or static IP set up?

Pudge

It depends on the arguments fed to the script

1 Like