Using tmpfs for /tmp
tmpfs is a special filesystem that does not reside on a physical disk but rather in a portion of your memory. This has the side effect that if you lose power (or reboot) all contents on tmpfs are lost. For /tmp this is not a problem since the Linux Filesystem Hierarchy states that data in /tmp is not expected to survive a reboot. On a side note: Personally I always make sure that /tmp is cleared out on reboot because existing data (locks, etc.) of a previous boot can cause trouble.
Since memory is fairly cheap these days there are a number of advantages to put /tmp on tmpfs:
- Since tmpfs resides in your memory its lightning fast. It has seek times that are incomparable to normal filesystems on physical disks. For example tmpwatch makes use of the atime attribute which is often disabled on normal filesystems for performance reasons. On tmpfs the impact of using atimes is minimal.
- Its automatically cleared at each bootup
2. Practical details
So how does this all work ? tmpfs creates the virtual filesystem in the kernels page cache space. This means that tmpfs, like page cache, is on the bottom of the memory food chain. As soon as applications on the running system fill up all the non-cache memory the cache space will get smaller and smaller until it runs out. When tmpfs doesn't fit in the page cache anymore it is pushed in to swap space. Effectively nullifying its speed benefit, because tmpfs is now sitting on a slow hard drive instead of blazing fast RAM.
You need to make sure your swap space is large enough to handle such an event (or make sure you have plenty of RAM so that it will never be pushed into swap). So basically your swap need to be big enough to handle the normal swap load (as substitute for RAM memory) + the maximum size your tmpfs can be.
3.1. Step 1
Putting /tmp on tmpfs is a fairly trivial operation. Open /etc/fstab with your favorite editor and indicate that you want /tmp to be in tmpfs:
tmpfs /tmp tmpfs size=512m 0 0
The 'size=512m' indicates the maximum size of the filesystem. You will, of course, have to choose an amount which fits your needs / system. 128m will probably do nicely for most people. This amount is not allocated all at once, it is dynamically allocated according to the amount of data on /tmp.
Other flags you might want to supply in the fstab line are:
- noexec - To prevent people from running executables of /tmp. Some rootkits do that. This flag might cause trouble for some legitimate applications so be sure to test everything properly after setting this flag.
- nosuid - To prevent the setuid bit from being set on files in /tmp.
The 'official' tmpfs documentation with the kernel states 'Some people (including me) find it very convenient to mount it e.g. on /tmp and /var/tmp and have a big swap partition'.
I personally wouldn't recommend putting '/var/tmp' on tmpfs. I make this recommendation based on the following reason: The FHS (Filesystem Hierarchy Standard) states that '/var/tmp' is for 'Temporary files preserved between system reboots'. And since the contents of tmpfs do not survive a reboot, putting '/var/tmp' on tmpfs would make your system incompatible with the FHS. Which could cause applications to malfunction or behave unexpectedly.