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?
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.
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
fsckat 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:
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.
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.
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.
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???