Cleaning Up Gentoo Distfiles

calendar

January 18, 2020

categories
Guides
tags
gentoo linux portage

Content:

One of the major benefits Gentoo has over other Linux distributions, is its source-based package system.

While more widespread distros (such as Ubuntu or Fedora) rely on binary packages, packages in Gentoo are usually compiled on the target system. This means compile-time features can be enabled/disabled, and multiple versions of a single package can be installed side-by-side.

The downside to this is that a lot of cached files remain on the system, and are not removed automatically, wasting disk space.

It is fairly straightforward to trim the cache, and potentially reclaim gigabytes space.

Portage Package Cache

When installing a package, Portage (the Gentoo package manager) will first save a copy of the package source. From this, it will then build and install the package. By default, these packages are saved in /var/cache/distfiles, but this directory can be overridden in /var/portage/make.conf. On my system, the cache is stored in /var/portage/distfiles.

All of this cached source code will be kept by Portage, even when the package is no longer installed. If you need to downgrade to a previous version, or rebuild a current version. the source will not need to be downloaded again – both speeding up the process and reducing server load.

The downside to this is that, over time, the system will accumulate a large quantity of old source files, which are no longer needed. While it would be useful to keep copies of 1 or 2 versions of a package, any more than this is a waste of space.

distfiles before
Baobab showing the current size of the distfiles folder on my system.

A quick look at this folder (using sys-apps/baobab, also known as ‘Disk Usage Analyser’) shows that, in the 18 months since my system was installed, almost 20GB worth of package source files have been cached.

Of course, some of this would be retained after the clean, but there is definitely space to be saved.

Installing & Using Eclean

Fortunately, there is a utility available to make the process of cleaning up the Portage cache easy – eclean. Part of Gentoolkit, eclean is specifically designed to clean up unneeded package source files, and binary packages.

It is assumed that all commands below will be executed in a root shell. If not, every command should be prefixed with sudo to run them with root privileges.

You can install Gentoolkit by running…

emerge -a app-portage/gentoolkit

Running eclean on the source files is as simple as running either

eclean distfiles

…or the shorthand version…

eclean-dist

A summary of all of the removed files, their individual sizes, and the overall space saved, will returned. Adding -p (or --pretend) to the end of either of these commands will allow you to see a list of files to be cleaned, without making any changes.

running eclean
The end of my eclean output – over 6GB has been freed.

On my system, this has saved over 6GB (around 1/3) from the distfiles folder, which can be confirmed by taking another look at baobab.

distfiles after
Still a large directory, but a third smaller than it was originally.

By default, eclean will remove the source of any package version that is not installed, and not available from the repositories.

It is possible to save even more space, by running either of

eclean distfiles --deep
eclean-dist --deep

This will additionally remove the source for any package versions available from the repos, but not currently installed, leaving only the sources for installed packages. On my system, a deep clean saved a further 9GB, for a total saving of around 16GB (~85%).

Running Eclean on a Schedule

To maintain the benefits of using eclean, it will need to be ran regularly. It is possible to automate the process, using cron.

At this point, I should stress that it’s generally not recommended to do this, in case packages need to be reinstalled which are no longer available from the Gentoo repositories. In practice, for the average user this will not be a problem.

You will first need to make sure there is a cron daemon installed on the system. To do this, run

emerge -a virtual/cron

At time time of writing, cronie is the Gentoo default.

To learn more about creating a cron job, take a look at our article here.

An example of the command to use in your crontab would be

@weekly eclean-dist --deep

This will run the cleaner once a week.

Portage distfile cache is one of the largest space hogs on a Gentoo system – with this automated cleanup, the cache will be kept to a sensible size.

If you like what we do, consider supporting us on Ko-fi