7

I've configured tmp in /etc/fstab like this:

tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 2

The problem is that now /tmp is limited to half of the machine's memory, and when it reaches that limit I'm getting "no space left on device" error.

I'd like to make it "unlimited", i.e. grow to the size of the disk.

2 Answers 2

15

I get the impression you have a few misconceptions regarding tmpfs. You might find it useful to read the kernel documentation on the topic; I’ll attempt to clarify things for you here.

Your question’s title “tmpfs does not overflow to swap” doesn’t seem to reflect the actual contents of your question, but in any case tmpfs does use swap, although arguably it doesn’t overflow to swap. tmpfs is fundamentally a (virtual) memory-based file system; its contents live in memory only, but since they’re swappable the kernel can store them in swap instead of physical memory if necessary. Nevertheless tmpfs file systems can’t be larger than the total amount of virtual memory available, i.e. physical RAM and swap, as indicated e.g. by free -h.

By default tmpfs file systems have a maximum size equal to half the amount of physical memory available. You can increase this using the size parameter, but again it can’t ever be more than the available physical memory and swap (although that limit isn’t enforced at mount time). Once the file system reaches its maximum size (or rather, contains files occupying that much space), it reports that it’s run out of space, as you found out. tmpfs itself doesn’t support overflowing anywhere when it runs out of space.

If you need temporary storage space for large files, you should use /var/tmp rather than /tmp. You really don’t want a very large tmpfs file system, that’s a recipe for disaster when it fills up (the kernel’s usual ways of recovering memory don’t work in a tmpfs).

(If you have lots of RAM of course, a large tmpfs can work. I run a few systems with build tmpfs file systems sized at 75% of RAM, out of 32GiB, 64GiB or even more.)

3
  • Isn't there any mechanism for adding a secondary tmp? For example, having a main tmpfs for /tmp, and then another /tmp2, in a conventional disk, that might be used only if /tmp is full.
    – jesjimher
    Jan 30, 2019 at 12:22
  • @jesjimher I’m not aware of anything like that. Jan 31, 2019 at 7:53
  • 1
    @jesjimher you can set tmpfs size to larger than the actual RAM in your system and provide enough swap space to handle the usage as needed. If you want temporary setup only you can do something like as root dd if=/dev/zero of=/swapfile42 bs=1m count=8192 && chmod 600 /swapfile42 && mkswap /swapfile42 && swapon /swapfile42. If some process now starts to write to tmpfs the system RAM is first used as much as possible but when you run out of RAM, the rest will be written to swap. For permanent setup, you can define swap in /etc/fstab. May 11, 2020 at 7:58
0

You could use something like aufs. It let's you "merge" two different mounts inside a single directory. So, in this case you could mount both a tmpfs volume somewhere and a conventional directory on disk, and then unify them using aufs as /tmp. You can even assign priorities, so tmpfs would be used first, and disk would only be used when tmpfs runs out of space.

It's not a perfect solution, though, because aufs works as a per file basis. So, if some process creates a file that starts growing slowly beyond tmpfs size, it won't "switch" volumes when tmpfs is full. It'll just get out of space (or some generic I/O error, since technically there's still space left in /tmp). But in a lot of scenarios it might do the trick of having /tmp in memory, and just fall back to disk when it grows too much.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .