[root tip] [Utility Script] Preconfigure ARM installer for WiFi

Source: https://root.nix.dk/utility-scripts/arch-arm-setup-wifi-script

Difficulty: ★★☆☆☆

Preconfigure ARM WiFi

The utility script preconfigures an ARM 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.

Assuming your sd-card is /dev/mmcblk0 the command to create a network using DHCP address assignment

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

Optionally you can add ip info which will create a static network configuration

sudo bash wifi-hack.sh /dev/mmcblk0 my-ssid my-passphrase 192.168.1.10/24 192.168.1.1 192.168.1.1
#!/usr/bin/env bash
#
# Script to preconfigure Manjaro 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/>.
#
# @linux-aarhus - 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 ssid 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