damnsmallportage.sh
by Michael Hampicke
Gentoo Linux
Update 2026-11-18: Brought up to date with current repository paths and repos.conf settings, plus a note on Manifest verification, which has to be turned off for this to work.
The Gentoo ebuild repository is several hundred megabytes in size, so syncing takes forever, especially over a slow connection.
One way around this is a smaller repository. With an rsync exclude list you can tell Portage to sync only the ebuilds of packages that are actually installed on your system. Here is how.
Before you start
This only works with sync-type = rsync, which is still the default for the Gentoo repository. It does not work with git sync.
It also requires turning off Manifest verification. By default Portage checks the whole repository against its signed Manifests after every rsync sync, and a deliberately incomplete tree fails that check. Without verification you lose the protection against a tampered mirror, so only do this if you trust your rsync mirror and the connection to it.
Install gentoolkit
The script uses equery from gentoolkit:
emerge -av app-portage/gentoolkit
Configure the sync
Both settings go into the [gentoo] section of /etc/portage/repos.conf/gentoo.conf. If that file does not exist yet, create it from the Portage defaults:
mkdir -p /etc/portage/repos.conf
cp /usr/share/portage/config/repos.conf /etc/portage/repos.conf/gentoo.conf
Then add these two lines to the [gentoo] section:
[gentoo]
# ... keep the existing settings ...
sync-rsync-extra-opts = --exclude-from=/etc/portage/rsync_excludes --delete-excluded
sync-rsync-verify-metamanifest = no
--delete-excluded makes rsync remove everything that is already on disk but excluded now, so the first sync actually shrinks the tree.
The damnsmallportage script
Next we generate the exclude list. The script asks equery for every installed package and writes rsync rules that keep those packages and their metadata cache entries and drop everything else:
nano /root/damnsmallportage.sh
Copy and paste:
#!/bin/sh
#
# Copyright (c) 2006-2026 by Michael Hampicke
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
set -e
command -v equery >/dev/null || { echo "equery not found, emerge app-portage/gentoolkit" >&2; exit 1; }
pkgs=$(equery -q list -F '$cp' '*' | sort -u)
[ -n "$pkgs" ] || { echo "no installed packages found" >&2; exit 1; }
{
echo "$pkgs" | sed 's|^|+ /|'
echo "- /*-*/*"
echo "- /virtual/*"
echo "$pkgs" | sed 's|.*|+ /metadata/md5-cache/&-[0-9]*|'
echo "- /metadata/md5-cache/*/*"
echo "- /licenses/*"
} > rsync_excludes
Run it:
cd /root
sh ./damnsmallportage.sh
and move the generated list to /etc/portage:
mv rsync_excludes /etc/portage/
Shrink the repository
All that is left is syncing:
emerge --sync
During this sync rsync deletes every ebuild that is not on the list. How small the result gets depends on how many packages you have installed, but on a lean server it is a fraction of the full tree:
du -sh /var/db/repos/gentoo
Known problems
Packages that are not in your tree. If you want to install something that is not installed yet, nano for example, emerge cannot find it:
emerge -av app-editors/nano
...
emerge: there are no ebuilds to satisfy "app-editors/nano".
Add the package and its metadata cache entry at the very top of /etc/portage/rsync_excludes. The position matters: rsync applies the first rule that matches, so these lines have to come before the exclude rules.
+ /app-editors/nano
+ /metadata/md5-cache/app-editors/nano-[0-9]*
Then sync again and install it:
emerge --sync
emerge -av app-editors/nano
New dependencies. The same happens when an update of an installed package pulls in a dependency you do not have yet. Add it the same way.
Once new packages are installed, run the script again and move the fresh list into place. It picks up everything that is installed now, so the manual lines are no longer needed.
Going back to the full tree
If you need the complete repository again, comment out the two lines in /etc/portage/repos.conf/gentoo.conf and run emerge --sync. The next sync brings back the full tree and verifies it again:
[gentoo]
# sync-rsync-extra-opts = --exclude-from=/etc/portage/rsync_excludes --delete-excluded
# sync-rsync-verify-metamanifest = no
Comments
Add a comment