Linux – Making SD card corruption-proof

corruptionembeddedfilesystemslinuxsd-card

My embedded linux device uses an SD card for saving certain diagnostics data, far too copious for internal flash.

The problem is if the device is switched off unexpectedly, the filesystem (FAT32) on the card is corrupted.

There is no way to prevent unexpected power outages or user switching it off like that, and the device should be relatively maintenance-free. Worse, the data is written continuously, so the corruptions are very frequent, and Linux upon detecting faulty FS remounts it read-only silently.

What methods would you suggest to mitigate this? Will running fsck.vfat automatically on startup suffice?

Some more info:

  • The card is not to be considered removable by user. It's to be thought of as internal disk. Any data stored on it will be accessible for download over the network or to a usb drive, and the system automatically purges oldest entries. That means it does not need to be readable in your average PC.
  • The system currently supports FAT, yaffs and jffs2. Adding other filesystems to the kernel is possible but if other avenues exist, we'd prefer them first.
  • Writing can be suspended on demand even for several minutes without data loss.
  • partial data loss or minor corruption is acceptable. Complete stopping of logging is not.
  • the poweroff events are completely unpredictable most of the time.
  • the system is running on ARM9, 200MHZ, 64MB RAM, 32MB internal flash and uses up most of CPU power for its primary role. Take this into consideration while thinking of fancy resource-heavy solutions.

Best Answer

You could use the block2mtd driver to use the transactional jffs2 or yaffs(2) filesystems you seem to be employing elsewhere for your SD card, which would solve your problem of data loss or filesystem corruption at poweroff.

Doing so might incur other problems, though. As the SD card is likely to have own mechanisms for wear levelling and sector remapping in place, these could interfere with jffs2's and yaffs' implementatons to do the very same, decreasing the lifespan or the performance of your SD card. If this is not an issue, it should be worth trying.