Portage TMPDIR on tmpfs

During, you can build packages in tmpfs (aka 'ram disk' or 'virtual memory file system'), instead of having massive intermediate, temporary files on an HDD. Let RAM replace HDD.

Reduction of compilation time may very well not be in any way significant. The usefulness of TMPDIR on a tmpfs is often discussed, such as on this forum thread.

This page also describes some basic aspects of tmpfs itself.

Kernel config
You need to have a kernel with swap and Virtual memory file system support enabled. Almost all default kernel configurations have these enabled.

fstab
Add the tmpfs entry to ,

Then mount the tmpfs,

Size caveat
The size is very important, and fully explained here.

Tmpfs should be big enough, because once it gets full, emerge fails at that point. Probably you can't resume it, since files in tmpfs may be killed by oom killer, just like processes.

If you don't specify the tmpfs size, it'll be half of your physical RAM. It's merely the limit; only the actually used size occupies the RAM. Tmpfs grows and shrinks dynamically, so to say.

Swap can be used, so tmpfs can be bigger than the physical RAM. However, if the use of tmpfs exceeds the half of physical RAM, it slows down considerably. 

If you have parallel merge, take 1 or 2GB. For single merge most packages suffice with 500MB, but some packages need huge area, for example:
 * : More than 2GB.
 * or : more than 1GB to compile (1.8GB with the USE flag enabled).
 * : assume 10G or so.
 * : More than 2GB.

Per-package choice for tmpfs / HDD
There're two methods to switch between tmpfs and HDD per packages.

By portage configuration
In this method, the packages you don't want to use tmpfs are specified in. This requires portage >= 2.1.9.

First create the following file in which you tell portage where to place the temporary files directory portage instead of default /var/tmp.

Next create the file in which you will list all the packages that need some special environment variable settings:

And you are done! Portage will now use default settings for all packages except for ones listed in /etc/portage/package.env.

(This section is adapted from the blog post by Gentoo developer Jeremy Olexa which is available under CC-BY-SA license.)

Script to use tmpfs temporarily
This method is the opposite. You don't use tmpfs by default, but the script below automatically does all to emerge with tmpfs temporarily. This doesn't need the line in /etc/fstab. It will


 * 1) checks if /var/tmp/portage is already mounted (then quits, because it would confuse the other emerge process)
 * 2) mounts tmpfs to /var/tmp/portage
 * 3) runs emerge
 * 4) unmounts /var/tmp/portage

To use the script, copy it to /usr/local/sbin/tmerge, make it executable using chmod and perform a so that it can be found by the shell. Then, simply use  instead of emerge.