Kernel/Building from userspace

This article discusses an alternative to the normal Gentoo way of building the linux kernel from root. It allows managing several kernels at once and managing kernels for different machines and architectures in a uniform way from one non-root user. The approach described uses documented facilities within the linux kernel build system.

Rationale
Reasons to work this way include:
 * you may dislike doing kernel configuration and build from root for security and safety; building as a user prevents damage your system (at least, until you install your new kernel; that must be done as root, or at least a user with write access to wherever you store your kernels and );
 * the kernel developers consider building as root to be not advised (one once joked it would be worth including in the kernel scripts to teach people not to do it); and
 * the normal Gentoo way to build kernels is to make your configuration in, which means you can only work on one configuration at a time, unless you copy the underlying directory tree, which is a ridiculous thing to do.

Working as described in the following article allows one or more kernel build subdirectories of your home directory (or of course of some admin user you set up for the purpose). Each directory has its own configuration stored in its own file and a set of Environment variables to tailor how the kernel is built. For example:
 * a directory on a powerful desktop machine to build the kernel for a feeble laptop;
 * with the necessary cross-development environment in place, a directory for building the kernel for a single-board micro such as the Raspberry Pi; or
 * a directory for building a compiler using Clang LLVM and LTO.

Set up
Create a shell script and store it somewhere convenient, such as :

Use this script instead of the  command when working in the kernel build directory. Specify or update the chosen kernel source directory (default the current target of the symlink) by running  within the kernel build directory.

For each kernel configuration, make a directory, and if necessary create a script within that directory containing environment settings for the kernel. For example:


 * cross-compile for a Raspberry Pi:


 * compile a version 5.12 or later kernel using Clang, LLVM and LTO:


 * a full-rice kernel might have:

Run, and setup an initial configuration file. That be the result of, an old from a previous kernel build, or a kernel seed from the Internet. Thus:

When finished, enter root mode with and enter the commands to install the kernel, modules and build any initramfs as normal, or use  for each command. For example:

Kernel updates
The source from which the kernel is built is determined by the target of the symbolic link, in the usual way. Manage this either through, or by enabling the symlink USE variable in to automatically update the link after installing updated source.

After changing the link target, always to update the configuration, as usual.