![]() Markus Wernig UNIX / Network / Security Engineer CCSA, CCSE CISSP PGP Schlüsseltransition GPG Schlüssel (nach 9. Aug. 2013) alter GPG Schlüssel (bis 9. Aug. 2013) |
|
||
A customized binary Gentoo kernel packageAfter having set up a build server/binhost for use with my Gentoo desktop machines, there was one piece of software left that still needed to be compiled on the client machines: the kernel. While Gentoo does have a binary kernel package nowadays, this "distribution kernel" shares one characteristic with the kernels of all other binary GNU/Linux distributions: It is very generic and has almost every possible feature/option enabled. This makes sense, as it must be able to run on any supported hardware, and people should not need to go back to compiling their own kernels as soon as they want to use a more uncommon option or piece of hardware. But that characteristic (or the lack of it, more precisely) is exactly why I am using Gentoo: because it gives me fine-grained control over the software I'm using and over which features this software has and does not have. So using the "distribution kernel" does not do for me, I had to find a different approach. One that would combine the convenience of binary packaging with the type of control that I've grown to appreciate over the years. The same, basically, that I had achieved with the binhost, where all packages are built with the exact flags that I want to use. So I started hacking together a "custom-kernel-bin" ebuild that would allow me to build a custom kernel binary package, based on the gentoo-sources kernel source package, from which I have built every of my kernels in the last 15 years. The following assumes that you have a build environment for binary packages already set up. In my case this build environment (or rather its PKGDIR the binary packages are installed into) also serves as binhost, from where the clients download their binary xpak files, but that is out of scope here and not relevant for the kernel package. Important notes
/etc/portage/make.confI use the following build flags, among others:
CBUILD="x86_64-pc-linux-gnu"
CHOST="x86_64-pc-linux-gnu"
ACCEPT_KEYWORDS="amd64"
PORTDIR_OVERLAY=/usr/local/portage
CFLAGS="${COMMON_FLAGS} -march=x86-64 -mtune=generic --param l1-cache-size=32 --param l1-cache-line-size=64
--param l2-cache-size=8192"
FEATURES="-sandbox -preserve-libs -ccache userpriv buildpkg binpkg-multi-instance"
EMERGE_DEFAULT_OPTS="--backtrack=200 --with-bdeps=y --keep-going=y --ask-enter-invalid"
Some of these may need to be adjusted for other environments. Note that this particular buildhost is set up to also use the same files locally that it installs as binpkg into $PKGDIR. This may not be possible with every CPU combination. /usr/local/portageThis is where the ebuild is located (if PORTDIR_OVERLAY points somewhere else on your
buildhost, use that instead). The following shows two ebuilds, one for kernel 5.5.9, one for 5.6.7. buildsrv # ls -R1 /usr/local/portage/ /usr/local/portage/: sys-kernel /usr/local/portage/sys-kernel: custom-kernel-bin /usr/local/portage/sys-kernel/custom-kernel-bin: custom-kernel-bin-5.5.9.ebuild custom-kernel-bin-5.6.7.ebuild files metadata.xml Manifest /usr/local/portage/sys-kernel/custom-kernel-bin/files: config-5.5.9-gentoo config-5.6.7-gentoo There are basically four files here that we need, one of which is optional.
Build the packageRun the following command: emerge --buildpkgonly custom-kernel-binThis will compile the kernel and create the package file in /var/cache/binpkgs/sys-kernel/custom-kernel-bin/custom-kernel-bin-$version-1.xpak(or wherever your $PKGDIR points to). Install the packageThere is more than one way to do this. My method uses the binhost (where the above command installed the package into $PKGDIR, which serves as my binhost) The target machine (client) needs to have the same ebuild and Manifest files that the package was built with.
Either set up a repository from which it can be downloaded with mkdir /usr/local/portage/sys-kernel/ scp -r buildhost:/usr/local/portage/sys-kernel/custom-kernel-bin /usr/local/portage/sys-kernel/Also put this into /etc/portage/make.conf:(all the same USE flags etc. as on the buildhost) PORTDIR_OVERLAY=/usr/local/portage PORTAGE_BINHOST="https://$binhost/path/to/$PKGDIR" FEATURES="-sandbox -ccache -buildpkg preserve-libs getbinpkg" If everything is set up correctly, emerge should find the new package emerge -s custom-kernel-bin
* sys-kernel/custom-kernel-bin
Latest version available: 5.6.7
Latest version installed: [ Not Installed ]
Size of files: 0 KiB
Homepage: https://www.kernel.org
Description: Custom-built amd4 kernel binpkg from gentoo-sources
License: GPL-2
Now install the package with emerge -K custom-kernel-bin.The "-K" makes sure it will rather fail than try to build the package if the package is not installed eg. because of some flag mismatch. Note for initramfs:If you want dracut to build an initramfs after installation, you need to set the sys-kernel/custom-kernel-bin initramfsThis will install dracut and its dependencies before installing custom-kernel-bin. If you are using a binhost,
make sure the dracut package (and its dependencies) are available there.
If you want to be able to install the same custom binary kernel on multiple targets with and without initrd, you need to build the kernel package twice, one time with, one time without the initramfs USE flag. Make sure that the binpkg-multi-instance flag is set in FEATURES, else the two package versions will overwrite each other.You can customize dracut's operation locally in /etc/dracut.conf. (This might be necessary if you need to
modify the modules that dracut puts into the initramfs.)
|