← How-Tos
raspberry-pi 1 hr ago ◯ 4 min read

Raspberry Pi Read-Only Root Filesystem: Overlay FS Setup to Prevent SD Card Corruption

raspberry-piread-onlyoverlayfssd-cardreliabilityraspi-configkioskembedded

The most common way a Raspberry Pi fails in the field is a corrupted SD card after an unexpected power loss. Kiosks, sensors, signage and camera nodes are powered off by pulling the plug, and a write in progress at the wrong moment can damage the filesystem. Running the root filesystem as read-only, with all changes held in RAM, removes the problem. This guide covers the built-in overlay filesystem option in Raspberry Pi OS and how to keep useful data when the root is read-only.

How the Overlay Works

The overlay filesystem layers a RAM-based writable filesystem on top of the read-only root partition. Programs can write files as normal, but those writes exist only in memory and vanish on reboot, while the SD card is never modified. That means the system boots into the same known-good state every time.

Prepare the System First

  1. Install and configure everything the Pi needs to do, and test it.
  2. Update the system and remove packages you do not need.
  3. Set the timezone, hostname, Wi-Fi and SSH settings you require. They cannot be changed while the overlay is active without disabling it.
  4. Take a backup image of the SD card.

Enabling the Overlay with raspi-config

  1. Run sudo raspi-config.
  2. Go to Performance Options, then Overlay File System.
  3. Choose to enable the overlay file system.
  4. When asked whether the boot partition should be write-protected, choose yes for maximum protection.
  5. Finish and reboot.

After reboot, run mount and look for a line showing the root as overlay. You can also try to create a file in the home directory, reboot, and confirm the file is gone.

Making Changes Later

To install software or edit settings, run raspi-config again, disable the overlay and, if you enabled it, the read-only boot partition, then reboot. Make your changes, then re-enable the overlay. It is easy to forget you are in read-only mode, so note it in a label or in the hostname of the device.

Keeping Data That Must Survive

Some data has to persist, such as sensor logs, captured images or configuration. Options include:

ApproachBest forNotes Separate writable partition on the SD cardSmall logs, configMount it read-write and limit write frequency USB flash or SSDLarge data, camera imagesUse a mount entry in fstab with nofail; SSD endures more writes Network storage (NFS, SMB)Centralised loggingNeeds a reliable network Remote syslog or MQTTLogs and telemetryNo local writes at all

A separate data partition should still be shut down cleanly where possible, and you should use a journalling filesystem such as ext4 and a periodic filesystem check.

Details Worth Handling

Reduce SD Card Wear Even When Writable

If you cannot use a read-only root, reduce writes by moving logs to RAM with a tool such as log2ram, disabling swap or moving it elsewhere, and using a high-endurance SD card. A high-endurance card or SSD boot is much more reliable than a cheap consumer card.

Troubleshooting

ProblemCauseFix Changes vanish after rebootOverlay is doing its jobDisable overlay to make permanent changes Service fails to startIt tries to write to a read-only pathPoint it at a tmpfs or the data partition Out of memory over daysLogs or caches filling the overlayLimit journald, clean temporary directories Cannot save Wi-Fi changesRoot is read-onlyDisable overlay, change settings, re-enable

Closing

A read-only root turns a Raspberry Pi into an appliance: the same clean system boots every time, and you can switch it off at the wall without ceremony. Configure everything first, enable the overlay, and put any data you need in a separate, deliberately writable location.