Raspberry Pi Read-Only Root Filesystem: Overlay FS Setup to Prevent SD Card Corruption
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
- Install and configure everything the Pi needs to do, and test it.
- Update the system and remove packages you do not need.
- Set the timezone, hostname, Wi-Fi and SSH settings you require. They cannot be changed while the overlay is active without disabling it.
- Take a backup image of the SD card.
Enabling the Overlay with raspi-config
- Run sudo raspi-config.
- Go to Performance Options, then Overlay File System.
- Choose to enable the overlay file system.
- When asked whether the boot partition should be write-protected, choose yes for maximum protection.
- 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 allA 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
- Logs: with the overlay active, journald writes to RAM and logs are lost at reboot. Limit their size with SystemMaxUse or RuntimeMaxUse in journald.conf so RAM does not fill up.
- RAM use: anything written accumulates in memory until reboot. Watch for apps that write large caches or temporary files.
- Time: the Pi has no real-time clock, so it relies on NTP. Without network access, the clock may be wrong after a reboot. Add an RTC module such as the DS3231 if the device is offline.
- Updates: unattended upgrades cannot run in read-only mode. Plan maintenance windows for updates.
- Wi-Fi and DHCP leases: some state is lost. Use static configuration where it matters.
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-enableClosing
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.