Reduce write operations to SD card with Raspbian
After a few SD cards thrown away with failure symptoms, I had a look at what the web has to say about reducing the amount of write operations – because SD cards have their life limited to the amount of write operations.
I’ve compiled in this post a list of tweaks and tips to the Raspberry Pi configuration to help on this issue.
Note: The tips below must not be treated as mandatory nor as a “we all should do this otherwise … ” attitude. Please be very careful and understand what you’re doing, some of these tips might significantly decrease the Raspberry Pi performance and stability.
DISCLAIMER: Use at your own risk, I cannot be held accountable if anything goes wrong.
1 – Use tmpfs
Raspbian is now using SystemD to manage its startup processes, so I’ll only cover that, there are other blog posts (see references) that will help you on a SysVinit system.
Using tmpfs means that you’ll be using RAM instead of the SD card. If this will reduce the amount of writes in the SD card it will also increase the memory usage, take this into account if your Raspberry Pi usage is very intensive in terms of RAM.
Move /tmp to tmpfs
By default Raspbian is already mounting several mount points in tmpfs, /dev/shm
, /run
, /sys/fs/cgroup
, but some applications do use /tmp
to create temporary files to keep their internal state on while running. Files located in /tmp
should be temporary, and not permanent, then this is something that you don’t need to keep between reboots.
systemctl enable tmp.mount
Move /var/log to tmpfs
Another tweak is to move all the log files to tmpfs, that has a huge impact on the writes, however after every reboot you lose log files from basically anything running in your Raspberry Pi. Usually they are not needed, but if needed by any case this can be reverted at any time.
Add this entry in /etc/fstab
tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0
Don’t forget to reboot for the changes to take effect.
2 – fstab and noatime
The noatime option on the fstab entries that use the SD card is essential to reduce the number of writes. Linux updates the last access time when a file is accessed. Although this is already the default in Raspbian for the SD card partition, this option might also be very useful for USB connected SD Cards and NFS/SMB mounts as the network is usually slower and this option improves a bit the network performance.
3 – Turn off swap
Note: Don’t turn swap off unless you know what you’re doing !!! Thinking that you know what you’re doing is not enough!
Swap memory is only used when the RAM is fully used, depending on the application we are giving to the Raspberry Pi this might or might not be a concern. If the RAM usage of your Pi is very intensive and swap is required, don’t turn off swap, that will decrease the performance of your system.
Once the operating system starts to swap, the amount of writes is affected thus affecting the lifetime of your SD card.
To turn off swap use the following command:
sudo systemctl disable dphys-swapfile
This option might not be permanent, if Raspbian continues to create a swap file after being rebooted, there’s always the option to remove the package that manages it.
sudo apt-get remove dphys-swapfile
4 – Move swap to an external device
dphys-swapfile allows you to define where the swapfile is going to be stored. By default it is stored in /var/swap
which takes up 100MB space in your SD Card. However, depending on your usage, 100MB might not be enough, so it is advisable to increase it a bit. But increasing its size will only increase the read/write operations on your SD Card. So the solution can be to move this file somewhere else.
dphys-swapfile uses a configuration file located in /etc/dphy-swapfile and has a few self explainable variables to be set. The ones we are interested in are CONF_SWAPFILE which defined the location of the swapfile and CONF_SWAPSIZE defines its size. So the solution would be to set them like this:
CONF_SWAPFILE=/media/Volume/swapfile CONF_SWAPSIZE=1024
Once this is done, run:
sudo dphys-swapfile setup
And you should set to go with less writes in your SD Card.
Final remarks:
Please use this with caution and be very careful with what you do, as stated before not all options, if any, are suitable to your system.
I’ll try to keep this post updated and will include new information if it becomes available.
References:
http://oils-of-life.com/blog/linux/reduce-writes-to-the-raspberry-pi-sd-card/
https://raspberrypi.stackexchange.com/questions/169/how-can-i-extend-the-life-of-my-sd-card
http://ideaheap.com/2013/07/stopping-sd-card-corruption-on-a-raspberry-pi/
https://raspberrypi.stackexchange.com/questions/70/how-to-set-up-swap-space
Recent Comments