3

I'm using 2G of tmpfs for /tmp dir to increase performance for java unit-testing: they are performing IO operations intensively in /tmp.
Here is my tmp section from /etc/fstab file:

tmpfs /tmp tmpfs rw,noatime,size=2G,nodev,mode=1777,noexec,nosuid,rootcontext=system_u:object_r:tmp_t 0 0

Almost always it's working fine, but sometimes I need more than 2G disk space in /tmp. It usually happens when some application downloading updates. In this case performance is not critical for me, so I'd like to write it to physical volume or loopback file.

Is it possible to use tmpfs if /tmp dir size is less than 2G and extend it with other file system when required?

g4s8
  • 478

1 Answers1

4

If you just need to change tmpfs size, you can remount it on-line with new size:

mount -o remount,size=<new size> /tmp

Also, have a sufficiently large swap on your system. When available free RAM is low, memory (including tmpfs) will be paged to swap.

Swap won't be slowing your system, kernel swaps the least used and cached first and the balance can be adjusted with vm.swappiness parameter. For a good explanation for vm.swappiness, see Why is swappiness set to 60 by default?. If the system starts to slow due active pages being swapped, the cause is not swap; the reason is that your system is running out of RAM.

You can increase total swap space by creating a swap file (note: you can't place a swap file on btrfs file system). Create and allocate a new file with dd and set it up as swap area with mkswap. Make sure the swap file is not world readable, you don't want to leak your memory contents.

dd if=/dev/zero of=<swap file> bs=1M count=<size>
chmod 600 <swap file>
mkswap <swap file>

And to enable it:

swapon <swap file>

You can also add the swap file to fstab to make it available automatically.

sebasth
  • 14,872
  • Thanks for anwer, but it doesn't satisfy me, because it makes all other applications slow. I want to control tmpfs size that used by /tmp – g4s8 Sep 23 '17 at 11:08
  • From my long experience with Linux systems, swap unfortunatelly slow down systems in most use cases. – Kepi Nov 23 '21 at 13:25