Reliability engineering
SD card corruption on Raspberry Pi, and how to avoid it
Updated
Most SD card corruption on a Raspberry Pi comes from power: the supply sags or is cut while the card is writing. Raspberry Pi’s own documentation says “power dropouts can cause storage corruption” and that “low quality power supplies can corrupt storage”. The fix is layered: give the board stable power, write less, make the root filesystem read-only, use eMMC or NVMe instead of an SD card where you can, and shut down cleanly when power fails.
Why do SD cards corrupt?
Three things have to line up: a write in progress, a power interruption, and storage that cannot finish or roll back that write.
- Power quality. On current Pi boards, low-voltage detection triggers when the supply drops below 4.63 V (±5 %), and the documentation’s advice is to “switch to a higher quality power supply and cable” if you see warnings (Raspberry Pi documentation). A sagging supply can cause brown-outs during writes even when the power is never fully cut.
- Writes in flight. Linux holds writes in RAM and flushes them later. With ext4, the default
commitinterval is 5 seconds, and the defaultdata=orderedmode writes data out before committing the related metadata to the journal (kernel ext4 documentation). The journal protects filesystem structure, but anything not yet flushed is lost when power goes. - The card itself. An SD card’s controller maps logical blocks onto flash and moves data around internally. If power is cut during that internal work, the damage can be below the filesystem, where
fsckcannot repair it. Consumer card makers generally do not publish how their cards behave on power loss, so you cannot rely on it either way.
The more a system writes, the more often a power cut lands mid-write. Logging, databases and swap on the SD card make it worse.
What actually happens when power is lost?
| What you see | Likely cause | Recoverable? |
|---|---|---|
| Last few seconds of logs or data missing | Writes still in RAM (ext4 commit interval) | Data is gone; filesystem is fine |
Boot runs fsck, fixes errors, continues |
Journal replay or minor metadata damage | Usually yes |
| Read-only remount, boot drops to emergency shell | Filesystem metadata damage | Often, with fsck; sometimes needs reimaging |
| Card not detected, wrong size, or every write fails | Card controller damage | No; replace the card |
eMMC vs SD vs NVMe on the CM5
The Compute Module 5 changes the choice. According to Raspberry Pi’s Compute Module documentation:
- CM5 comes with 16, 32 or 64 GB of eMMC soldered on the module, or as CM5Lite with none.
- The CM5IO board’s microSD slot works only with CM5Lite; “other variants ignore the slot”.
- CM5IO has an M.2 M-key PCIe socket (2230 to 2280). Booting from NVMe needs the bootloader
BOOT_ORDERset to0xf6.
| microSD (CM5Lite) | eMMC (CM5) | NVMe SSD (PCIe) | |
|---|---|---|---|
| Replaceable | Yes, by hand | No, soldered | Yes |
| Vibration and contact issues | Socket can loosen | None | M.2 screw-down |
| Capacity on CM5 | Card-dependent | 16, 32 or 64 GB | Drive-dependent |
| Power-loss behaviour | Card-dependent, rarely documented | Better than SD in practice, still not guaranteed | Depends on drive; some have power-loss protection, most consumer drives do not |
| Main risk | Card quality varies widely | Wear-out ends the module’s life | Higher power draw to hold up during shutdown |
Raspberry Pi describes eMMC as providing “persistent storage with low power consumption and built-in features that improve reliability”. That is real, but no storage type removes the need for clean power. Treat eMMC and NVMe as reducing the risk, not as making power cuts safe.
For a controller in a cabinet, eMMC is the usual choice: nothing to vibrate loose, nothing for a site visitor to swap. Size it so that wear is not a concern over the product life, and keep writes low anyway.
How do you reduce writes?
These assume Raspberry Pi OS (Debian 12 or 13 based) with systemd.
Keep the journal in RAM instead of on flash. Logs are lost at reboot, so forward them elsewhere (a syslog server or MQTT) if you need them.
sudo mkdir -p /etc/systemd/journald.conf.d
sudo tee /etc/systemd/journald.conf.d/volatile.conf > /dev/null <<'EOF'
[Journal]
Storage=volatile
RuntimeMaxUse=32M
EOF
sudo systemctl restart systemd-journald
Other changes that help:
- Mount filesystems with
noatimein/etc/fstabso reads do not cause writes. - Avoid swap on flash. Check what your image uses for swap before disabling it, since this has changed between Raspberry Pi OS releases.
- Batch application writes. Writing one sample per second to SQLite is far harder on flash than writing a minute of samples at once.
Should the root filesystem be read-only?
For a controller that runs a fixed application, yes. Raspberry Pi OS supports an overlay file system: the root stays read-only and a RAM layer takes all writes. Raspberry Pi’s documentation warns that with it enabled “any changes will be lost when your Raspberry Pi reboots or powers off”, and it can also write-protect the boot partition (Raspberry Pi documentation).
Enable it from raspi-config under Performance Options, Overlay File System, or non-interactively. In raspi-config’s source, 0 means enable:
sudo raspi-config nonint do_overlayfs 0
sudo reboot
Run sudo raspi-config nonint do_overlayfs 1 and reboot to turn it off for updates.
What this needs from your design:
- A separate writable partition for data you must keep (configuration, logs you care about), mounted with care and written rarely.
- An update process that does not depend on writing to root at runtime: disable the overlay, update, re-enable; or use A/B images.
- RAM headroom, since all writes land in memory until reboot.
A read-only root removes the most common failure: a corrupted system that will not boot. It does not protect the writable data partition.
UPS, supercapacitors and clean shutdown
The best protection is to never lose power during a write: detect the outage, shut down, then let power go.
- Hold-up energy. A battery UPS or supercapacitor bank keeps the 5 V rail up for a known time after input power fails.
- Power-fail signal. The UPS or hold-up circuit raises a signal when input power is lost.
- Shutdown trigger. The
gpio-shutdowndevice tree overlay turns a GPIO change into aKEY_POWERevent that systemd-logind handles by shutting down (overlays README).
# /boot/firmware/config.txt
# Shut down when GPIO 17 goes low (power-fail signal, active low, pull-up enabled)
dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up
Pick a GPIO your carrier board actually wires to the power-fail signal; 17 is an example.
Sizing a supercapacitor. Usable energy between two voltages is ½·C·(V₁² − V₂²). As an illustration only: if the board draws 6 W and a clean shutdown takes 10 s, you need 60 J plus margin for converter losses. A 10 F bank charged to 5.0 V and discharged to 2.5 V through a boost converter gives ½·10·(25 − 6.25) ≈ 94 J before losses. Measure your own shutdown time and power draw under load; NVMe drives and radios raise both.
A clean shutdown also matters for the CM5 bootloader: Raspberry Pi warns that a power failure during an EEPROM update “could corrupt the EEPROM” (Compute Module documentation).
A checklist for field units
- Supply rated for the board with margin; no undervoltage warnings in the logs.
- eMMC or NVMe instead of SD where the hardware allows.
- Journal in RAM,
noatime, no swap on flash. - Overlay file system on; data on a separate partition.
- Power-fail detection wired to
gpio-shutdown, with hold-up time measured, not guessed. - Hardware watchdog enabled, so a hung system recovers without someone cutting power.
- A tested recovery path: reimage over USB or network, and a backup of configuration.
How will halfnine handle this?
halfnine’s controller is not built yet. To be decided: storage type and size, hold-up circuit and measured hold-up time, read-only root and update scheme. These will be published with test results on the test reports page once they exist.
Sources
- Raspberry Pi documentation, Raspberry Pi hardware (power supply, power button), accessed 2026-11-19
- Raspberry Pi documentation, Compute Module hardware, accessed 2026-11-19
- Raspberry Pi documentation, Configuration (raspi-config overlay file system), accessed 2026-11-19
- raspi-config source (do_overlayfs), accessed 2026-11-19
- Raspberry Pi firmware, device tree overlays README (gpio-shutdown), accessed 2026-11-19
- Linux kernel documentation, ext4 mount options, accessed 2026-11-19