Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I'm not all that keen to buy a USB hard disk, but I'm aware that SD cards aren't suitable for many repetitions of reading and writing.

Are there any steps I can take to extend the life of my SD card while it's being used by my Raspberry Pi?

share|improve this question
8  
If you write to it at full speed the whole time day and night 24/7- a 16gb will last about 30days. At normal usuage you can expect about 27 years based on 10,000 write cycles. If it uses 100,000 write cycles over 50 years. The bigger the card the longer it will last thanks to clever wear levelling logic –  ppumkin Mar 15 '13 at 8:22
    
Wow that's great to know, I had no idea about your first point of 16gb sd lasting 30 days with writing full speed. I suppose constantly recording video to the sd and then transferring it isn't the best idea. –  SSH This Jun 28 '13 at 16:10
    
With all the performance hits you'll take from these solutions, why not just pay $10 every few years? –  XAleXOwnZX Mar 2 at 16:59
add comment

10 Answers

up vote 125 down vote accepted

These methods should increase the lifespan of the SD card by minimising the number of read/writes in various ways:

Disable Swap

Swapping is the process of using part of the SD card as volatile memory. This will increase the amount of RAM available, but it will result in a high number of read/writes. It is unlikely to increase performance significantly.

Disable swap with the swapoff command:

sudo swapoff --all

You must also prevent it from coming back after a reboot:

  • For Raspbian which uses dphys-swapfile to manage a swap file (instead of a "normal" swap partition) you can simply sudo apt-get remove dphys-swapfile to remove it permanently. Best to remove because setting the CONF_SWAPSIZE to 0, as explained in this answer, doesn't seem to work and still creates a 100MB swap file after reboot.
  • For other distributions that use a swap partition instead of a swap file, remove the appropriate line from /etc/fstab

Disabling Journaling on the Filesystem

Using a journaling filesystem such as ext3 or ext4 WITHOUT a journal is an option to decrease read/writes. The obvious drawback of using a filesystem with journaling disabled is data loss as a result of an ungraceful dismount (i.e. post power failure, kernel lockup, etc.).

You can disable journaling on ext3 by mounting it as ext2.

You can disable journaling on ext4 on an unmounted drive like this:

tune4fs -O ^has_journal /dev/sdaX
e4fsck –f /dev/sdaX
sudo reboot

The noatime Mount Flag

Assign the noatime mount flag to partitions residing on the SD card by adding it to the options section of the partition in /etc/fstab.

Reading accesses to the file system will no longer result in an update to the atime information associated with the file. The importance of the noatime setting is that it eliminates the need by the system to make writes to the file system for files which are simply being read. Since writes can be somewhat expensive as mentioned in previous section, this can result in measurable performance gains. Note that the write time information to a file will continue to be updated anytime the file is written to with this option enabled.

Directories in RAM

Highly used directories such as /var/tmp/ and possibly /var/log can be relocated to RAM in /etc/fstab like this:

tmpfs /var/tmp tmpfs nodev,nosuid,size=50M 0 0

This will allow /var/tmp to use 50MB of RAM as disk space. The only issue with doing this is that any drives mounted in RAM will not persist past a reboot. Thus if you mount /var/log and your system encounters an error that causes it to reboot, you will not be able to find out why.

Directories in external Hard Disk

You can also mount some directories on a persistent USB hard disk. More details of this can be found in this question.

The Raspberry Pi can also boot it's root partition from an external drive. This could be via USB or Ethernet and means that the SD card will only be used to delegate to different device during boot. This requires a bit of kernel hacking to accomplish, as I don't think the default kernel supports USB storage. You can find more information at this question, or this external blog post.

share|improve this answer
8  
noatime should be a default. –  Alex Chamberlain Jun 13 '12 at 8:54
1  
@AlexChamberlain: noatime is not default, at least on Arch. –  Jivings Jun 18 '12 at 11:19
6  
It is in the latest Raspian builds. –  Oli Aug 23 '12 at 23:06
add comment

If the options provided by Jivings aren't possible for your application then another option to extend it's life substantially is to use an SD card which is much bigger than you need.

Leave plenty of free space

Most decent SD cards use wear levelling algorithms to minimise the number of times each block is written, so if the SD card is bigger than you need the wear can be spread over a much larger area of free space.

Part of the reason wear levelling is so important is that some file systems, such as FAT (the default format for many SD cards), hammer the same sectors over and over again.

For more information on this, see the answers to the question Is it true that a SD/MMC Card does wear levelling with its own controller? over on Electronics Stack Exchange, especially this answer.

One interesting statistic from this answer is that

taking a 2GB card and writing it beginning to end over and over again averages about 10TB before the card is dead and no longer is writable.

But the worrying thing is that

SD cards will not let you know when data is bad, i.e. wont return an I/O error like a PC harddrive will.

This may make your choice of file system important if you need to guarantee reliable storage.

One final note: Doubling the size of the SD card could more than double the longevity of it.

I.e. if you have a 2 GB SD card with 200 MB free then switching to a 4 GB card will give you 11 times the free space, wear levelling capacity and thus longevity, while switching to a 16 GB card will give you 71 times the free space.

share|improve this answer
    
Interesting read, thank you. –  Jivings Jun 19 '12 at 11:42
    
To allow the SD card to perform wear levelling in the first place it needs to be told which blocks are actually free to use for that. fstrim could help in doing so. –  Hanno Binder Aug 27 '13 at 15:23
    
Trim isn't required, but without it a block is only reused when the OS reuses a block rather than when it stops using it. As far as I know trim is an SSD function and isn't supported by SD cards which are accessed by a much simpler protocol. –  Mark Booth Aug 27 '13 at 16:07
add comment

The only thing I see is to NOT swap on the SD card.

Swapping on the Sd card is probably what could kill your SD card.

If you need more RAM, you can try to use zram, theres a post on http://raspberry.pi.gw.gd/t50-Using-ZRAM.html giving some details about using ZRAM on the raspberry pi

More info for zram on http://en.wikipedia.org/wiki/ZRam

Also the most recent SD cards are know to be much more solid than older ones, buying a brand new class 10 SD card is probably a good option is you want to see it last a long time.

share|improve this answer
add comment

You could try running Puppy Linux which is completely ram-resident. It's very small and blindingly fast since it runs completely in memory by copying the storage image (on SD card in your case) into RAM at boot and then flushing changes periodically back to storage. The frequency of this save is user controlled including manually.

Puppy uses the layered aufs or older unionfs filesystem underneath any of the standard Linux filesystems like ext3 or ext4. It can also reside on FAT, or NTFS partitions.

There are at least a couple of versions of Puppy specifically designed for the RPi, one of them created by the "Puppy Master", Barry Kauler.

For more, go to http://puppylinux.org/wikka/Puppi

share|improve this answer
add comment

Flash Cell Endurance:

  • For Multi-Level Cell (MLC) Flash, up to 10,000 write cycles per physical sector.
  • For Single-Level Cell (SLC) Flash, up to 100,000 write cycles per physical sector.
  • Newer SSD offer 1 million write cycles per physical sector.

It is purely mathematical on large cards and normal wear. If you were to write to a 8GB MLC type flash card day and night over and over it would take about 30 days to kill it.

Episode 99 at Techsnap talks about wearing out SSD's and Allen explains how it is impossible to wear out a SSD in everyday usage and we do not need to worry about disabling swap, crons and all that. It just works now! The smart wear levelling takes care of everything.

Normal wear as quoted per Kingston should give you 27 years of life for the life in a professionals digital camera.

The price of flash has fallen and the technology is a lot better.

Most SD cards will outlive two or three generations of devices and by that time it will be considered to small and too slow to use with a much better and cheaper upgrade available!

share|improve this answer
    
A Raspberry Pi set up with a webcam for security purposes may well be writing data 24/7. I guess in that case a SSD is probably better. –  Highly Irregular Mar 15 '13 at 8:46
1  
Yes but it is not writing ALL the sectors the WHOLE time sequentially. It is managed for least wear and new data always goes to the least accesses sector/area. The video wont overwrite the file system files or paging files. Besised how many hours can you get on a 16GB flash drive? Usually CCTV should be accessible 15-30days later! Use a 1 TB drive instead or NAS to store for historical purposes. Even short term for storage and transfer you will get years out of a SD card! Better yet use a 32gb SD but cap the folder to 16GB- You will more than double the life time usign that technique! –  ppumkin Mar 15 '13 at 10:10
add comment

Disable Swapfile:

sudo dphys-swapfile swapoff

There is no necessity to uninstall, however if you are not using it, and do want the space, you can safely remove it. Alternate command to remove:

sudo dphys-swapfile uninstall

Usage: /sbin/dphys-swapfile {setup|swapon|swapoff|uninstall}

share|improve this answer
add comment

Note: The 100,000 cycles limit is a hypothesis that applies to every computing device, even the keys in a keyboard.I believe running Pi in proper cooling conditions and proper shutdown/start cycles will give your better results rather than going into analogies.

Also this may augment my above opinion.

Add a new user in addition to user Pi[default]. Point the home directory of the new user in external drive[thumb/hard drive]. Give the new user, super-user permissions and start using it as your primary account.

I hope this helps..

share|improve this answer
    
I always try to type less to save on keyboards ;) –  ppumkin Nov 5 '13 at 15:38
add comment

Just a tiny reduction of write cycles can be reached by streaming the syslog output to another server. Of course, having such a server with a syslogd running is a precondition. However, with the Pi being a toy for Linux enthusiasts this is probably very often the case. :-)

To activate this streaming simply insert a statement like

*.*    @myserver.mydomain

at pretty much the top of the of the file /etc/rsyslog.conf, comment out all the other lines, and restart the logging by issuing service rsyslog restart. After this the messages should be coming in on the selected server.

One clear advantageous side effect of this is that you can easily monitor your Pi in conjunction with other machines on the same server. One downside is that during system startup and shutdown you may loose some messages when the network connection has not been established yet or has already been shut down.

share|improve this answer
add comment

I have compared all solutions the utilitze TMPFS and the bast answer is a synthesis of the script prepare-dirs (see http://grenzdebiel.dyndns.org/wordpress/?p=98) with a proper /etc/defaults/tmpfs ((see http://www.a-netz.de/2013/02/ramdisks-for-the-raspberry/).

The necessary steps to perform on raspbian are:

1. edit /etc/default/tmpfs and set:

RAMLOCK=yes<br>
RAMSHM=yes<br>
RAMTMP=yes<br>

I'd recommend the following sizes:

TMPFS_SIZE=10%VM
RUN_SIZE=10M
LOCK_SIZE=5M
SHM_SIZE=10M
TMP_SIZE=25M

2. enable additional directories using /etc/fstab

>tmpfs   /var/log                tmpfs   size=20M,defaults,noatime,mode=0755 0 0 
>tmpfs   /var/cache/apt/archives tmpfs   size=100M,defaults,noexec,nosuid,nodev,mode=0755 0 0
>tmpfs   /var/spool/cups         tmpfs   size=100M,defaults,noatime,mode=0755 0 0
>tmpfs   /var/spool/cups/tmp     tmpfs   defaults,noatime,mode=0755 0 0

3. use the script /etc/init.d/prepare-dirs to create missing directories in /var/log so that all daemons start

See at the end what it contains in my case.

4. Make the script executable chmod 755 /etc/initd/prepare-dirs.

5. Make sure that the script will be started first on boot before your daemons start: update-rc.d prepare-dirs defaults 01 99

contents of /etc/init.d/prepare-dir:

>#!/bin/bash
>#
>### BEGIN INIT INFO
># Provides:          prepare-dirs
># Default-Start:     2 3 4 5
># Default-Stop:      0 1 6
># Required-Start:
># Required-Stop:
># Short-Description: Create needed directories on /var/log/ for tmpfs at startup
># Description:       Create needed directories on /var/log/ for tmpfs at startup
>### END INIT INFO
># needed Dirs
>DIR[0]=/var/log/lighttpd
>DIR[1]=/var/log/cups
>DIR[2]=/var/log/apt
>DIR[3]=/var/log/ConsoleKit
>DIR[4]=/var/log/fsck
>DIR[5]=/var/log/news
>DIR[6]=/var/log/ntpstats
>DIR[7]=/var/log/samba
>DIR[8]=/var/log/lastlog
>DIR[9]=/var/log/exim
>DIR[10]=/var/log/watchdog
>case "${1:-''}" in
>  start)
>        typeset -i i=0 max=${#DIR[*]}
>        while (( i < max ))
>        do
>                mkdir  ${DIR[$i]}
>                chmod 755 ${DIR[$i]}
>                i=i+1
>        done
>        # set rights
>        chown www-data.www-data ${DIR[0]}
>    ;;
>  stop)
>    ;;
>  restart)
>   ;;
>  reload|force-reload)
>   ;;
>  status)
>   ;;
>  *)
>   echo "Usage: $SELF start"
>   exit 1
>   ;;
>esac

That's it.

share|improve this answer
add comment

Couple of things I did: chmod of the dphys-swapfile (somewhere in /etc - not near the PI at the moment) to: s udo chmod a-x dphys-swapfile I get minor errors on boot (can't start service dphys-swapfile) - Suppose there's a better way... rc-update?? Also, I capture images from the camera module, to eventually put on my webserver (the Pi). I formatted /dev/ram0 to ext2, mounted it as /media/ramdrive (using /etc/init.rc, I think). It's 4megs, big enough for one snap. No writes to SD. The server (oululife.dnsdynamic.com) is experimental, but on the web. to really stress it out, I let it also stream an MP4 episode of 'Heartbeat*'. It runs lighttpd, Mysql, PHP, WordPress, and even when I remotely stream over the web it hardly breaks a sweat, load average about 0,2. No overclocking at all. Model-B rev. 2, up 24/7. So, if I can get my logfiles into the other 15 /dev/ramX, I reckon my micro-sd 16G card will last years....

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.