Creating /boot/ssh and /boot/wpa_supplicant.conf (for a headless setup) works, but how?

Have you ever wondered HOW this works? I did. And it was quite the rabbit hole to go down.
  • First I Googled and found the documentation about creating the file /boot/ssh
  • I never managed to find any official documentation on creating the file /boot/wpa_supplicant.conf (Though I did find lots of references that stated that it did work.)
  • Then I studied https://github.com/RPi-Distro/pi-gen extensively.
  • But I couldn't find anything that looked for /boot/ssh or /boot/wpa_supplicant.conf
  • Then I started looking for packages that may contain the implementation.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    ~/src/pi-gen $ cat $(find stage[012] -type f -name '*package*') | tr " " "\n" | grep 'pi' | sort
    libraspberrypi-bin
    libraspberrypi-dev
    libraspberrypi-doc
    libraspberrypi0
    pi-bluetooth
    python-rpi.gpio
    raspberrypi-bootloader
    raspberrypi-net-mods
    raspberrypi-sys-mods
    raspi-config
    raspi-copies-and-fills
    rpi-update
    
  • I grabbed all the matching packages I could find in https://archive.raspberrypi.org/debian/pool/main/r/
  • I got lucky with raspberrypi-net-mods and raspberrypi-sys-mods
I finally discovered that it is a bit of systemd magic that makes each happen.

raspberrypi-net-mods.service
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[Unit]
Description=Copy user wpa_supplicant.conf
ConditionPathExists=/boot/wpa_supplicant.conf
Before=dhcpcd.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/mv /boot/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
ExecStartPost=/bin/chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf

[Install]
WantedBy=multi-user.target
Notice the ConditionPathExists and ExecStart! That's pretty clever.

Similarly, but slightly more clever.

raspberrypi-sys-mods.sshswitch.service
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[Unit]
Description=Turn on SSH if /boot/ssh is present
ConditionPathExistsGlob=/boot/ssh{,.txt}
After=regenerate_ssh_host_keys.service

[Service]
Type=oneshot
ExecStart=/bin/sh -c "update-rc.d ssh enable && invoke-rc.d ssh start && rm -f /boot/ssh ; rm -f /boot/ssh.txt"

[Install]
WantedBy=multi-user.target
Now that I've uncovered this, I can finally finish the project I started with this thread nearly 5 years ago. It's been a long journey of discovery, this $35 whim I indulged in back in May 2012

https://www.raspberrypi.org/forums/viewtopic.php?t=198660

Keine Kommentare:

Kommentar veröffentlichen

Datei in einer windows.wim image ändern

 Um Dateien in einer Datei.wim zu ändern, und zwar nicht im 1. Index sondern 2. öffnet man Powershell (ich habs jetzt im admin Modus gestart...