Skip to content

How to update wifi after first boot? #82

Closed

Description

@jedahan

It looks like /boot/custom.toml is applied on firstboot, but if I have an SD card of an existing image, and move from one location to another and want to update the wifi, can we have /boot/custom.toml apply on every boot?

Activity

  1. jedahan commented on Oct 24, 2023

    @jedahan
    Author

    This assume custom.toml is any (partial? full?) cloud-init file. I haven't gotten to digging that far yet.

  2. XECDesign commented on Oct 25, 2023

    @XECDesign
    Member

    custom.toml is just an experimental thing in case cloud-init doesn't work out. There are no plans to support it and it will be removed after cloud-init is added.

  3. jedahan commented on Oct 25, 2023

    @jedahan
    Author

    Outside of the specific implementation I mentioned (with custom.toml), is there generally a way to update the wifi settings after first boot via the sd card with cloud-init?

  4. XECDesign commented on Oct 25, 2023

    @XECDesign
    Member

    Not right now, no.

  5. XECDesign commented on Oct 25, 2023

    @XECDesign
    Member

    I should clarify that there's no supported way. There's nothing stopping anybody from implementing their own preferred scheme. Getting basic cloud-init functionality isn't too difficult and google should give plenty of examples. You can also tell systemd to run a specific script on startup by editing cmdline.txt and providing the script in the boot/firmware partition. That approach is how rpi-imager works, if you need a reference for how to do it.

  6. jedahan commented on Oct 25, 2023

    @jedahan
    Author

    Thanks for the context.

    If I was interested in extending rpi-imager's firstrun.sh to create a supported way, anything else I should look out for? Where would be the most appropriate place for discussion and potential pull requests?

    (Likely something as simple as cp /boot/*.nmconnection /etc/NetworkManager/system-connections && rm /boot/*.nmconnection)

  7. XECDesign commented on Oct 25, 2023

    @XECDesign
    Member

    Why would that be better than cloud-init?

  8. XECDesign commented on Oct 25, 2023

    @XECDesign
    Member

    Just to add a bit of context, so it doesn't sound like I'm just dismissing a potential quality of life improvement for no reason..

    When we had a few changes like turning on SSH or configuring wifi, dropping files into the boot partition was a quick and dirty solution. However, it has some drawbacks.

    • The underlying OS itself can also change. /boot has changed to /boot/firmware, but rpi-imager has /boot hard-coded everywhere. Network Manager doesn't use a static wpa_supplicant.conf file, so the previous approach doesn't work at all.
    • It's not a standard way of doing things, so it leaves other distros out and requires rpi-imager to try to guess what the right way to do things is.
    • Users will always want to do something more than whatever functionality we provide. If we keep extending things by dropping files into the boot partition we'll soon have to support 50 other files that can be added.
    • Good luck making sure all of these options work in all the possible combinations

    So should we update rpi-imager any time the OS changes and pray that all users always use the latest version? How would rpi-imager know what the right approach is?

    custom.toml addresses only addresses the first point.

    As a de-facto industry standard, cloud-init should address everything and will be a better long-term solution overall.

  9. jedahan commented on Oct 25, 2023

    @jedahan
    Author

    Yeah I’m happy to help with cloud-init if that’s the best way forward, just do not have experience with how it’s integrated with raspberry pi OS right now, so trying to figure out what’s needed to enable per-boot updates support, and how to get cloud-init to read updates from /boot

  10. XECDesign commented on Oct 25, 2023

    @XECDesign
    Member

    No help or discussion needed, it's all on the todo list already. It's just a matter of getting around to it. We've just launched the pi5 and the bookworm release, so as you can imagine, things are a bit busy right now.

    I can't just merge proposed changes without fully understanding and testing them. This isn't something that can be rushed along.

  11. ViRb3 commented on Nov 7, 2023

    @ViRb3

    Sorry for reviving this issue @XECDesign, but I just wanted to understand the situation a bit better. The initial config appears to be quite confusing at the moment.

    If you use the official imager tool, it will generate /boot/firstboot.sh which calls /usr/lib/raspberrypi-sys-mods/imager_custom to configure the system, with fallback to doing things manually in case the imager doesn't exist. The imager will use raspi-config for WiFi, while the fallback will just set /etc/wpa_supplicant/wpa_supplicant.conf, which no longer works on Bookworm (12) according to official docs. Should I understand that the fallback is for the old Bullseye (11), and the "correct" way to do it for Bookworm (12) is via the imager? I've been reading some people successfully configure WiFi on Bookworm by just setting wpa_suppplicant.conf, so I am a bit confused here...

    And lastly, on the cloudinit vs. custom.toml part. it seems like the second init script /usr/lib/raspberrypi-sys-mods/firstboot checks for a custom.toml and passes it to /usr/lib/raspberrypi-sys-mods/init_config if it exists. This script will then parse it and invoke the imager just like above. I saw no trace of cloudinit support, and no custom.toml is generated with the official imager. Should I understand that the former is not implemented yet, while the latter is just an experiment in case cloudinit support cannot be added?

    Thanks a lot!

  12. XECDesign commented on Nov 7, 2023

    @XECDesign
    Member

    Should I understand that the fallback is for the old Bullseye (11), and the "correct" way to do it for Bookworm (12) is via the imager?

    The fallback is for very old images which didn't have imager_custom.

    I've been reading some people successfully configure WiFi on Bookworm by just setting wpa_suppplicant.conf, so I am a bit confused here...

    That doesn't have a chance of working on a Raspberry Pi OS Bookworm image, so there's probably some missing context.

    Should I understand that the former is not implemented yet, while the latter is just an experiment in case cloudinit support cannot be added?

    Yup, spot on.

  13. omya3qno commented on Nov 23, 2024

    @omya3qno

    One year later, what is the blessed way to bulk-produce a set of sdcards with already-booted images, but different wifi-settings, i.e. "how to update wifi after first boot"?

  14. matthijs166 commented on Dec 3, 2024

    @matthijs166

    I'm missing exactly this option... Only have access to the SD card on an MacBook, I only can access bootfs.
    There is no way for me to change the wifi and keep all the data on de card.

  15. adamkerz commented on Feb 17, 2026

    @adamkerz

    I had the same need, moved house, WiFi changed. Spent too long to figure out this fairly simple guide.

    1. You can run a script on startup by changing cmdline.txt on the boot partition. Add to the end:

      debug systemd.run=/boot/firmware/add_wifi_connection.sh systemd.run_success_action=reboot systemd.run_failure_action=exit systemd.unit=kernel-command-line.target
      

      debug - remove quiet if it is there; get as much info logged as possible
      systemd.run=<script to run> - eg. /boot/firmware/something.sh
      systemd.run_success_action=reboot - reboot back into a normal state. This only works if these systemd.* options are removed from cmdline.txt
      systemd.run_failure_action=exit - stop the system so that you know it failed. Alternative is none, which MIGHT (untested) leave you in a state where you can debug via ethernet, eg ssh then sudo journalctl -u kernel-command-line.service -n 200
      systemd.unit=kernel-command-line.target - changes the default systemd unit of default.target

      A possibly not obvious, but important, detail: systemd.run triggers systemd-run-generator, which creates a unit named kernel-command-line.service BUT it doesn't do anything more. You must also add systemd.unit=kernel-command-line.target to override the default of default.target

      Remember that the boot partition is mounted at /boot/firmware/ now-a-days, no longer just /boot/

    2. Create your own .nmconnection file. Suggest you modify an existing one. Use your brain - I don't know what yours looks like, but this was what I needed to do:

    3. Put this script on the boot partition, called add_wifi_connection.sh and tweak the TODO parts:

      #!/bin/bash
      # Append `debug systemd.run=/boot/firmware/add_wifi_connection.sh systemd.run_success_action=reboot systemd.run_failure_action=exit systemd.unit=kernel-command-line.target` to cmdline.txt to run this file
      # remove `quiet` option (replaced by debug)
      
      # exit on first error (doesn't seem to work)
      set +e
      
      # configure wifi
      raspi-config nonint do_wifi_country "AU" # TODO
      echo Adding new wifi config 
      NETWORK_CONNECTION_FILE=wifi.nmconnection # TODO
      NETWORK_CONNECTION_PATH=/etc/NetworkManager/system-connections/$NETWORK_CONNECTION_FILE
      mv /boot/firmware/$NETWORK_CONNECTION_FILE $NETWORK_CONNECTION_PATH
      chmod 600 $NETWORK_CONNECTION_PATH
      chown root:root $NETWORK_CONNECTION_PATH
      rm /etc/NetworkManager/system-connections/old.nmconnection # TODO, remove old connection file, if needed
      
      # clean up
      echo Cleaning up
      # remove the "on startup" instructions that launch this script, then reboot. Replacing debug first is important
      sed -i 's| debug | quiet |g' /boot/firmware/cmdline.txt
      sed -i 's| systemd.*||g' /boot/firmware/cmdline.txt
      exit 0
      
    4. Debug, if needed

      • use an ethernet cable to ssh into the Pi, if you can
      • systemctl status kernel-command-line.target, journalctl, dmesg are all useful; sorry, I've turned the Pi off now and don't have the exact commands I was using to hand. Google is your friend.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions