First appearing as extended storage for PDAs, smartphones and digital cameras around 10 years ago, SD cards are now a popular addition to tablet and mini computers such as the Raspberry Pi. Offering high capacity and fast write speeds, SD cards are particularly important to the Raspberry Pi, which uses this type of storage as a system disk.
If you know anything about SD cards, then you’ll see how this might prove to be a problem. SD cards have a finite life, with limits on how often data can be written and rewritten before the card gives in to entropy.
Given the wide number of projects available to Raspberry Pi users (anything from media centers and retro gaming systems to taking photos from space) it seems sensible to investigate just how SD cards can have their usable lifespan extended.
Make Your SD Card More Reliable
Finding solid evidence of SD card failures is difficult. The format is so cheap that in the majority of cases, the owners might have just discarded them in favour of a replacement. On the other hand, this format has become more resilient in the past few years, thanks to advances in card design (something we’ll return to later).
To enjoy trouble-free SD card-based computing, you should choose the largest card for your budget. The thinking behind this is simple: with a limit on the number of times data can be written to SD cards, and the fact that data written to the device should be spread out into untouched areas before going back to the beginning, there is less change of writing to the same area of the card. Choosing 16 GB over 8 GB will cut by half the number of rewrites. In theory this will double the life expectancy of your storage.
It’s also worth shopping for only the big name brands. Yes, it might be tempting to grab a 32 GB card for $10 on eBay or Amazon, but you’re not guaranteed to get a reliable device – or even a working one. If you had some old SD cards you were hoping use, check out all the other ways they can be used insteadx.
Using Raspberry Pi? Write To RAM, Not The Card
Increasing the lifespan of your SD card is possible by making better use of your device RAM. The following trick can be used on any Linux device, although we’ve tested it on the Raspberry Pi.
Using the tmpfs feature you instruct the device to write to system RAM just like it would to a storage device (you can make a RAMdisk in Windows too, actually). The result is that there is less writing to the SD card. A bonus is that tmpfs is fast and easy to setup.
To use this, open /etc/fstab in the Raspbian file system (using nano in the command line) and add:
tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0
After saving, restart your Raspberry Pi. This will mount the virtual file system, ready for use. Upon rebooting, /var/log will be mounted as a RAM disk; files written to the directory will be in RAM, for as long as they are needed.
Several other locations can also be used:
tmpfs /tmp tmpfs defaults,noatime,nosuid,size=100m 0 0 tmpfs /var/tmp tmpfs defaults,noatime,nosuid,size=30m 0 0 tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0 tmpfs /var/run tmpfs defaults,noatime,nosuid,mode=0755,size=2m 0 0 tmpfs /var/spool/mqueue tmpfs defaults,noatime,nosuid,mode=0700,gid=12,size=30m 0 0
These lines can all be added to /etc/fstab. Note the use of the size= condition, which limits how much space each temporary folder should take up. Remember that the RAM will also be used by the operating system, so setting size limits will avoid Raspbian locking up. Also, take care to only add locations with temporary data to /var/log. These files are all deleted when your Raspberry PI restarts, so anything you need to keep or persist across reboots shouldn’t be stored in RAM.
By moving these locations to RAM, the amount of data written to your Raspberry Pi’s SD card can be reduced, thereby extending its life.
Better Still, Bypass The SD Card Completely!
Most Raspberry Pi users rely on some form of Linux, and as such have in their hands an even better way of both improving performance and reducing the rewrite volume to the SD card.
The solution is by using a USB 2.0 device, which might be a standard thumb drive, perhaps a powered USB HDD or even an SSD. Although the Raspberry Pi is hardwired to boot from the SD card, you can reconfigure the OS so that only the boot partition is found on the SD card, while the root partition is moved to another device.
While you might find the process a little complicated, once the boot partition has been edited to look for the root partition on the USB device, you’ll see that the result is impressive.
Conclusion: SD Cards Are Amazing, So Treat Them Well
The most important thing you can do with SD cards to ensure they last is to buy the named brands and where possible use them for tasks that don’t require constant rewriting (like an always-on torrent downloading megalith).
What is perhaps most fascinating is that SD cards are habitual liars, backed up by some very clever software. As revealed by hardware hacker Bunnie Huang at the Chaos Compute Club Congress, SD cards are made with very cheap, defect-riddled flash memory. Creating an illusion of a robust piece of storage media is sophisticated error correction, meaning that the cards are able to display a completely different capacity to the one actually available (you can check your actual capacity with the H2testw tool).
As for Raspberry Pi owners, take advantage of the tips above concerning buying larger storage and using tmpfs to reduce the volume of rewriting on your SD card.
Treat your SD cards well, and they’ll invariably last!
Explore more about: Raspberry Pi.