raspberian-firstboot icon indicating copy to clipboard operation
raspberian-firstboot copied to clipboard

How to reboot after firstboot?

Open janoskut opened this issue 3 years ago • 4 comments

This project is awesome and I can't thank you enough for also providing the patched Raspberry Pi OS images. I have no understanding why something like this isn't in the official images since years, and is still being refused to be added.

While loving raspberian-firstboot, I'm fighting with rebooting the system after executing my firstboot.sh script. I assume this could be a generally useful case, reboot after setup.

The issue is that when we just invoke reboot shutdown -r now, the system reboots without completing systemd's firstboot.service. Hence the firstboot.sh file doesn't get removed and will be executed again.

All my attempts to use a delayed reboot with something like

  • nohup sh -c "sleep 3 && touch /boot/reboot && reboot" > /dev/null 2>&1 &
  • setsid sh -c "sleep 3 && touch /boot/reboot && reboot" > /dev/null 2>&1 &

failed. The commands don't seem to be executed.

The only thing that worked is shutdown -r +1, but that takes 1 full minute to reboot. 1 minute is the smallest time unit, unfortunately.

Is there anything that can be done about making it possible to reboot after successful script execution?

janoskut avatar May 18 '21 22:05 janoskut

Wow, I tried so long, but after compiling my issue description above I found a way which works: systemd-run.

systemd-run --no-block sh -c "sleep 3 && touch /boot/reboot && reboot"

(the --no-block option shouldn't actually be necessary, but also doesn't hurt).

I'm not sure if this is the best usage of systemd-run. But if you like it and don't know of any better canonical solution, I'd be happy to contribute an example to the repo.

janoskut avatar May 18 '21 22:05 janoskut

This is a great solution! Please send a PR and I will merge it right away. THANK YOU @janoskut!!

nmcclain avatar Jun 19 '21 21:06 nmcclain

Oh my god I'm so sorry that this has slipped through my attention! I added a PR in #16 , but that's just a snippet from what I came up with back then. I better give it another test next day.

janoskut avatar Dec 28 '21 00:12 janoskut

I found a Stack Exchange thread that gave this code:

sync
umount /boot
mount / -o remount,ro
sync
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger

What this does is

  1. All writes stored in the cache, write them to the disk
  2. Unmount the boot partition
  3. Remount the root partition
  4. Do Step 1 again
  5. Enable all sysrq features
  6. Finally, trigger a reboot with sysrq

User8395 avatar Jan 10 '22 16:01 User8395