• Advertisement

Raspberry Pi - Extending the life of SD card with tmpfs

Mini computers based on the ARM architecture. Articles posted in "Vendor - Model - Subject" format.

Raspberry Pi - Extending the life of SD card with tmpfs

Postby lmmtux » Tue Apr 01, 2014 1:41 pm

This article demonstrates how to minimize writes to the SD card and therefore extend the life of it in Linux, by taking advantage of a feature called "tmpfs". Tmpfs is a feature of the Linux kernel that can write files to a virtual filesystem that is stored in memory for any situation where you would rather write to memory than disk. For the Raspberry Pi or any device that uses flash media (SD card, compact flash card, etc.) that is constantly writing to the card, this can minimize the amount of writes to the card and theoretically extend its life considerably.

The newer the SD card, the better, as it will generally be a higher capacity and will spread the writes over a larger surface area and extend the life using a feature called "wear leveling". But, in addition to "wear leveling", simply minimizing the writes to the card should also help.

How does Tmpfs work?

To use Tmpfs, you specify which filesystems to mount in memory. As files are written to these filesystems, they are written to memory. As files are deleted, memory is freed up. So all reads and writes are done directly to memory. Note: Since all files are written to memory (RAM) with Tmpfs, files will be lost at reboot because the memory is cleared. If you desire to keep changes after reboots, the Tmpfs filesystems would have to be written to disk at various intervals to save the data, using a tool like "rsync".

Enabling Tmpfs

To use Tmpfs, add entries to the "/etc/fstab" file and specify which filesystems to mount. An example of common points where frequent writes take place is below. This example applies to the RedSleeve Linux distribution on the Raspberry Pi, which is the equivalent of CentOS or Red Hat Enterprise. Other Linux distributions may have similar areas to below. Verify each one before adding a Tmpfs mount point for each.

Code: Select all
tmpfs                                           /tmp            tmpfs   defaults,noatime,nosuid,size=100m       0 0
tmpfs                                           /var/tmp        tmpfs   defaults,noatime,nosuid,size=100m       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
tmpfs                                           /var/named/chroot/var/run/named tmpfs   defaults,noatime,nosuid,mode=0770,uid=25,gid=25,size=2m 0 0

In this example, I have also inluded a line for Bind (named) to show a more complex situation. The mount options specified are standard, but here's a summary of what they do:

  • "noatime" - will avoid saving the last accessed time for all files, which will save writes.
  • "nosuid" - Ignore set-user-id and set-group-id for security reasons.
  • "mode=" - Set the rwx options
  • "size=" - Specify the maximum size for the virtual filesystem. Note: This is important to avoid something filling up the virtual filesystem and using up all of your RAM.
  • "uid=" and "gid=" - Specify the user and group.

Reboot the computer for the changes to the "/etc/fstab" file to take effect. When the system boots, the entries in "/etc/fstab" are mounted to the points on the SD card before files are written to them. If for some reason these mounts fail to memory, the original filesystem (on the SD card) will be used.
Posts: 55
Joined: Mon Jul 30, 2012 9:40 pm
Reputation: 0

Return to ARM Mini Computers