Minimize/reduce/stop corruption on Raspberry Pi SD card

If you have spent any time with a Raspberry Pi you have undoubtedly had at least 1 SD card get corrupted on you. SD cards are susceptible to write fatigue, so running an operating system from one, even a light weight one designed for the RPi, can damage the card relatively quickly.

One of the biggest reasons for disk, or in this case card, activity is logging. Unless you are trying to troubleshoot something specifically you probably won’t spend much time looking at /var/log. This location and /var/run, where lock file, PID files, etc reside are common places for the OS to write data to disk.

One of the greatest strengths of Linux is its flexibility and customization. One of these is the ability to use tmpfs. This is a feature of mounting a location in the filesystem that resides in RAM and so never gets written to the disk. To set this up for the two location mentioned above add the following line in your /etc/fstab file:

none /var/run tmpfs size=1M,noatime 0 0
none /var/log tmpfs size=1M,noatime 0 0

These lines mount /var/log and /var/run using tmpfs at boot time, and give them an upper limit of 1MB. This is a pretty restrictive size, and if you are very active on your RPi, you may try setting /var/log to 2M or 3M. Keep in mind that you are working with a Raspberry Pi and have a limited amount of RAM to work with in the first place. (Although the Raspberry Pi 2 just got announced, and even though I have not been able to get my hands on one yet, the 1GB of RAM on this model should help alleviate the restriction here, with the 1GB you should be able to set this to 5M, which will be plenty of space for logging, and still have plenty available to use elsewhere.)

Another thing to do to help minimize the disk activity is to set /boot to read only. It isn’t very often this needs to get written to, and you can always change it back to “defaults” and reboot if need be. To set this, change the following line in red to the example in green inside /etc/fstab:

/dev/mmcblk0p1 /boot vfat defaults 0 2

/dev/mmcblk0p1 /boot vfat ro,noatime 0 2 

At this point you should already notice less activity with the filesystem light on the RPi, however there is one more item that you can modify to help extend the life of your SD card. You can disable the swapfile. The swapfile is a file on the SD card that is used as memory if you run out of system RAM. Even conventional computers have this feature, and even they have a huge performance hit when they start using swap. With the low specs on the RPi, and the transfer speed limitations of even a class 10 SD card, using the swapfile will bring your RPi to a crawl and render it essentially unusable until you reboot it. With all of this in mind, why even have it, so to permanently disable it run the following commands:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove

After running this you should see something similar to the following when you run: free -m

pi@raspberrypi ~ $ free -m
             total       used       free     shared    buffers     cached
Mem:           438         59        378          0          9         27
-/+ buffers/cache:         22        416
Swap:            0          0          0

 

This should help considerably. Although nothing is going to make an SD card last forever. You could go even farther and once you have your RPi updated, configured, and acting like you need it to, then mount your root ,/, filesystem as readonly. This will make it so nothing writes to the SD card ever. This obviously will come with some drawbacks, like you won’t be able to install any new packages or updates unless you change the setting back and reboot. But in certain situations it could be done and be useful.

Yet another option would be to initiate the boot process from the SD card, then change the root filesystem to be on a USB flash drive (which will have the same drawbacks as the SD card) or a USB external hard drive or SSD. This is a post that I will do later. When I get it done I will come back and link to it here.

 

Tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *