5

I was working on my Pi and accidentally disconnected it from power. I have read that disconnecting power while the SD card is being written to can cause SD card corruption. How often does that really happen?

Aloha
  • 7,078
  • 1
  • 25
  • 52
sir_ian
  • 970
  • 4
  • 14
  • 37

4 Answers4

7

Rarely, but still happens. Besides, there are a lot of factors to consider to guess the chance of SD card corruption including but not limited to:

  • How busy the card was when power got disconnected
    • What the operation was before power was disconnected (read or write)
  • How often a "dirty" shutdown is done
  • Electrostatic discharge (ESD)
  • Whether the SD card is mounted as read-only or writable
  • SD card quality
  • Bad luck or someone wishing bad luck upon you. Pointed out by SlySven
  • Software quality (whether what the software does causes corruption or not)
  • Power noise
  • Voltage to the SD card
  • Clock stability to the SD card
  • Processor and friends' clock speed (GPU, SDRAM, etc.)
    • Overclocks are known to cause corruption
  • Software configuration
  • File system contingency plans (how the file system handles corruption)
  • OS' contingency plans (how the OS handles data corruption)
  • Data redundancy
  • SD card temperature (you should not put a lighter or stove under an SD card)
  • How much stress the SD card is having (physical stress, bending, etc.)
  • SD card connection quality
  • SD card connector quality
  • Normal wear and tear
  • Magnetic fields
    • Most SD cards are immune to this
  • Cosmic rays
  • Environmental radioactivity
    • An SD card is more likely to get corrupted inside a nuclear reactor
  • Acts of God
  • War (regular or nuclear)
  • Apocalyptic scenario
  • Rogue computer virus destroying everything
  • Corrosion from normal wear and tear

My point is that SD card corruption is hard to guess, but using common sense will save you the hassle of recovering corrupted data.

Shut down using the command sudo shutdown -h now and wait for the green LED to blink. That's the "clean" way of shutting down.

Aloha
  • 7,078
  • 1
  • 25
  • 52
  • 1
    "*Whether the SD card is read-only or writable*" - remembering of course that the read-only position detector switch on the card holder in the original A & B modules was not wired up and was therefore ineffective - the "write protect" slider does not work on the standard size SD cards in the RPi - but does of course when you plonk it back into your PC to re-flash it *again*! 8-O – SlySven Feb 28 '16 at 04:14
  • 2
    "War (regular or nuclear)" - the EMP from **nuclear** explosions being particularly *nasty* - and the radiation (alpha, beta, gamma, X-Ray, Neutron) all have bad effects on semiconductor structures - which is a big issue on the ISS where [radiation-hardening](https://en.wikipedia.org/wiki/Radiation_hardening) against Cosmic Rays is a really important issue. – SlySven Feb 28 '16 at 04:31
  • 1
    @SlySven wrapping aluminium foil around both Pi and your own head will also protect against various government conspiracy spy programs. Ridicul aside, having corrupted SD cards [in most of the general consumer Pi usage base] is IMHO a matter of having bad luck rather than bad or neglected software configuration. http://raspberrypi.stackexchange.com/questions/42647/can-pulling-the-power-cause-any-problem-except-with-the-sd-card-contents/42650#42650 – EDP Feb 28 '16 at 06:05
  • @SlySven First comment: I was referring to it being mounted as read-only or read-write. All in all, it's great info. Answer updated :) – Aloha Feb 28 '16 at 07:16
  • Nice summary of causes, but you get a bit distracted near the end. – CousinCocaine Apr 26 '16 at 14:40
5

I think PandaLion98's answer is fairly comprehensive so this is more of an anecdotal observation based on:

  • Having owned at least one pi for more than three years.
  • Having had one pi (first a B, then a B+) on 24/7 for most of that 3 years.
  • Having a few other pi's (another B, and a 2B) that I normally use headless.

In the last category, I'm regularly doing things that may leave a pi in an unresponsive, or at least network disconnected state, and the way I have networking configured once it is down I would have to plug a screen and keyboard in to get it back up again. This is pretty easy since I have them at hand but to be honest: I don't bother unless I am curious about investigating something that happened, which usually I'm not because the information relevant to me is logged anyway.

So I just pull the plug. The number of times that happens is anywhere between several times a day and several times a month. I have no qualms or worries about this because never once in all that time, on any of the pis have I seen any sign of SD card corruption having occurred. There's also occasional power outages here, power bars get accidently turned off or unplugged, etc. There's also one I use with a camera and battery pack outside and I sometimes just let it keep going until it runs of of juice.

That said, there are a few caveats to this anecdote:

  • Generally the pis are not very busy WRT to SD card I/O. I'm not running them as Tor nodes, etc. This is significant because if the power is cut while a lot of I/O is going on, there is more likely to be corruption. However, corruption of that sort, based on my experience with other linux machines over the past decade or so, is almost always fixable by fsck at boot, which unless you've screwed with the configuration will happen in such cases. I have a couple of times seen a (spinning disk) drive left in an unusable state, and in both cases the root problem turned out to be hardware failure. Spinning disks do not last for ever (and neither do SD cards; if you want some of my thoughts on that see here).

  • I tend to use a power supply (2-2.5 amps) that is ample for what I am doing -- wifi dongle, sometimes RF keyboard/mouse combo, maybe an external HD (only works without a hub on the B+ and 2B), maybe a small servo. However, occasionally I've used them with smaller ones, down to 1 and I think even 0.75 A.

  • I don't spend a lot of money on SD cards. Not because I have anything against brand names (I have at least one Sandisk one) but because I've gotten them together in kits that included a power supply and case and those are unbranded. They do usually have class 10 stamped on them. Also there's a near-by electronics store that sometimes has unbranded micro-SDs in a jar on sale at the cash register. Finally, I don't think it matters much on the pi, the controller itself is too slow and the machine itself is just not high performance.

    Note that all this doesn't mean I have a pile of them and are switching them in and out all the time; I don't -- once I have one in the state I want it usually stays in the pi for months, I'm sure there's one that's been there for more than a year running 24/7.

There seems to be an IMO highly unusual degree of paranoia about SD cards amongst pi users. I say that because our use of them is statistically irrelevant -- I like many other people also use them in phones, tablets, and cameras, and have never had an issue there either. What's more, if there were a real problem, that statistical use is so high it would probably make TV network news. I'd think photographers are the ones with the best experience regarding how long an SD card can last and what difference the various qualities of cards really amounts to.

Anyway, I'd put that paranoia down to two causes:

  1. There's this issue that may have affected some people for some period of time, I don't know, but it is in the past now.

  2. People don't rant online about how their equipment is working just fine. An absolutely horrible form of reasoning is to claim that "there must be problem because I've seen hundreds of complaints about this issue online". First, there have been millions (as in 2-4+ by now, I think) of pis sold. Second, a lot of those complaints (which I think realistically would be better measured in the dozens) are obviously by the same irate/upset/frustrated person posting multiple places, or having someone else re-post it for whatever reason. Third, from observing things here over the past 2-3 years, a lot of problems come down to ignorance and incompetence. There is nothing shameful about incompetence, I am incompetent on a unicycle, however, I am sensible enough not to blame it when I fall down.

goldilocks
  • 56,430
  • 17
  • 109
  • 217
  • 1
    Spot on. This rant about SD cards has been going on for about 6 months. My opinion is, that the card you currently have in your Pi, will outlive the next couple of your phones, and if you're not using it related to tor or torrent, it will probably outlive your next car also. – EDP Feb 28 '16 at 10:42
  • Do you *own* a uni-cycle - and are there any photos of you *falling down from one*? P-) – SlySven Feb 28 '16 at 22:20
  • @SlySven Actually no but there is a highwire in the backyard. Unfortunately I cannot put pictures of me or anyone else falling from that on the internet because of the top secret tile mosiac work I am doing in the same yard for an unamed arm of the US DoD (hint: it involves experiments with extraterrestrial communication). – goldilocks Feb 29 '16 at 08:35
  • 1
    Well we have 2 RPi and the cards corrupt in about 6 months at top. Wel not the cards but the file-system. Once we clean the card, run checks on it everything goes well. Put it back into RPi, reinstall and after 4-6 months we have another corruption. They are stored on the shelf, no physical contact. Can't figure out what's going on there... – pagep May 16 '18 at 13:38
