Kernel/Building from userspace

This article discusses an alternative to the normal Gentoo way of building your linux kernel from root. It allows you to manage several kernels at once; it even lets you manage kernels for different machines and architectures.

Reasons you may want to do this include:
 * You dislike doing kernel configuration and build from root for security and safety. If you build as a user, you can't damage your system - at least, not 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 /lib/modules.
 * The kernel developers consider building as root to be not advised; one once joked it would be worth including "rm -r /" in the kernel scripts to teach people not to do it.
 * The normal Gentoo way to build kernels is to make your configuration in /usr/src/linux, which means you can only work on one configuration at a time, unless you copy the underlying /usr/src/linux-v-r-m-gentoo directory tree, which is a ridiculous thing to do.

Working as described in the following article, you have 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 .config file, and can have a set of Environment variables to tailor how the kernel is built. For example:
 * you might have a directory on a powerful desktop machine to build the kernel for a feeble laptop
 * if you set up the necessary cross-development environment, you could have a directory for building the kernel for a single-board micro such as the Raspberry Pi
 * you might set up a directory for building a compiler using Clang LLVM and LTO.

The approach described uses documented facilities within the linux kernel build system. You need to setup a shell script and store it somewhere convenient. I use /usr/local/bin/kmake:

You use this script "kmake" whenever you would previously have used the "make" command in the kernel source directory. The parameter "-C" tells make where to find the kernel source, and the "O=" parameter says to build in the current working directory. The rest of the script exports environment variables set in the kmake.env script

For each kernel configuration you want, make a directory, and if necessary create a "kmake.env" script within that directory containing Enviroment settings for the kernel. For example, to cross-compile for a Raspberry Pi, my kmake.env script reads:

ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu-

whereas to compile using Clang, LLVM and LTO, my kmake.env script reads:

CC="clang" LLVM="1" LLVM_IAS="1"

run kmake clean, and setup your initial configuration .config file. You could start with a default, an old .config from a previous kernel build, or a kernel seed from the Internet. Thus:

mkdir kernel-pi cd kernel-pi echo "ARCH=arm64" > kmake.env echo "CROSS_COMPILE=aarch64-unknown-linux-gnu-" >> kmake.env kmake clean kmake default kmake nconfig kmake -j25
 * 1) or maybe "cp /usr/src/linux-2.26.1-gentoo/.config ." followed by "make oldconfig"