Use RAM for /tmp instead of my SSD

I am not sure if I read or if I heard on a podcast or something about using RAM instead of your SSD for /tmp. I searched around a bit and saw this as a partial answer to my question.

In short, I think what I SHOULD be able to do is add this to /etc/fstab:

tmpfs /tmp tmpfs defaults,nosuid,nodev,noexec,noatime,mode=1777 0 0

I have 40 GiB of RAM and unless I am doing a lot with VMs, I barely use it.

My questions are:

  1. Would adding that to /etc/fstab and nothing else force the usage of RAM for my /tmp folder
  2. Should I add a limit to how much it can grow?
  3. Besides setting a drive size limitation, is there any other way to automatically clear out temporary files in a clean and elegant way?

Thanks!

1 Like

According to the Arch Wiki:

It is also used for /tmp by the default systemd setup and does not require an entry in fstab unless a specific configuration is needed.

Source: tmpfs - ArchWiki

So probably you don’t have to do anything if you’re on a system with systemd. You can simply check the status by systemctl status tmp.mount, and double-check the unit file in /usr/lib/systemd/system/tmp.mount.

2 Likes

I had a /tmp issue on Fedora yesterday where imagemagick had been dumping massive files into it (it can do that if it doesn’t exit properly). I was getting /tmp “out of space” errors and thanks to your post now I know where that space comes from. Thank you. That made the foundation of the rest of this post:

Unless you want to throw some BASH together tmpwatch is pretty good as a cron entry or a systemd job. Aside from the Debian universe it has general repo availability incl’ OpenSUSE.

# Delete anything that hasn't been accessed in a day
/usr/bin/tmpwatch --atime 1d /tmp

Just make sure atime isn’t turned off for the mount, otherwise use --mtime.

cat /usr/lib/systemd/system/tmp.mount | grep 'Options='
# Fedora output:
# Options=mode=1777,strictatime,nosuid,nodev,size=50%,nr_inodes=400k

From the above tmp.mount output, it looks like mine is capped at 50% of my RAM: size=50%
If you want a specific size you could change that to something like size=20G

1 Like
> systemctl status tmp.mount
â—Ź tmp.mount - Temporary Directory /tmp
     Loaded: loaded (/proc/self/mountinfo; static)
     Active: active (mounted) since Wed 2021-10-27 09:36:05 EDT; 3 days ago
      Where: /tmp
       What: tmpfs
       Docs: https://systemd.io/TEMPORARY_DIRECTORIES
             man:file-hierarchy(7)
             https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
      Tasks: 0 (limit: 4915)
        CPU: 1ms
     CGroup: /system.slice/tmp.mount

Warning: some journal files were not opened due to insufficient permissions.

So, that doesn’t tell me that it is RAM disk or am I just not understanding what I am reading?

Now I am wondering, since the systemd module is running, is there a systemd tmpwatch implementation @Ulfnic ?

Excerpt from kernel.org’s section on tmpfs.

Everything in tmpfs is temporary in the sense that no files will be created on your hard drive.

It tells you that whatever is described in the tmp.mount file, has been successfully executed.
The lines Where: /tmp and What: tmpfs tell you that at the /tmp directory you have a tmpfs (RAM disc) mounted.

Looks like it has a timer for cleanup running by default: CentOS / RHEL 7 : how tmpfiles clHow systemd-tmpfiles cleans up /tmp/ or /var/tmp (replacement of tmpwatch) in CentOS / RHEL 7ean up /tmp/ or /var/tmp (replacement of tmpwatch) – The Geek Diary

cat /usr/lib/tmpfiles.d/tmp.conf
# Output w/o comments:
q /tmp 1777 root root 10d
q /var/tmp 1777 root root 30d

My /tmp is emptied of files that haven’t been accessed in 10 days and /var/tmp for 30 days.

To run the cleanup service manually:

sudo /usr/bin/systemd-tmpfiles --clean

To see it’s timer settings, service settings and status:

cat /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
cat /usr/lib/systemd/system/systemd-tmpfiles-clean.service
systemctl status systemd-tmpfiles-clean

Very cool. I learned something here. So, it looks like /tmp as well as some others folders are indeed using tmpfs

tmpfs            20G   80M   20G   1% /dev/shm
tmpfs           7.8G  2.4M  7.8G   1% /run
tmpfs            20G   12M   20G   1% /tmp
tmpfs           3.9G   14M  3.9G   1% /run/user/9000

It also looks like it clears out after 10 days

> cat /usr/lib/tmpfiles.d/fs-tmp.conf
q /tmp 1777 root root 10d

I am not so concerned about the time between clearing out, so long as it does happen. It should also be noted, I do reboot this laptop about weekly, after I sudo zypper dup the thing. My biggest concern was that I was making unnecessary writes to the disk causing premature wear on the NVME.

Thanks everybody. I appreciate the input as I learned a thing or three!

1 Like

Same here, thanks Nate

1 Like