2

In my experience over the past few years with 30+ RPi's, having used different power supplies, different brands of SD-cards, different USB-devices connected etcetera:

  • A bad power supply will most definitely cause SD-card corruption issues (highly reproduceable).
  • If you can disconnect the power abruptly while the system is running, and you can do this multiple times without any problem. Then your unique configuration is probably going to stay safe, and you shouldn't worry. However, in certain configurations, the odds on corruption may be as high as 50%, even when the power supply and SD-card is good or even better quality as another one that has been running flawlessly. There are just too many hidden factors to understand why. Just test a few times, and your result will be very consistent to predict future faults (for that specific configuration!). If you are unlucky, either try to change the configuration and test until it does not happen anymore, or accept the possibility of corruption and find workarounds so it is not a problem. This is a matter of hope and pray, you will find it to be very hard to find out why certain configurations work, and others don't.
  • Many times you will not notice a little corruption here and there, but you can verify integrity with checksums (e.g. md5sum), and you might find out it happens more often than you thought, except in many cases it's non-critical to system function.
  • Combination of writing to SD-card, and abruptly disconnecting power, is almost guaranteed to give a little corruption.
  • Eventually, an SD-card will start to fail (broader than just corruption, but corruption may be part of it), but this might only happen after 5+ years.
  • Cheap brands but also established brands may have very crappy products, which already fail within weeks or months. Cheap or expensive, brand or not, does not matter much. The only way to make sure - prior to purchasing, is to check the reviews, and get an SD-card that has been sold a lot. Always check the reviews!
  • Sometimes you may get a bad SD-card that is already a problem case when you bought it, even when others of the exact same model and type are completely fine.
Yeti
  • 121
  • 2
0

How likely is it? I just had one crashed today. I had an RCD failure. Turned the relay on again, failure again after a few seconds. Found disconnected the suspected Christmas light, and turned the relay on again, then it worked.

20 seconds later the power went again. This time my 10 year old son stod with a grin on his face, he had turned the RCD relay off for fun. I gave him a scold that it was not good for the computers.

Now I can also tell him that he broke a RPi installation.

And what did I find. /bin/dash had been replaced by a section of syslog. During startup, there usually is heavy writing to the SD card, and syslog is taking a lot of data.

And do we want to put syslog in ramdisk???

Kjeld Flarup
  • 121
  • 2