I work on Raspberry Pi 3b+, I want to industrial a Raspberry (don't judge me, it's my customer).

I use a SD card who contains two partitions (/boot and /).

My greatest fear is when there are a power failure who corrupts my SD card.

For solve this problem, my SD card is on read only.

My question

I know how to protect my SD card (...I hope) from power failures.

But, I don't understand why SD card is corrupted after power failures.

Can you explain to me the reasons please ?


Some notes to be aware of:

Write protecting the SD card may cause the entire system to simply not start up. Some files are temporary/dynamic, and need to be written to while the OS (or programs in the OS) is running. There are solutions, of course, such as creating a fake disk drive in RAM to hold these temporary files (being temporary files, there is no problem if the power failure caused the RAM to disappear). Modern Linux kernels do this or similar already; The /run and /sys directories are created in RAM, and not actually written to any physical drive. Others, such as some of the files/directories in /dev, are created as needed.

As for how and why corruption happens.. Understand that reading/writing to he SD card is very slow compared to the speed of the CPU and RAM. When the CPU is executing billions of instructions per second, and the RAM is accessing memory locations in a similar manner, all is well. But what happens when you need to read or write from/to the SD card? It can only read/write at maybe a few thousand memory locations per second.

You do not want the OS to just stop everything to wai for the read/write to complete.. Imagine if you loaded a web page that took a full minute to load. But while it was loading, you had to wait until it was done! You could not drink your coffee, you could not switch to another web page, you could no even tell it to stop loading because you are impatient.

So what the OS does is cache certain data in the RAM so it does not have to constantly wait to read it again from SD. It does it when it writes to SD, as well: Instead of actually writing to the SD card, it saves the data to RAM and schedules it to be actually written to SD at a later time, when the CPU/OS is not so busy, and goes back to doing other things, such as responding to your wishes.

The corruption happens when this data is only in RAM, but the power fails. No power to keep the RAM fresh, so it cannot write the cached data to the SD card. It's gone. So the data on the SD card is now outdated--I does not have the data that was in RAM. Sometimes you are lucky and it does not matter; the lost data is trivial and can be created again. Other times, the lost data is critical data, and corruption results.

Even a small change can cause a big problem. If a file got only one byte larger, the OS has to update the disk data: Both the one byte added to the file, as well as the data on the disk that tells the OS how big the file is. I has to update that to know the file is now 1 byte larger. Again, however, that information is cached in RAM and scheduled to be written to the SD card later.

If a power failure happens now before it writes the new size, the SD card will "think" the file is still the old size. That new bye is completely lost--corruption.

Most operating systems take steps to try to minimize the risk, but it cannot be completely eliminated. Even making the SD card write protected is not perfect; The SD card degrades over time until it can no longer maintain the data stored on it.

An EMP (Electromagnetic pulse) is similar--it causes electrical devices to short and burn out. So your SD card may still be wiped out if it suffered a strong enough EMP. And so on.

  • Wow thank you for this very clear informations ! I understand now \o/ – Julien Apr 22 at 14:18

This is not limited to SD cards.

Linux, like all general-purpose operating systems, uses a file system buffer cache. When a process writes a file, the modified file data and metadata is stored in the buffer cache. A power failure while writing metadata to the storage medium may have the effect that metadata is only written partially. As a result, the filesystem structures on the SD card are inconsistent. This is often named a "corrupted" filesystem.

  • That said, everyone who is in the business of writing operating systems and/or designing hardware is aware of this potential issue and various pretty clever measures have been undertaken to minimize -- although not eliminate, because that's not possible -- the risk. – Shadur Apr 22 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.