HardwareHardware problems and fixes

Raspberry Pi SD card corruption on an always-on agent host

An agent host writes all the time: logs, session history, memory files, package caches, sometimes swap. On a Raspberry Pi booting from an SD card, that steady writing meets the two things Raspberry Pi names as causes of storage corruption: power cuts and poor power supplies. This page covers why it happens and the three fixes Raspberry Pi's own docs support.

In short: Use the official 27 W supply, move the system to an SSD (NVMe on a Pi 5, USB on a Pi 4), and put the board on a UPS if your power is unreliable. OpenClaw's own Pi page says the same about storage: "Use a USB SSD, SD cards are slow and wear out." The read-only overlay file system also stops writes to the card, but it throws away every change at reboot, which rules it out for the agent's own data.

At a glance
Cause 1 (Raspberry Pi)power dropouts can cause storage corruption
Cause 2 (Raspberry Pi)low quality power supplies can corrupt storage
Fix for the systeman SSD: M.2 HAT+ on a Pi 5, or USB 3
Fix for the supplythe 27 W USB-C supply (5.1 V, 5.0 A) and a UPS
Overlay file systemroot read-only, changes in RAM, lost at reboot

Why the card corrupts

Raspberry Pi's power supply docs say it plainly: "Raspberry Pi devices are frequently used as data storage and monitoring devices, often in places where sudden power-downs can occur. As with any computing device, power dropouts can cause storage corruption." And about supplies: "Low quality power supplies can corrupt storage or cause unpredictable behaviour within the Raspberry Pi."

An agent host makes both more likely to hurt. It is on all day, so it is on when the power cuts. It is writing at almost any moment, so a cut is likely to land in the middle of a write. And SD cards are slow at the small random writes an agent makes: the official Raspberry Pi card, rated A2 with command queueing, is documented at 2,000 random 4 kB writes per second on a Pi 5 and 1,200 on a Pi 4. The official SSDs are documented at 70,000 to 90,000. Slower writes mean more time with data in flight.

Raspberry Pi does not claim that an A2 card prevents corruption; A2 is a performance rating of its card. It does say card quality matters: "SD card quality is a critical factor in determining the overall user experience for a Raspberry Pi."

Check the power first

Before buying storage, rule out the supply. Every Pi since the B+ (except the Zero range) detects a drop below 4.63 V (±5%) and logs it. Run:

vcgencmd get_throttled

If bit 0 (0x1, undervoltage now) or bit 16 (0x10000, undervoltage since boot) is set, the supply or cable is the first thing to change. Raspberry Pi's advice: "If you see warnings, switch to a higher quality power supply and cable." For a Pi 5 it recommends the 27 W USB-C supply (27W supply on Amazon). The full decoding and the 5A message are on the power supply warning page.

Move the system to an SSD

This is the fix that removes the card from the write path. On a Pi 5, the PCIe connector takes an NVMe drive through the M.2 HAT+ (M.2 HAT+ on Amazon), which supports 2230 and 2242 drives such as the Raspberry Pi SSD (Raspberry Pi SSD on Amazon). Booting from it needs a bootloader setting:

sudo rpi-eeprom-config --edit
# then set:
BOOT_ORDER=0xf416

The M.2 HAT+ page adds that with no SD card inserted, a Pi 5 "boots automatically from your NVMe drive". If the drive does not appear, the checks in order are on Pi 5 NVMe not detected; the full setup is on NVMe on the Raspberry Pi 5.

On a Pi 4, which has no PCIe connector, a USB 3 SSD (USB 3 SSD on Amazon) is the route; Pi 4 and newer boot from USB by default once USB is in the boot order, although early Pi 4 boards may need a bootloader update first. One catch on a Pi 5: USB boot is disabled by default on a 3A supply, another reason to use a 5A one.

The read-only overlay, and why it rarely suits an agent

Raspberry Pi OS can make the root file system read-only, with a temporary overlay in RAM, and write-protect the boot partition. In sudo raspi-config it is under 4 Performance Options > P2 Overlay File System. With nothing written to the card, a power cut has nothing to corrupt.

The price is stated in the same docs: "any changes will be lost when your Raspberry Pi reboots or powers off." For a kiosk or a sensor logger that is fine. For an agent it means losing its sessions, its memory, its configuration changes and every update. It only works if the agent's data lives on another drive that stays writable, at which point that drive is already the better boot disk. It also puts the writes in RAM, which a small board does not have to spare.

Power cuts: a UPS and a clean shutdown

An SSD makes writes faster; it does not make a power cut safe. Where cuts happen, add a UPS (line-interactive UPS on Amazon) so the board can shut down cleanly; the options, including UPS HATs for the Pi 5, are on the UPS page. If you stay on a card for now, keep it to the system only and take a decent one: Raspberry Pi recommends at least 8 GB for the Lite image and 32 GB for the desktop images, and its own card comes in 32, 64 and 128 GB.

What to buy

Shop links go through /go/ and open Amazon (amazon.fr in the EU and Switzerland, amazon.com elsewhere). As an Amazon Associate we earn from qualifying purchases. Your price does not change and no brand pays for a place on this page (disclosure). We write no price down: it moves, the shop shows today's.

Questions

Why does my Raspberry Pi keep corrupting SD cards?
Raspberry Pi names two causes: power dropouts and low quality power supplies. An always-on agent that writes constantly is exposed to both. Check vcgencmd get_throttled for undervoltage, then move the system to an SSD.
Does an A2 or high-endurance card stop corruption?
Raspberry Pi does not claim that. A2 is a speed rating; its official card adds command queueing and faster random IO. A better card is faster, but a power cut mid-write can still corrupt it.
Should I enable the overlay file system on an agent host?
Usually not. It keeps the card read-only, but every change is lost at reboot or power-off: sessions, memory, config and updates. It suits a fixed appliance, not an agent.
NVMe or USB SSD on a Pi 5?
NVMe through the M.2 HAT+ is the native route on a Pi 5. A USB 3 SSD works too, but USB boot is disabled by default on a 3A supply, so use the 27 W supply either way.
Is a UPS worth it for a Raspberry Pi?
Where power cuts happen, yes: Raspberry Pi lists power dropouts as a cause of storage corruption, and an SSD does not change that. A UPS gives the board time to shut down.

Sources