Building Gentoo Packages in tmpfs
- posted:
2021-05-10
Gentoo is a Linux distribution that stands out for building packages from source. I really appreciate this approach because it provides me the flexibility to use custom build options [1] and compiler/linker flags [2]. However, source-based builds generates a large number of temporary files, resulting in heavy disk I/O - especially for an SSD, intensive workload like this can significantly shorten its lifespan.
To address this issue, tmpfs - a file system that stores all its files in virtual memory - can be used. The concept is simple: shift all I/O operations from the disk to memory. An additional benefit of this approach is that it significantly improves I/O speed.
While building packages for Gentoo, several directories are involved (check out man 5 make.conf for more details):
$PKGDIR (defaults to "/var/cache/binpkg"): This variable specifies the location for storing binary packages (e.g., xz-utils-5.4.2-1.gpkg.tar), which will be created for all merged packages, if the Portage feature "buildpkg" is enabled.
$DISTDIR (defaults to "/var/cache/distifles"): This variable specifies the location where the source files for each package are stored, which typically are compressed tarball files.(e.g., xz-5.4.2.tar.gz).
$PORTAGE_TMPDIR (defaults to "/var/tmp"): This variable specifies the base directory, along with subdirectories used as temporary working directories for specific tools or tasks. For example:
$PORTAGE_TMPDIR/portage is used as the Portage working directory
$PORTAGE_TMPDIR/genkernel is used as the working directory for the script "genkernel"
$PORTAGE_TMPDIR/ccache is used as the ccache working directory, if the Portage feature "ccache" is enabled.
So, $PORTAGE_TMPDIR/portage (i.e., "/var/tmp/portage") is the directory where temporary files are stored during package building, and it's the exact location that should be mounted with tmpfs:
# ed /etc/fstab a tmpfs /var/tmp/portage tmpfs size=12G,uid=portage,gid=portage,mode=775,rw,nosuid,noatime,nodev 0 0 . wq # mount /var/tmp/portage
Here is an explaination of some of the options:
size=12G: Set the limit of allocated memory size to 12G. The default is half of physical RAM without swap.
uid=portage,gid=portage: assign the directory ownership to the user "portage", as this is the user responsible for building packages.
Thanks for reading :)