Aros
Posts: 37
Joined: Tue Apr 28, 2015 9:08 am

Prevent SD card from being corrupted (improper shutdown)

Mon Aug 03, 2015 3:39 pm

Hi, I use Raspberry as a part of my product. Basically, there is a Raspbian (with most of the stuff turned off - no X, no network and so on...) and it is set to launch my application on startup. Pretty easy so far. It works but there is one thing I am worried about. The device can be switched off anytime. More specifically, the power cord can be unplugged anytime. And I need the device to always work when switched on again. Well it has not happened to me so far, that it would not work, but I think it can happen when writing some data to the SD card, the power may go down and it would corrupt the filesystem on the card.

Well, I could set the SD card as read-only as in viewtopic.php?p=213440. But the thing is that from time to time (not really often), I need to save the application settings to the file.

Do you have any solution or suggestion - what is best in my case? I do not insist on having the partition as read-only. But I need it to boot under any circumstances. I would not even mind losing the settings file (but there is zero probability the power drops while saving the settings file as it has few bytes and the user typically sets the stuff only once). What i am worried about is what the OS itself writes to the SD card (for whatever reason).

Thanks
Aros

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Prevent SD card from being corrupted (improper shutdown)

Mon Aug 03, 2015 4:12 pm

What you could do, and this is what NOOBS does, is make a third partition which will contain your files that you write to, make the root file system read only, so that it will always boot and run your application, and then access your settings partition for the settings.
There are 10 types of people: those who understand binary and those who don't.

henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Re: Prevent SD card from being corrupted (improper shutdown)

Mon Aug 03, 2015 5:01 pm

Hi Arosi,
Nothing is going to be 100% bullet proof. I am also using the pi for a prototype product run. Not sure if you are good with electronics or not but the best solution that I could come up with was to build a circuit board that has a small super capacitor powered UPS on it that supplies power to the pi. The board also has a microchip on it that signals the pi when the main power goes off. The super cap power supply gives about 3 minutes worth of steady 5VDC. The pi shuts down in about 20 seconds after getting the signal thru the GPIO to shutdown so it never loses 5VDC until after the shutdown is complete. Super caps by themselves cannot hold a steady voltage so their charge is fed into one of these:

http://www.ebay.com/itm/New-Mini-5pcs-L ... 43d6263c03

I tested this setup using a PLC that interrupted the 120 volt mains every 20 minutes for weeks. The PLC was programmed to count the interruption cycles. After hundreds of power interruptions the pi has never failed to boot correctly.

Henry

Aros
Posts: 37
Joined: Tue Apr 28, 2015 9:08 am

Re: Prevent SD card from being corrupted (improper shutdown)

Mon Aug 03, 2015 8:06 pm

kusti8: Well, thanks. I actually thought about that, though It didn't strike me i can create another partition on the same SD card. I initially thought of mounting a usb drive. Anyway I did not use NOOBS to install my Raspbian but rather flashed the raspbian image directly on my SD. I will definitely try to create it manually.

Henry: I considered this option as well. The thing is that the super cap is extremely expensive. It has a similar price as the the PI board itself. Or at least those I know of. If you can recommend some super cap that would do the job I would be grateful.

Heater
Posts: 15378
Joined: Tue Jul 17, 2012 3:02 pm

Re: Prevent SD card from being corrupted (improper shutdown)

Mon Aug 03, 2015 9:17 pm

If you want a reliable system then make sure that the media you are booting from is not writeable.

That way it will always start up. Give or take some freak hardware problems.

Presumably this system is connected over the net. So never think about writing files locally, send them out to the server that is watching the system.
Memory in C++ is a leaky abstraction .

jpezz
Posts: 98
Joined: Fri Apr 26, 2013 6:14 pm
Location: North Carolina, U.S.

Re: Prevent SD card from being corrupted (improper shutdown)

Tue Aug 04, 2015 2:39 am

I just force an fsck on startup and have never had a problem with either of my Pis (B+ & 2) booting. They are used for a museum display and are booted by flipping a circuit breaker on which turns on museum lights and powers Pi. Flipping the breaker off at the end of the day turns off lights and Pi. I'd NEVER recommend people use the circuit breaker as a switch but the museum has been doing that for years.

Heater
Posts: 15378
Joined: Tue Jul 17, 2012 3:02 pm

Re: Prevent SD card from being corrupted (improper shutdown)

Tue Aug 04, 2015 7:11 am

jpezz,

Anecdotes are great and you have been very lucky. Perhaps your museum application is never writing to SD, or at least not much. I would not want to rely on a system operated like that.
Memory in C++ is a leaky abstraction .

Aros
Posts: 37
Joined: Tue Apr 28, 2015 9:08 am

Re: Prevent SD card from being corrupted (improper shutdown)

Tue Aug 04, 2015 7:59 am

Well, it seems I made a mistake by extending the root partition to span the whole SD card. If I understand correctly there is no way to shrink it now, so that I can create separate home partition. At least not without any help of another Linux based computer. It was a lot of work to create the SD card image (lot of settings so that it matches demands of my application), and it seems I would have to do it all over again. Am I right?
Perhaps your museum application is never writing to SD, or at least not much.
Just to be sure... My application does not write to SD as well, at least not much. Typically only during the first execution. But it still doesn't mean the filesystem doesn't get corrupted, right? The problem is that the services running in the background might want to write something. Maybe if I remove the services I do not need...
Presumably this system is connected over the net.
No it's not. It's completely offline and often might be used in places that there is basically no way to connect to internet.

Heater
Posts: 15378
Joined: Tue Jul 17, 2012 3:02 pm

Re: Prevent SD card from being corrupted (improper shutdown)

Tue Aug 04, 2015 8:09 am

My plan would be:

1) Make the file system on SD read only. See instructions here: https://wiki.debian.org/ReadonlyRoot You might want to practice than on a fresh SD and installation first.

2) Use a tiny USB memory stick to write any data you need to save to. Perhaps one of those little USB SD card adapters. I have one here that is minute. Perhaps use a DOS file system on that so that it can be easily read from any other computer when need be.
Memory in C++ is a leaky abstraction .

henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Re: Prevent SD card from being corrupted (improper shutdown)

Tue Aug 04, 2015 2:24 pm

The thing is that the super cap is extremely expensive. It has a similar price as the the PI board itself. Or at least those I know of. If you can recommend some super cap that would do the job I would be grateful.
They are very expensive I agree. I am using 2 of these at $13 each
http://www.mouser.com/ProductDetail/Max ... qq4w%3d%3d

These are only a couple of dollars each but I have no experience with them:
http://www.ebay.com/itm/10pcs-Super-Cap ... 5b1229cec6

Wire them in series so that they could be charged to a maximum charge of 5.2 volt. However, if you are not going to use an active charge controller for the bank then only charge them to a maximum of 75%, which works out to be about 3.9 volts total. Super caps are easily destroyed if they are over charged. This is the method that I am using and it is plenty of reserve power to shut down the pi.

One more thing. The microchip on the board does not allow the pi to get power UNTIL the cap bank is at full charge. This assures that enough power exist to fully boot the pi and to fully shut it down again. This does create about a 1 minute delay from first power to actual boot up. Completely booting is as important as a controlled shutdown procedure for SD card data protection.

Aros
Posts: 37
Joined: Tue Apr 28, 2015 9:08 am

Re: Prevent SD card from being corrupted (improper shutdown)

Wed Aug 05, 2015 10:42 am

OK, thanks to both of you. I'll have a look at those tips.

Return to “General discussion”