Repository: ViRb3/pi-encrypted-boot-ssh
Branch: master
Commit: 3bfbce034361
Files: 4
Total size: 24.0 KB
Directory structure:
gitextract_oqmpe99l/
├── README.md
├── Wireless-Builtin.md
├── Wireless-USB.md
└── Wireless-USB2.md
================================================
FILE CONTENTS
================================================
================================================
FILE: README.md
================================================
# Raspberry Pi Encrypted Boot with SSH
> ⚠️ This guide is only tested on the [Raspberry Pi 5](https://www.raspberrypi.com/products/raspberry-pi-5/) with [Raspberry Pi OS Lite 64-bit 2023-12-11](https://www.raspberrypi.com/software/operating-systems/). \
> Other platforms and distributions may work, but there may be unexpected issues or side effects.
## Introduction
This guide will show you how to encrypt your Raspberry Pi's root partition and set up an [initramfs](https://en.wikipedia.org/wiki/Initial_ramdisk) that will prompt for the password, decrypt the partition and gracefully resume boot. You will also learn how to enable SSH during this pre-boot stage, allowing you to unlock the partition remotely. There are also optional steps for WiFi setup.
This guide operates directly on an image file and therefore does not require an SD card for the setup. The resulting image can be flashed to an SD card as usual.
## Table of Content
- [Raspberry Pi Encrypted Boot with SSH](#raspberry-pi-encrypted-boot-with-ssh)
- [Introduction](#introduction)
- [Table of Content](#table-of-content)
- [Requirements](#requirements)
- [On the host](#on-the-host)
- [In the chroot](#in-the-chroot)
- [Prepare](#prepare)
- [Device configuration](#device-configuration)
- [Cryptsetup](#cryptsetup)
- [SSH](#ssh)
- [WiFi support](#wifi-support)
- [Raspberry Pi OS](#raspberry-pi-os)
- [Ubuntu (obsolete)](#ubuntu-obsolete)
- [Build initramfs](#build-initramfs)
- [Finish](#finish)
- [On the host](#on-the-host-1)
- [On the Raspberry Pi](#on-the-raspberry-pi)
- [Avoiding SSH key collisions](#avoiding-ssh-key-collisions)
- [Resources](#resources)
## Requirements
- A Raspberry Pi Linux image (e.g. [Raspberry Pi OS Lite 64-bit 2023-12-11](https://www.raspberrypi.com/software/operating-systems/))
- A computer (host) running Linux (e.g. [Kali Linux 2023.2](https://www.kali.org/get-kali/#kali-platforms))
> :warning: **NOTE:** Your host's Linux should be as similar as possible to the Raspberry Pi's Linux. If you are preparing Debian 12/kernel 6.1 for the Raspberry Pi, use similar versions on the host, otherwise you may encounter issues inside the chroot.
## On the host
Install dependencies:
- You can skip `binfmt-support` and `qemu-user-static` if your host Linux's architecture matches that of the Raspberry Pi's Linux image.
- If your host Linux doesn't use systemd, such as WSL on Windows, you need to manually run `update-binfmts --enable` after installing `binfmt-support`.
```sh
apt update
apt install -y parted kpartx cryptsetup-bin rsync binfmt-support qemu-user-static
```
Create two copies of the Raspberry Pi's Linux image — one to read from (base), and one to write to (target):
- pi-base.img
- pi-target.img
Increase the size of the target image or you may run into issues:
```bash
dd if=/dev/zero bs=1G count=1 >> pi-target.img
parted pi-target.img resizepart 2 100%
```
Map both images as devices, ensuring the base is readonly:
```sh
kpartx -ar "$PWD/pi-base.img"
kpartx -a "$PWD/pi-target.img"
```
If your system automatically mounted any partitions, unmount them:
```sh
umount /media/**/*
```
Run [lsblk](https://linux.die.net/man/8/lsblk) and verify the process was successful — you should see two loopback devices, each with two partitions:
```sh
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT # COMMENT
loop0 7:0 0 3.3G 0 loop # pi-base.img
├─loop0p1 253:0 0 256M 0 part # ├─ boot
└─loop0p2 253:1 0 3G 0 part # └─ root
loop1 7:1 0 3.3G 1 loop # pi-target.img
├─loop1p1 253:2 0 256M 1 part # ├─ boot
└─loop1p2 253:3 0 3G 1 part # └─ root
```
Mount the base image's root partition:
```sh
mkdir -p /mnt/original/
mount /dev/mapper/loop0p2 /mnt/original/
```
Replace the target image's root partition with a new, encrypted partition:
> :warning: **NOTE:**
>
> The default encryption algorithm is `aes-xts-plain64`, which is fast only on the Raspberry Pi 5 due to its hardware AES acceleration. If you have an older generation, then use [aes-adiantum](https://github.com/google/adiantum) instead via `-c xchacha20,aes-adiantum-plain64`. It is much faster than AES in software.
>
> By default cryptsetup will [benchmark](https://man7.org/linux/man-pages/man8/cryptsetup-luksformat.8.html) your host and use a memory-hard PBKDF algorithm that can require up to 4GB of RAM. If these settings exceed your Raspberry Pi's available RAM, it will make it impossible to unlock the partition. To work around this, set the [--pbkdf-memory](https://man7.org/linux/man-pages/man8/cryptsetup-luksformat.8.html) and [--pbkdf-parallel](https://man7.org/linux/man-pages/man8/cryptsetup-luksformat.8.html) arguments so when you multiply them, the result is less than your Pi's total RAM. For example: `--pbkdf-memory 512000 --pbkdf-parallel=1`
```sh
cryptsetup luksFormat /dev/mapper/loop1p2
```
Open (decrypt) the new partition:
```
cryptsetup open /dev/mapper/loop1p2 crypted
```
Then format and mount it:
```
mkfs.ext4 /dev/mapper/crypted
mkdir -p /mnt/chroot/
mount /dev/mapper/crypted /mnt/chroot/
```
Copy the base image's root partition files to the target image's new, encrypted root partition. You can use [dd](https://linux.die.net/man/1/dd), but [rsync](https://linux.die.net/man/1/rsync) is faster:
```sh
rsync --archive --hard-links --acls --xattrs --one-file-system --numeric-ids --info="progress2" /mnt/original/* /mnt/chroot/
```
Set up a [chroot](https://linux.die.net/man/1/chroot) by mounting the target image's boot partition and required virtual filesystems from the host:
```sh
mkdir -p /mnt/chroot/boot/
mount /dev/mapper/loop1p1 /mnt/chroot/boot/
mount -t proc none /mnt/chroot/proc/
mount -t sysfs none /mnt/chroot/sys/
mount -o bind /dev /mnt/chroot/dev/
mount -o bind /dev/pts /mnt/chroot/dev/pts/
```
Enter the chroot:
```sh
LANG=C chroot /mnt/chroot/ /bin/bash
```
## In the chroot
### Prepare
Install the dependencies:
```sh
apt update
apt install -y busybox cryptsetup dropbear-initramfs
```
### Device configuration
Edit [/etc/fstab](https://linux.die.net/man/5/fstab) and replace the root entry with your decrypted (virtual) partition's device name:
```sh
# Original:
PARTUUID=e8af6eb2-02 / ext4 defaults,noatime 0 1
# Replace with:
/dev/mapper/crypted / ext4 defaults,noatime 0 1
```
Run [blkid](https://linux.die.net/man/8/blkid) and note the details of your encrypted partition:
```sh
blkid | grep crypto_LUKS
/dev/mapper/loop1p2: UUID="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" TYPE="crypto_LUKS" PARTUUID="cccccccc-cc"
```
Edit [/etc/crypttab](https://linux.die.net/man/5/crypttab) and add an entry with your encrypted (raw) partition's UUID:
```sh
crypted UUID=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa none luks,initramfs
```
Edit `/boot/cmdline.txt` and update the root entry:
```sh
# Original:
root=PARTUUID=21e60f8c-02
# Replace with:
root=/dev/mapper/crypted cryptdevice=UUID=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:crypted
```
### Cryptsetup
Edit the cryptsetup initramfs hook to ensure cryptsetup ends up in the initramfs:
```sh
echo "CRYPTSETUP=y" >> /etc/cryptsetup-initramfs/conf-hook
```
The [initramfs-tools](https://manpages.ubuntu.com/manpages/xenial/man8/initramfs-tools.8.html) `cryptroot` hook will resolve any UUIDs to device names during initramfs generation. This is a problem because the device names will likely differ between the host and the Raspberry Pi, resulting in failure to boot. To work around this, apply the following patch:
```patch
patch --no-backup-if-mismatch /usr/share/initramfs-tools/hooks/cryptroot << 'EOF'
--- cryptroot
+++ cryptroot
@@ -33,7 +33,7 @@
printf '%s\0' "$target" >>"$DESTDIR/cryptroot/targets"
crypttab_find_entry "$target" || return 1
crypttab_parse_options --missing-path=warn || return 1
- crypttab_print_entry
+ printf '%s %s %s %s\n' "$_CRYPTTAB_NAME" "$_CRYPTTAB_SOURCE" "$_CRYPTTAB_KEY" "$_CRYPTTAB_OPTIONS" >&3
fi
}
EOF
```
The default timeout when waiting for decryption (10 seconds) may be too short and result in a timeout error. To work around this, bump the value:
```sh
sed -i 's/^TIMEOUT=.*/TIMEOUT=100/g' /usr/share/cryptsetup/initramfs/bin/cryptroot-unlock
```
### SSH
Write your SSH public key inside dropbear's and your decrypted OS's `authorized_keys` and fix permissions:
```sh
mkdir -p /root/.ssh && chmod 0700 /root/.ssh
echo "/REDACTED/" | tee /etc/dropbear/initramfs/authorized_keys /root/.ssh/authorized_keys
chmod 0600 /etc/dropbear/initramfs/authorized_keys /root/.ssh/authorized_keys
```
### WiFi support
This step is optional. If you want the Raspberry Pi to be decryptable over WiFi, check out the guides below. Note that the differences between distros is very small, so you can easily adapt any particular guide.
#### Raspberry Pi OS
- [Wireless-USB2.md](Wireless-USB2.md)
#### Ubuntu (obsolete)
- [Wireless-Builtin.md](Wireless-Builtin.md)
- [Wireless-USB.md](Wireless-USB.md)
### Build initramfs
Note your kernel version. If there are multiple, choose the one you want to run. The `2712` suffix is for Raspberry Pi 5, while `v8` is for all previous generations:
```sh
ls /lib/modules/
```
> :warning: **NOTE:**
>
> Starting with the Raspberry Pi 5, the default kernel page size is 16K instead of 4K. This breaks some software, but more importantly, is experimental in btrfs with 4K sector size disks:
>
> > BTRFS warning (device dm-1): read-write for sector size 4096 with page size 16384 is experimental
>
> For this reason, you may want to switch back to the old kernel by adding the following to your `/boot/config.txt`:
>
> ```sh
> echo "kernel=kernel8.img" >> /boot/config.txt
> echo "initramfs initramfs8 followkernel" >> /boot/config.txt
> ```
Build the new initramdisk using the kernel version from above. The `initramfs-tools` package parses the compression method from a file that doesn't exist, so we need to create it:
```sh
kversion="6.1.0-rpi7-rpi-v8" # "6.1.0-rpi7-rpi-2712" for 16k pages
echo "CONFIG_RD_ZSTD=y" > /boot/config-$kversion
mkinitramfs -o /boot/initramfs8 $kversion # "initramfs_2712" for 16K pages
rm /boot/config-$kversion
```
Customize first run/init in `/boot/cmdline.txt` as it will break the encrypted setup and prevent booting:
```sh
# Original:
[...] quiet init=/usr/lib/raspberrypi-sys-mods/firstboot
# Replace with:
[...] systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target
```
Create `/boot/firstrun.sh` and customize it for your needs. Most of these scripts allow additional configuration, so feel free to check their source:
```sh
#!/bin/bash
set +e
/usr/lib/raspberrypi-sys-mods/imager_custom enable_ssh
/usr/lib/raspberrypi-sys-mods/regenerate_ssh_host_keys
/usr/lib/userconf-pi/userconf 'pi'
/usr/lib/raspberrypi-sys-mods/imager_custom set_wlan 'MyWiFi' 'pass1234' 'GB'
rm -f /boot/firstrun.sh
sed -i 's| systemd.run.*||g' /boot/cmdline.txt
exit 0
```
Make sure the newly created file is executable:
```sh
chmod +x /boot/firstrun.sh
```
### Finish
Sync and exit the chroot:
```sh
sync
history -c && exit
```
## On the host
Unmount everything and clean up any remaining artifacts:
```sh
umount /mnt/chroot/boot
umount /mnt/chroot/sys
umount /mnt/chroot/proc
umount /mnt/chroot/dev/pts
umount /mnt/chroot/dev
umount /mnt/chroot
cryptsetup close crypted
umount /mnt/original
rm -d /mnt/chroot
rm -d /mnt/original
kpartx -d "$PWD/pi-base.img"
kpartx -d "$PWD/pi-target.img"
```
You are now ready to flash `pi-target.img` to an SD card.
## On the Raspberry Pi
Boot the Raspberry Pi with the new SD card. It will obtain an IP address from the DHCP server and start listening for SSH connections. To decrypt the root partition and continue boot, from any shell, simply run `cryptroot-unlock`.
The first time only:
1. Once decrypted, the system will power off. The reason for this is currently unknown. Restart the device and decrypt again.
2. The decrypted system will run the init script from before and reboot back into the initramfs. Proceed to decrypt one last time.
Once booted into the decrypted system, you will notice that the root partition is still sized at ~3GB, no matter how much space you have on the SD card. To fix this, resize the partition:
```sh
parted /dev/mmcblk0 resizepart 2 100%
cryptsetup resize crypted
resize2fs /dev/mapper/crypted
```
Finally, reboot the system one last time for good measure:
```sh
reboot
```
## Avoiding SSH key collisions
To avoid host key collisions you can configure a separate trusted hosts store in the `~/.ssh/config` of your client:
```ssh
Host box
Hostname 192.168.0.30
User root
Host box-initramfs
Hostname 192.168.0.30
User root
UserKnownHostsFile ~/.ssh/known_hosts.initramfs
```
## Resources
- https://www.kali.org/docs/arm/raspberry-pi-with-luks-disk-encryption/
- https://wiki.archlinux.org/index.php/Dm-crypt/Specialties
- https://wiki.gentoo.org/wiki/Custom_Initramfs
- https://www.raspberrypi.org/forums/viewtopic.php?t=252980
- https://thej6s.com/articles/2019-03-05__decrypting-boot-drives-remotely/
- https://www.pbworks.net/ubuntu-guide-dropbear-ssh-server-to-unlock-luks-encrypted-pc/
- https://raspberrypi.stackexchange.com/questions/92557/how-can-i-use-an-init-ramdisk-initramfs-on-boot-up-raspberry-pi/
- https://www.raspberrypi.com/documentation/computers/configuration.html#setting-up-a-headless-raspberry-pi
================================================
FILE: Wireless-Builtin.md
================================================
# WiFi Support
This guide will show you how to set up the Raspberry Pi's built-in WiFi module for use during initramfs. The steps here are intended for Ubuntu, slight changes will be necessary for Raspberry Pi OS.
## Set up initramfs
Create the following files and customize them if necessary:
- `/etc/initramfs-tools/scripts/init-premount/a_enable_wireless`
```bash
#!/bin/sh
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
echo "Waiting for wlan device to come up..."
while [ ! -d "/sys/class/net/wlan0" ]; do
sleep 1
done
echo "Initializing wpa-supplicant..."
/sbin/wpa_supplicant -i wlan0 -c /etc/wpa_supplicant.conf -P /run/initram-wpa_supplicant.pid -B
```
- `/etc/initramfs-tools/hooks/enable-wireless`
```bash
#!/bin/sh
set -e
PREREQ=""
prereqs()
{
echo "${PREREQ}"
}
case "${1}" in
prereqs)
prereqs
exit 0
;;
esac
. /usr/share/initramfs-tools/hook-functions
copy_exec /sbin/wpa_supplicant
# copy WiFi driver
copy_modules_dir kernel/drivers/net/wireless/broadcom/brcm80211/brcmfmac
# copy additional firmware files, ignoring error if they are already copied
for f in /lib/firmware/brcm/brcmfmac*; do
copy_file firmware "$f" || true
done
copy_file config /etc/initramfs-tools/wpa_supplicant.conf /etc/wpa_supplicant.conf
```
- `/etc/initramfs-tools/scripts/init-bottom/kill_wireless`
```bash
#!/bin/sh
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
# allow the decrypted OS to handle WiFi on its own
echo "Stopping wlan device..."
kill $(cat /run/initram-wpa_supplicant.pid)
ip link set wlan0 down
# created by initramfs
# for some reason it lists wlan0 as ethernet, which breaks netplan - remove it
rm -f /run/netplan/wlan0.yaml
```
- `/etc/initramfs-tools/wpa_supplicant.conf`
```bash
ctrl_interface=/tmp/wpa_supplicant
country=GB
network={
ssid="Foo"
scan_ssid=1
psk="Bar"
key_mgmt=WPA-PSK
}
```
Chmod all scripts you just created:
```bash
chmod +x /etc/initramfs-tools/scripts/init-premount/a_enable_wireless
chmod +x /etc/initramfs-tools/hooks/enable-wireless
chmod +x /etc/initramfs-tools/scripts/init-bottom/kill_wireless
```
Set up WiFi for the decrypted OS. On Ubuntu, you do this by creating e.g. `/etc/netplan/10-user.yaml`:
```yaml
network:
version: 2
ethernets:
eth0:
dhcp4: true
optional: true
wifis:
wlan0:
optional: true
access-points:
"Foo":
password: "Bar"
dhcp4: true
```
You're done! Follow the rest of the guide to finish building your initramfs.
## Resources
- https://gist.github.com/telenieko/d17544fc7e4b347beffa87252393384c
- https://morfikov.github.io/post/wsparcie-dla-wifi-w-initramfs-initrd-by-odszyfrowac-luks-przez-ssh-bezprzewodowo/
- https://morfikov.github.io/post/odszyfrowanie-luks-przez-ssh-z-poziomu-initramfs-initrd-na-raspberry-pi/
- https://github.com/endlessm/linux-firmware/tree/master/brcm
- https://forums.gentoo.org/viewtopic-t-1040452-start-0.html
- https://github.com/lamby/initramfs-tools/blob/pass-reproducible-option/hook-functions
- https://github.com/unixabg/cryptmypi/blob/master/hooks/0000-experimental-initramfs-wifi.hook
- https://askubuntu.com/questions/1250133/interface-ip-conflict-after-dropbear-initramfs-boot
================================================
FILE: Wireless-USB.md
================================================
# WiFi USB Support
This guide will show you how to set up a WiFi USB dongle for use during initramfs. The steps here are intended for Ubuntu, slight changes will be necessary for Raspberry Pi OS.
## Installing driver
The steps below are written for the [TP-LINK T4U](https://www.tp-link.com/uk/home-networking/adapter/archer-t4u/) USB dongle, which uses the Realtek RTL88x2B driver. As of Linux 5.15, this driver is not included in the kernel, so it needs to be built and installed manually. If your driver comes with your kernel, you can skip this section.
First, install [dkms](https://help.ubuntu.com/community/DKMS):
```bash
apt install dkms
```
Build your initramfs once to generate dependencies needed by dkms. Replace the kernel version if necessary (check `/usr/lib/modules/`):
```bash
mkinitramfs -o /boot/initrd.img "5.15.0-1005-raspi"
```
Prepare the driver:
```bash
git clone https://github.com/ViRb3/88x2bu-20210702.git
cd 88x2bu-20210702
```
By default, this driver's Makefile will use all of your CPU cores. This has the side effect of also using additional RAM, and if you're on a 1GB RAM device, it may run out of memory. To work around, disable the parallel build:
```bash
sed -i 's/-j$sproc/-j1/g' dkms-make.sh
```
Build and install the driver:
```bash
KVER="5.15.0-1005-raspi" ./install-driver.sh
```
To prevent issues, disable all power-saving features:
```bash
./edit-options.sh
options 88x2bu rtw_power_mgnt=0 rtw_ips_mode=0 rtw_enusbss=0
```
If you are connecting the card to a USB3 port, also add `rtw_switch_usb_mode=1` to force it into USB3 mode. Note that the Raspberry Pi 4B specifically has an issue where the driver silently stops working in USB3 mode when acting as AP or when downloading large volumes of data (~120GB). To work around, leave as default or add `rtw_switch_usb_mode=2` to force USB2 mode.
## Set up initramfs
On Ubuntu 22.04, external (USB) WiFi dongles have a deterministic interface naming scheme that uses the device's MAC address, like: `wlx112233445566`. Replace the interface name with yours in the examples below.
Create the following files and customize them if necessary:
- `/etc/initramfs-tools/scripts/init-premount/a_enable_wireless`
```bash
#!/bin/sh
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
echo "Waiting for wlan device to come up..."
while [ ! -d "/sys/class/net/wlx112233445566" ]; do
sleep 1
done
echo "Initializing wpa-supplicant..."
/sbin/wpa_supplicant -i wlx112233445566 -c /etc/wpa_supplicant.conf -P /run/initram-wpa_supplicant.pid -B
```
- `/etc/initramfs-tools/hooks/enable-wireless`
```bash
#!/bin/sh
set -e
PREREQ=""
prereqs()
{
echo "${PREREQ}"
}
case "${1}" in
prereqs)
prereqs
exit 0
;;
esac
. /usr/share/initramfs-tools/hook-functions
copy_exec /sbin/wpa_supplicant
# copy WiFi driver
manual_add_modules 88x2bu
copy_file config /etc/initramfs-tools/wpa_supplicant.conf /etc/wpa_supplicant.conf
```
- `/etc/initramfs-tools/scripts/init-bottom/kill_wireless`
```bash
#!/bin/sh
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
# allow the decrypted OS to handle WiFi on its own
echo "Stopping wlan device..."
kill $(cat /run/initram-wpa_supplicant.pid)
ip link set wlx112233445566 down
# created by initramfs
# for some reason it lists wlx112233445566 as ethernet, which breaks netplan - remove it
rm -f /run/netplan/wlx112233445566.yaml
```
- `/etc/initramfs-tools/wpa_supplicant.conf`
```bash
ctrl_interface=/tmp/wpa_supplicant
country=GB
network={
ssid="Foo"
scan_ssid=1
psk="Bar"
key_mgmt=WPA-PSK
}
```
Chmod all scripts you just created:
```bash
chmod +x /etc/initramfs-tools/scripts/init-premount/a_enable_wireless
chmod +x /etc/initramfs-tools/hooks/enable-wireless
chmod +x /etc/initramfs-tools/scripts/init-bottom/kill_wireless
```
Set up WiFi for the decrypted OS. On Ubuntu, you do this by creating e.g. `/etc/netplan/10-user.yaml`:
```yaml
network:
version: 2
ethernets:
eth0:
dhcp4: true
optional: true
wifis:
wlx112233445566:
optional: true
access-points:
"Foo":
password: "Bar"
dhcp4: true
```
You may want to disable onboard WiFi from the decrypted OS so it doesn't conflict with your external USB card:
```sh
echo "dtoverlay=disable-wifi" >> /boot/config.txt
```
You're done! Follow the rest of the guide to finish building your initramfs.
## Resources
- [Wireless-Builtin.md](Wireless-Builtin.md)
- https://github.com/morrownr/88x2bu-20210702
================================================
FILE: Wireless-USB2.md
================================================
# WiFi USB Support
This guide will show you how to set up a WiFi USB dongle for use during initramfs. The steps here are intended for Raspberry Pi OS, slight changes will be necessary for Ubuntu.
## Installing driver
The steps below are written for the [Alfa AWUS036AXML](https://alfa-network.eu/alfa-usb-adapter-awus036axml) USB dongle, which uses the Mediatek MT7921AU driver. The driver is part of the Linux kernel starting from 6.1.
## Set up initramfs
Create the following files and customize them if necessary:
- `/etc/initramfs-tools/scripts/init-premount/a_enable_wireless`
```bash
#!/bin/sh
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
echo "Waiting for wlan device to come up..."
while [ ! -d "/sys/class/net/wlan0" ]; do
sleep 1
done
echo "Initializing wpa-supplicant..."
/sbin/wpa_supplicant -i wlan0 -c /etc/wpa_supplicant.conf -P /run/initram-wpa_supplicant.pid -B
```
- `/etc/initramfs-tools/hooks/enable-wireless`
```bash
#!/bin/sh
set -e
PREREQ=""
prereqs()
{
echo "${PREREQ}"
}
case "${1}" in
prereqs)
prereqs
exit 0
;;
esac
. /usr/share/initramfs-tools/hook-functions
copy_exec /sbin/wpa_supplicant
# copy WiFi driver
manual_add_modules mt7921u
copy_file config /etc/initramfs-tools/wpa_supplicant.conf /etc/wpa_supplicant.conf
```
- `/etc/initramfs-tools/scripts/init-bottom/kill_wireless`
```bash
#!/bin/sh
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
# allow the decrypted OS to handle WiFi on its own
echo "Stopping wlan device..."
kill $(cat /run/initram-wpa_supplicant.pid)
ip link set wlan0 down
```
- `/etc/initramfs-tools/wpa_supplicant.conf`
```bash
ctrl_interface=/tmp/wpa_supplicant
country=GB
network={
ssid="Foo"
scan_ssid=1
psk="Bar"
key_mgmt=WPA-PSK
}
```
Note that this will only set up WiFi in the initramfs. The decrypted OS needs to be configured separately.
Chmod all scripts you just created:
```bash
chmod +x /etc/initramfs-tools/scripts/init-premount/a_enable_wireless
chmod +x /etc/initramfs-tools/hooks/enable-wireless
chmod +x /etc/initramfs-tools/scripts/init-bottom/kill_wireless
```
You may want to disable onboard WiFi from the decrypted OS so it doesn't conflict with your external USB card:
```sh
echo "dtoverlay=disable-wifi" >> /boot/config.txt
```
You're done! Follow the rest of the guide to finish building your initramfs.
## Resources
- [Wireless-Builtin.md](Wireless-Builtin.md)
- https://github.com/morrownr/USB-WiFi/blob/main/home/USB_WiFi_Adapters_that_are_supported_with_Linux_in-kernel_drivers.md
gitextract_oqmpe99l/ ├── README.md ├── Wireless-Builtin.md ├── Wireless-USB.md └── Wireless-USB2.md
Condensed preview — 4 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (26K chars).
[
{
"path": "README.md",
"chars": 13531,
"preview": "# Raspberry Pi Encrypted Boot with SSH\n\n> ⚠️ This guide is only tested on the [Raspberry Pi 5](https://www.raspberrypi.c"
},
{
"path": "Wireless-Builtin.md",
"chars": 3480,
"preview": "# WiFi Support\n\nThis guide will show you how to set up the Raspberry Pi's built-in WiFi module for use during initramfs."
},
{
"path": "Wireless-USB.md",
"chars": 4762,
"preview": "# WiFi USB Support\n\nThis guide will show you how to set up a WiFi USB dongle for use during initramfs. The steps here ar"
},
{
"path": "Wireless-USB2.md",
"chars": 2810,
"preview": "# WiFi USB Support\n\nThis guide will show you how to set up a WiFi USB dongle for use during initramfs. The steps here ar"
}
]
About this extraction
This page contains the full source code of the ViRb3/pi-encrypted-boot-ssh GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 4 files (24.0 KB), approximately 7.3k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.