Skip to Content

SD card corrupted? 6 habits for Linux in the field

Short answer: an SD card that gets corrupted in a Linux device is often the result of a power cut during a write, not only of a faulty card. Let writes finish, keep ext4 journaling, shut down from software, plan for outages with a shutdown signal, write less and protect the system with a read-only root and a backup image.

Download the checklist (PDF, 2 pages)

Descarga la lista de comprobación (PDF, 2 páginas)

These habits apply to any Linux device that boots from an SD card or eMMC: Raspberry Pi based PLCs and panel PCs, gateways and edge computers. Cards also fail from wear or poor quality, so look at both causes before you replace one.

Six habits, in this order

HabitWhat to doGood result
1. WritesApplications write to the RAM cache first and the kernel moves the data to the flash later. ext4 commits data and metadata every 5 seconds by default (the commit mount option). If the power goes in between, that data can be lost. Applications that must not lose a record call fsync after writing it.Critical data is on the flash before the application moves on.
2. File systemKeep ext4 with its journal and the default data=ordered mode, where data is written before the metadata is committed to the journal. Journaling keeps the structure of the file system consistent; it does not guarantee the content of a file that was being written. It also relies on the card writing in order, so it cannot cover a failure inside the card controller.The file system mounts after a cut; the files that were open are checked.
3. ShutdownShut down from software before you remove power: sudo poweroff or the Shutdown option of the desktop. A power button wired to an input that triggers the shutdown (on Raspberry Pi, the gpio-shutdown overlay) tells the operating system; a switch that cuts the supply does not.The system is off before the supply is cut.
4. OutagesA UPS or a supercapacitor helps when it tells the system to shut down in time. With Network UPS Tools, upsmon runs the shutdown command when the UPS reports on battery and low battery. Some backup boards for Raspberry Pi signal the power loss on a GPIO input instead. Without a signal, the backup only delays the cut.Every outage ends in a clean shutdown.
5. Write lessThe flash in SD cards has a limited write cycle lifetime. Keep the systemd journal in RAM with Storage=volatile, put temporary files on tmpfs and turn off debug logging to the card. Logs in RAM are lost at reboot: send the ones you need for diagnosis off the device.Fewer writes to the flash, slower wear.
6. ProtectMount the root read-only, or enable the overlay file system in raspi-config: writes go to RAM and are lost at reboot. Choose cards from a reliable brand, industrial grade (power fail protection, extended temperature range) or eMMC if the site needs it; eMMC is not immune either. Keep a tested backup image and the data off the device.Recovery is a reflash, not a rebuild.

What protects what

  • Journaling: the structure of the file system, not the file being written.
  • Software shutdown: the writes still in the cache.
  • UPS or supercapacitor with a signal: power cuts.
  • Fewer writes: flash wear.
  • Read-only or overlay root: the operating system.
  • Backup image: the time to recover.

Reference commands

Shut down: sudo poweroff. Logs in RAM: Storage=volatile in the [Journal] section of /etc/systemd/journald.conf. Temporary files in RAM: a line such as tmpfs /tmp tmpfs defaults,noatime,nosuid 0 0 in /etc/fstab. Overlay: sudo raspi-config > Performance Options > Overlay File System. Logs in RAM and changes on the overlay are lost at reboot: keep the data you need on a separate partition or on another device.

Next steps

Sources

Raspberry Pi Ltd: Making a more resilient file system (white paper RP-003610-WP). Raspberry Pi documentation: Getting started (Shutdown options) and Configuration (raspi-config). Linux kernel documentation: ext4 and tmpfs. systemd: journald.conf(5). Linux man-pages: fsync(2). Network UPS Tools: upsmon(8).

¿SD corrupta tras un corte? Seis hábitos para equipos Linux en campo, de la caché de escritura al SAI y la raíz de solo lectura.

Commenting is not enabled on this course.