Last mod: 2024.10.29

Raspberry Pi - OS protection during power failure

In IoT systems, maintaining a valid filesystem after sudden power loss is crucial for data integrity, system stability, device lifespan, etc. Power loss can corrupt filesystems, leading to data loss, boot failures, and increased wear on storage media. To mitigate these risks, strategies like using read-only and employing RAM-backed storage can help preserve the filesystem and reduce the impact of unexpected shutdowns.
sudo apt-get update
sudo apt-get install overlayroot -y

There is no point in updating the package list that will not be saved on the microSD card anyway. We will comment on the cyclically called script:

sudo mv /usr/lib/apt/apt.systemd.daily /usr/lib/apt/apt.systemd.daily.DISABLED

Open file /etc/overlayroot.conf

sudo vi /etc/overlayroot.conf

and modify parametr overlayroot, set value tmpfs:

overlayroot="tmpfs"

Reboot and we have read only system.

If we need a part of the file system (e.g. /var/log) for writing, this is also possible. To do this, modify the partition table and create an additional partition, preferably one that is quite resistant to sudden power cuts.

  1. We can use GParted for this purpose: GParted

  2. Next modify /etc/fstab. In the example I added the line:

PARTUUID=c43ebaf9-03  /var/log        ext4    defaults,noatime  0       3

fstab

  1. Edit file /etc/overlayroot.conf, remove old configuration and add:
overlayroot="tmpfs:recurse=0"
overlayroot_cfgdisk="disabled"
overlayroot_ro_mountopts="defaults,ro"
overlayroot_chroot=""

overlayroot_cfg="dev=/dev/mmcblk0p2,recurse=1"
overlayroot_rw_dirs="/var/log"

Remove the card from the computer and insert it into the Rapsberry.