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,
/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
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
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
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.