Embedded Linux won't reboot, how to force it and repair the file system
I have a lot of embedded systems running in my product testing lab or at home, all of them either as Raspberries or self-made PCB with Yocto. Sometimes I can't reboot some systems, I think its the journald which causes some issues with SD cards, the error message usually is:
Failed to open /dev/initctl
Anyhow, if you have this issue - a reboot can be force-forced:
systemctl --force --force reboot
Since a forced reboot does not sync the journal, the system typically comes up with a damaged FS. The remote fsck can be initiated by extending the command above with
sudo tune2fs -i 1m /dev/DISK && touch /forcefsck && systemctl --force --force reboot
(assumed you have access to a shell, via SSH or local access). When all goes fine, the system comes up with a clean FS. All this fuss comes from the SD card writes. A well designed embedded IoT device should have dedicated flash memory for the running OS.
Why the reboot hangs
A normal reboot asks systemd to shut down. When systemd or its bus is stuck, the command falls back to the old SysV way of talking to init, and on a systemd system that fails with Failed to open /dev/initctl. Nothing happens and the box stays up.
- I hit this myself on Raspberry Pi gateways out in the field, not only in the lab.
- The steps below are how I got them back.
How to force the reboot
Try the soft force first. Per the systemctl docs, a single --force skips the normal service shutdown but still kills all processes and unmounts or remounts the file systems read-only:
sync
systemctl reboot --force
Only if that also hangs, use --force twice. systemctl then reboots by itself without asking systemd, which is why it works when systemd is stuck. It does not stop processes or unmount anything, so data can be lost. That is why the file system often comes up damaged.
Make sure the file system gets checked
Two parts of the command above need an update. tune2fs -i 1m only sets a check interval of one month, so it does not force a check on the next boot by itself. And systemd treats the /forcefsck file as deprecated, and writing to a file system you think is broken is not a good idea anyway. The current way is the fsck.mode=force kernel parameter from systemd-fsck, usually together with fsck.repair=yes. On Raspberry Pi OS add both to the single line in cmdline.txt on the boot partition, reboot once, then remove them again, or every boot will run a full check.
Stop it from happening again on SD cards
- Keep the journal in RAM with
Storage=volatilein/etc/systemd/journald.conf(see journald.conf). You lose logs on reboot, so ship the ones you need off the device. - Run the root file system read-only. Yocto has the
read-only-rootfsimage feature for this, and Raspberry Pi OS has an overlay file system option inraspi-config. - For devices in the field, use proper flash such as eMMC instead of an SD card for the running OS.
More on running devices at the edge: infinimesh 3.4 with rules, alarms and OTA at the device layer, connecting BACnet to the cloud with a gateway on an edge box and streaming IoT data to S3.
Need help with distributed systems, backend engineering or data platforms? See my consulting services.