Why am I using the buildpkg feature?

Gentoo emerges for Guests (Virtual Machines) take quiet a bit of time on a PC without many resources to share. By using buildpkg feature provided by Portage, Gentoo’s package manager, you can install binary packages compiled on your builder machine on your client.

Since our client machine is a VM that uses the same CPU we do not need to worry about matching CHOST, compatible CFLAGS and CXXFLAGS, or supported processor-specific instruction set features.

If you’re plan is to build binary packages for other clients other than a VM, ensure that:

  • Both systems on the same architechure and have a matching CHOST.
  • CFLAGS and CXXFALGS are compatible with all clients.
  • Processor-sepecific instruction set features (CPU_FLAGS_X86 for amd64 systems), you enable are supported by all clients.

Refer to the Gentoo Binary Package Guide for more information.

Builder machine configuration

Portage Configuration

To enable the buildpkg feature, add buildpkg to the FEATURES variable in make.conf file.

FEATURES = "buildpkg"
EMERGE_DEFAULT_OPTS="--buildpkg-exclude 'acct-*/* sys-kernel*-sources virtual/*'"

In emerge default command line options EMERGE_DEFAULT_OPTS, you can prevent Portage from creating a binary package for some packages or categories by adding the buildpkg-exclude option. You should quote the arguments; otherwise, portage may interpret them as flags and throw an error.

Lighttpd server

A webserver is needed to serve the binary packages to the client. I’ll be using Lighttpd. You can use nginx, apache, or other webservers of your choice.

Your binary packages are stored in $PKDGIR, which is by default set to /var/cache/binpkgs. You can change this default location by setting:

PKGDIR = /your/desired/directory 

Append the following to lighttpd configuration file located at /etc/lighttpd/lighttpd.conf

server.dir-listing = "enable"
server.modules += ( "mod_alias" )
alias.url = ( "/packages" => "/var/cache/binpkgs/" )
dir-listing.activate = "enable" 

You can start the lighttpd server from the command line using

lighttpd -f /etc/lighttpd/lighttpd.conf

Or, you can start the lighttpd service using OpenRC:

rc-service start lighttpd

If you want it to start automatically, add it to the default runlevel:

rc-service add lighttpd

Now your binary packages are ready to be emerged.

Client machine configuration

Portage configuration

Portage keeps binary package repository configuration files inside the /etc/portage/binrepos.conf directory.

Create a file with any name inside binrepos.conf; I’ll create a file named localbinhost.conf. Inside the file, add the following:

[localbinhost]
sync-uri = http://192.168.122.1/packages
priority = 1

You can change the section name [localbinhost]. Priority is set to 1 because I want binary packages in my local repository to take precedence over the Gentoo’s official binary repositories. Replace the sync-uri value with your uri, which in my case is http://192.168.122.1/packages.

If you’re using KVM+QEMU guest, look in the Virtual Networks properties. The CIDR range for my virtual network is 192.168.122.0/24. Guests will be assigned IPs ranging from 192.168.122.2 to 192.168.122.254, and for guests, the host machine is 192.168.122.1.

Finally, your packages can be emerged.

Emerging packages

To emerge a binary package in your Gentoo client, run:

emerge --getbinpkg foo

Or equivalenty:

emerge -g bar

The -g/--getbinpkg option tells portage to download the binary package from the binary package host. If the package is not found do a regular (source-based) emerge.

For additional options, see the Gentoo Binary Package Guide.