RT Cunningham

Blogging For As Long As I'm Able

Reducing Wear on the System Drive With Linux

Tagged with computers, linux, tmpfs, ram, zram on March 22, 2024

system drive How long your system drive will last depends on two things. First, the kind of drive. The NVMe SSD, like the one inside my laptop computer, is supposed to last for more than 10 years. Second, how much you actively write to it. I’m talking about what you store on it beyond what the operating system itself requires.

I’m focusing on two areas that Linux famously writes to, but has no mechanism in place to clean them up. One is the /tmp directory, and the other is the /home/user/.cache directory.

Temporary File Storage

Linux writes a lot of temporary files. Some of them are system or application caches that are supposed to make applications run faster. While I wouldn’t want to do it with the web browser cache, causing temporary files to be stored in RAM instead of your storage drive will reduce unnecessary wear and take up less space.

If you have at least eight gigabytes of RAM, the /tmp directory can be moved to tmpfs, which is normally limited to half the RAM available. Enter these commands and then reboot:

sudo cp -v /usr/share/systemd/tmp.mount /etc/systemd/system/
sudo systemctl enable tmp.mount

The /home/user/.cache (where user is your username) can’t be moved directly to RAM, but it can be linked symbolicly to a designated cache directory in tmpfs. You can run these commands, and you don’t even have to reboot. In this example, change “username” to the real username, but not “user”.

mkdir -p ~/.local/share/user-tmpfiles.d
echo "D /run/user/1000/cache 0700 username username 1w
L+ /home/username/.cache - - - - /run/user/1000/cache" > ~/.local/share/user-tmpfiles.d/cachetmp.conf
systemctl --user enable systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer
systemctl --user start  systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer

Of course, none of this will work properly if your Linux distribution doesn’t use systemd. This is also a condensed version of the instructions. If you want details, please examine the references below.

Other Ways to Reduce Wear on the System Drive

One way is to force the swapping mechanism to use RAM. See my article on Zram. Another way is to store as few files on your system drive as possible. It requires some work to use external storage automatically, but external storage devices are relatively inexpensive.

My laptop computer is inexpensive, but it has a 512 gigabyte NVMe SSD drive and eight gigabytes of RAM. Linux itself takes up less than 50 gigabytes, even with everything I’ve installed on it. Even though I have lots of storage space, I use very little of it. Music and video files eat up a lot of space, so I store them on external drives.

References

Image by D-Kuru, CC BY-SA 4.0, via Wikimedia Commons

← Previous ArticleNext Article →