Preventing (or at least reducing) SD corruption on Raspberry PI

From:
http://www.ideaheap.com/2013/07/stopping-sd-card-corruption-on-a-raspberry-pi/

Read for more info

The biggest offender for Filesystem writes on any linux system is logging. If you are like me, you don’t really look at /var/log after a recycle anyways. This area, and /var/run, a location where lock files, pid files and other “stuff” shows up, are the most common areas for mess-ups. Take a look at your blinking FS light on the board. Our goal is to make that light stay off as long as possible.

Set up tmpfs mounts for worst offenders.

The following two lines should be added to /etc/fstab:

[[code]]czoxMjg6XCJub25lICAgICAgICAvdmFyL3J1biAgICAgICAgdG1wZnMgICBzaXplPTFNLG5vYXRpbWUgICAgICAgICAwMA0Kbm9uZSB7WyYqJl19ICAgICAgIC92YXIvbG9nICAgICAgICB0bXBmcyAgIHNpemU9MU0sbm9hdGltZSAgICAgICAgIDAwDQpcIjt7WyYqJl19[[/code]]
There’s more, however. By default, linux also records when a file was last accessed.
That means that every time you read a file, the SD card is written to. That is no good! Luckily, you can specify the “noatime” option to disable this filesystem feature. I use this flag generously.

Also, for good measure, i set /boot to read-only. There’s really no need to regularly update this, and you can come back here and change it to “defaults” and reboot when you need to do something.

After this, /etc/fstab should look as follows:

[[code]]czozNzk6XCJwcm9jICAgICAgICAgICAgL3Byb2MgICAgICAgICAgICAgICBwcm9jICAgIGRlZmF1bHRzICAgICAgICAgICAgICAgICB7WyYqJl19ICAgMCAgIDANCi9kZXYvbW1jYmxrMHAxICAvYm9vdCAgICAgICAgICAgICAgIHZmYXQgICAgcm8sbm9hdGltZSAgICAgICAgICAgIHtbJiomXX0gICAgICAwICAgMg0KL2Rldi9tbWNibGswcDIgIC8gICAgICAgICAgICAgICAgICAgZXh0NCAgICBkZWZhdWx0cyxub2F0aW1lICAge1smKiZdfSAgICAgICAgIDAgICAxDQpub25lICAgICAgICAvdmFyL3J1biAgICAgICAgdG1wZnMgICBzaXplPTFNLG5vYXRpbWUgICAgICAgICB7WyYqJl19ICAgIDAgICAwDQpub25lICAgICAgICAvdmFyL2xvZyAgICAgICAgdG1wZnMgICBzaXplPTFNLG5vYXRpbWUgICAgICAgICAgICAgMHtbJiomXX0gICAwDQpcIjt7WyYqJl19[[/code]]
Go ahead and reboot now to see things come up. Check the Filesystem light on your raspberry pi after it’s fully booted. You should see no blinking at all.

Disable swapping

One protection against SD card corruption is an optional, but potentially “I’m glad i did that” change to disable swapping.

The raspberry pi uses dphys-swapfile to control swapping. It dynamically creates a swap partition based on the available RAM. This tool needs to be used to turn off swap, and then needs to be removed from startup.

Run the following commands to disable swapping forever on your system:

[[code]]czoxMDA6XCJzdWRvIGRwaHlzLXN3YXBmaWxlIHN3YXBvZmYNCnN1ZG8gZHBoeXMtc3dhcGZpbGUgdW5pbnN0YWxsDQpzdWRvIHVwZGF7WyYqJl19dGUtcmMuZCBkcGh5cy1zd2FwZmlsZSByZW1vdmUNClwiO3tbJiomXX0=[[/code]]
After doing this, call free -m in order to see your memory usage:
[[code]]czoyNjI6XCJwaUByYXNwYmVycnlwaSB+ICQgZnJlZSAtbQ0KICAgICAgICAgICAgIHRvdGFsICAgICAgIHVzZWQgICAgICAgZnJlZSB7WyYqJl19ICAgIHNoYXJlZCAgICBidWZmZXJzICAgICBjYWNoZWQNCk1lbTogICAgICAgICAgIDQzOCAgICAgICAgIDU5ICAgICAgICAzNzggIHtbJiomXX0gICAgICAgIDAgICAgICAgICAgOSAgICAgICAgIDI3DQotLysgYnVmZmVycy9jYWNoZTogICAgICAgICAyMiAgICAgICAgNDE2DQpTe1smKiZdfXdhcDogICAgICAgICAgICAwICAgICAgICAgIDAgICAgICAgICAgMA0KXCI7e1smKiZdfQ==[[/code]]
If you reboot, and run a free -m again, you should still see swap at 0. Now we don’t have to worry about tmpfs filesystems swapping out to hard disk!

From:
http://www.ideaheap.com/2013/07/stopping-sd-card-corruption-on-a-raspberry-pi/

Read for more info