Skip to content

Instantly share code, notes, and snippets.

@telenieko
Created March 5, 2018 16:57
Show Gist options
  • Save telenieko/d17544fc7e4b347beffa87252393384c to your computer and use it in GitHub Desktop.
Save telenieko/d17544fc7e4b347beffa87252393384c to your computer and use it in GitHub Desktop.
Sample files to enable wireless on Debian initramfs
#!/bin/sh
# this goes into /etc/initramfs-tools/scripts/init-premount/a_enable_wireless
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
. /scripts/functions
AUTH_LIMIT=30
INTERFACE="wlp5s0"
alias WPACLI="/sbin/wpa_cli -p/tmp/wpa_supplicant -i$INTERFACE "
log_begin_msg "Starting WLAN connection"
/sbin/wpa_supplicant -i$INTERFACE -c/etc/wpa_supplicant.conf -P/run/initram-wpa_supplicant.pid -B -f /tmp/wpa_supplicant.log
# Wait for AUTH_LIMIT seconds, then check the status
limit=${AUTH_LIMIT}
echo -n "Waiting for connection (max ${AUTH_LIMIT} seconds)"
while [ $limit -ge 0 -a `WPACLI status | grep wpa_state` != "wpa_state=COMPLETED" ]
do
sleep 1
echo -n "."
limit=`expr $limit - 1`
done
echo ""
if [ `WPACLI status | grep wpa_state` != "wpa_state=COMPLETED" ]; then
ONLINE=0
log_failure_msg "WLAN offline after timeout"
panic
else
ONLINE=1
log_success_msg "WLAN online"
fi
configure_networking
# !/bin/sh
# This goes into /etc/initramfs-tools/hooks/enable-wireless
set -e
PREREQ=""
prereqs()
{
echo "${PREREQ}"
}
case "${1}" in
prereqs)
prereqs
exit 0
;;
esac
. /usr/share/initramfs-tools/hook-functions
# CHANGE HERE for your correct modules.
manual_add_modules iwlwifi iwlmvm
copy_exec /sbin/wpa_supplicant
copy_exec /sbin/wpa_cli
copy_file config /etc/initramfs-tools/wpa_supplicant.conf /etc/wpa_supplicant.conf
# This is run on the shell
chmod +x /etc/initramfs-tools/scripts/local-bottom/kill_wireless
chmod +x /etc/initramfs-tools/scripts/init-premount/a_enable_wireless
chmod +x /etc/initramfs-tools/hooks/enable-wireless
update-initramfs -k all -u
update-grub
#!/bin/sh
# this goes into /etc/initramfs-tools/scripts/local-bottom/kill_wireless
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
echo "Killing wpa_supplicant so the system takes over later."
kill `cat /run/initram-wpa_supplicant.pid`
# sample /etc/initramfs-tools/wpa_supplicant.conf
# note that this is independent of the system /etc/wpa_supplicant.conf (if any)
# only add the network you need at boot time. **And keep the ctrl_interface** !!
ctrl_interface=/tmp/wpa_supplicant
network={
ssid="MyNetwork"
scan_ssid=1
psk="network passphrase"
key_mgmt=WPA-PSK
}
@morfikov
Copy link

morfikov commented Nov 13, 2021

Three things to note:

  1. You should delay the run of wpa_supplicant, or there's a chance you will get the error: Failed to connect to non-global ctrl_ifname error: No such file or directory . One second is enough, at least on my Raspberry Pi
  2. Don't forget to copy the right WiFi firmware to the image. The manual_add_modules command doesn't copy the full firmware. For instance in my case I had to copy the following files:
/lib/firmware/brcm/brcmfmac43455-sdio.bin
/lib/firmware/brcm/brcmfmac43455-sdio.clm_blob
/lib/firmware/brcm/brcmfmac43455-sdio.txt

The manual_add_modules command copied only brcm/brcmfmac43455-sdio.bin which resulted in missing wlan0 interface.

  1. Also copy the regulatory database (/lib/firmware/regulatory.db*).

@seffs
Copy link

seffs commented Nov 19, 2021

@morfikov How exactly did you delay it? I tried adding sleep 60 right before calling /sbin/wpa_supplicant here, but am unsuccessful. If you could share your full configuration, that would be awesome. I am trying to set this up in a RaspPi 400

@morfikov
Copy link

@seffs see here:

@seffs
Copy link

seffs commented Nov 20, 2021

Ok, so I found your blog entry and followed all of the steps. It works!

There are two extra things to note:

  1. I think it is better to first build the initramfs and figure out the exact files needed on boot for the wi-fi interface, then proceed with the rest of the steps (I also have a pi zero w and no mini HDMI converter, so for anyone in the same situation, this is strongly advised)

  2. The resized root partition won't work in another raspberry pi or resized SD card unless you get to boot there from a second USB stick, chroot into the installation with LUKS and rebuild the initramfs. "Cross-initramfs-building" is NOT an option. I learned this the very hard way, that is, time.

@morfikov
Copy link

morfikov commented Nov 20, 2021

  1. The resized root partition won't work in another raspberry pi or resized SD card unless you get to boot there from a second USB stick, chroot into the installation with LUKS and rebuild the initramfs. "Cross-initramfs-building" is NOT an option. I learned this the very hard way, that is, time.

It worked well for me, I just had to use QEMU.

@seffs
Copy link

seffs commented Nov 20, 2021

It worked well for me, I just had to use QEMU.

I tried to fit a 64gb installation into a 16 gb card (both rsync and resize/dd way). I made sure to use the same UUIDs for the partitions. SSH works on boot, but after entering the passphrase this follows:

Cannot initialize device-mapper. Is dm_mod kernel module loaded?

The same applied to the Pi400. Since I have another installation running on the Pi400, after chrooting and rebuilding it worked there, but rebuilding for the Pi Zero W failed.

@leksmut
Copy link

leksmut commented Jul 26, 2022

Thanks for this scripts.
To add autodetection of wlan interface name in a_enable_wireless.sh
change

INTERFACE="wlp5s0"

to

INTERFACE=`ip link | awk '{if(substr($2,1,1) =="w") {gsub(":","",$2); print($2); exit;}}'`

it finds first interface by mask "w*",
and then add check if INTERFACE is zero string and skip wpa_supplicant.

if ! [ -z "$INTERFACE" ]; then
  # wpa supllicant stuff
fi

configure_networking

@maxime1992
Copy link

Hello, I'm having a little issue. I've tried the above (thanks for the scripts!) but I'm now stuck and not able to boot my server.

image

I think the connection is failing for some reason except that I never end up again in a command line to unlock my disk manually from the laptop (without using wifi).

Any idea how I can solve this?

@telenieko
Copy link
Author

Hello, I'm having a little issue. I've tried the above (thanks for the scripts!) but I'm now stuck and not able to boot my server.

You are welcome.

I am not using Debian on this scenarios anymore so I cannot be of much help but:

a) I think there have been some considerable changes to initramfs/dracut that could make all this not very usefull
b) The dropbear packages (dropbear-initramfs), if memory serves well, had some scripts on line with what is proposed here which may be useful.
c) The mandos-client package also had some sample scripts that could help you

Oh, and check the comments posted here by some users giving updates! like @morfikov & @leksmut (Thank you all for those comments!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment