User:Steffo/CustomKernel

From ArchWiki

This article is an introduction to building custom kernels from kernel.org sources.

This method of compiling kernels is the traditional method common to all distributions. It can be, depending on your background, more complicated than using the Kernel/Arch build system.

Consider the Arch build system tools, as they are developed and maintained to make repeatable compilation tasks efficient and safe.

Prerequisites

Install the core packages

Comment: Are these the correct dependencies?

Install the base-devel meta package, which pulls in necessary packages such as make and gcc.

It is also recommended to install the following packages, as listed in the default Arch kernel PKGBUILD:

Create the working directory

It is recommended to create a separate build directory for your kernel(s).

In this article, the directory ~/kernelbuild will be used, and it will be assumed that commands are being run inside it:

$ mkdir ~/kernelbuild
$ cd ~/kernelbuild

Download sources

Kernel sources are distributed by the kernel.org website.

Multiple download options for various kernel versions are provided:

Determine the kernel version you want to build and the download method you want to use, then proceed to the appropriate step.

Warning: systemd, which Arch Linux uses, has minimum kernel version requirements, specified at /usr/share/doc/systemd/README. Make sure that the kernel supports the necessary features!
Comment: Which features are required by Arch Linux?

Download a source tarball via HTTP

The kernel sources for a given kernel version A.B.C can be downloaded as a tarball, a compressed file archive.

With a graphical Internet browser

From a graphical Internet browser, you can download the tarball for a chosen kernel version by right-clicking the [tarball] link and then selecting Save Link As....

Additionally, download the signature for the tarball, located at the [pgp] link, so that you'll be able to #Verify the tarball.

With wget
Note: For certain releases (e.g. -rc releases), the downloaded archive and resulting directories will not strictly follow the A.B.C naming used in the examples of this page; adjust them to your needs.

While inside your chosen build directory, and with wget installed, run:

$ wget https://cdn.kernel.org/pub/linux/kernel/vA.x/linux-A.B.C.tar.xz

Additionally, download the signature, so that you'll be able to verify the tarball:

$ wget https://cdn.kernel.org/pub/linux/kernel/vA.x/linux-A.B.C.tar.sign
Verify the tarball

It is recommended to verify the integrity of the download with GnuPG before proceeding with compilation.

Use the signature you previously downloaded to retrieve the fingerprint of the signing key:

$ gpg --list-packets linux-A.B.C.tar.sign

Then, use the fingerprint to obtain the actual signing key:

$ gpg --recv-keys FINGERPRINT-FROM-PREVIOUS-STEP

Since the signature was generated for the uncompressed .tar archive, and not the compressed .tar.xz file that you have downloaded, you need to decompress the latter without extracting it.

Ensure that you have xz installed, then use the following command to perform the decompression:

$ unxz linux-A.B.C.tar.xz

Now, perform the signature verification:

$ gpg --verify linux-A.B.C.tar.sign linux-A.B.C.tar

If the verification succeeds, the output will be the string Good signature; you may then go on to #Unpack the tarball; otherwise, do not proceed, as it means that the sources have been tampered.

Unpack the tarball

Within the build directory, extract the verified kernel tarball:

$ tar --extract --file linux-A.B.C.tar

To be absolutely sure that no permission errors occur, chown should be run to transfer ownership of the folder to the current user.

To transfer ownership of a folder with every file in it to our user, run the chown command.

$ chown -R $USER:$USER linux-A.B.C 

This will transfer ownership of every file in the folder to you, so you do not encounter any errors related to permissions.

Now, enter the sources directory:

$ cd linux-A.B.C

To finalise the preparation, ensure that the kernel tree is absolutely clean by running the distclean make target:

$ make distclean
Tip: The distclean Make target depends on the mrproper target, which in turn depends on clean, and thus, it is not necessary to execute the latter two. See [1] for reference.
Comment: Are those checks really needed?

Download sources via Rsync

Comment: To do.

Directly clone the Git repository

The full Git repository can be cloned directly from the website with the git command:

$ git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Sources will be cloned to the ./linux directory; enter it:

$ cd linux

The directory will be at the master development branch; to move to a different one, use the git checkout command with a valid branch or tag name:

$ git checkout REF

Clone the Git repository via bundle files

The full Git repository can also be cloned via bundle files (see https://www.kernel.org/cloning-linux-from-a-bundle.html).

While inside your chosen build directory, and with wget installed, run the following command to download the Git bundle, even if the download was previously interrupted:

$ wget --continue https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/clone.bundle

Use git to unpack the bundle into a repository:

$ git clone clone.bundle linux

Sources will be cloned to the ./linux directory; enter it:

$ cd linux

The cloned repository will be using the clone.bundle file as remote; to make it track the live repository, run:

$ git remote set-url origin https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Since bundles are updated weekly, you might be missing some commits; download them with:

$ git pull

Now, the directory will contain the latest commit of the master development branch; to move to a different one, use the git checkout command with a valid branch or tag name:

$ git checkout REF

Kernel configuration

The Linux kernel compilation must be configured to include or exclude specific Kernel modules via its .config file.

You can do a mixture of various things:

Copy the default Arch configuration

This method will create a .config file for the custom kernel using the default Arch kernel settings.

If a stock Arch kernel is running, you can use the following command inside the custom kernel source directory:

$ zcat /proc/config.gz > .config

Otherwise, the default configuration can be found online in the official Arch Linux kernel package.

Tip: If you are upgrading kernels, some options may have changed or been removed. In this case, when running make under #Compilation, you will be asked to provide answers to every configuration option that has changed between versions. To accept the defaults without being prompted, run make olddefconfig.

Limit compilation to the modules currently in use

modprobed-db can be used to strip unneeded modules from a given .config.

Once a properly populated database is obtained, remove all the modules not present in the modprobed.db database by running:

$ make LSMOD=$HOME/.config/modprobed.db localmodconfig
Comment: I think that alternatively scripts/kconfig/streamline_config.pl might be used...?

Manually configure the kernel options

There are several tools available to fine-tune the kernel configuration, which provide an alternative to otherwise spending hours manually configuring each and every one of the options available during compilation.

Those tools are:

  • make menuconfig: Command-line interface based on ncurses (superseded by nconfig)
  • make nconfig: Newer command-line interface still based on ncurses
  • make xconfig: Graphical interface based on packagekit-qt5 (recommended, especially for less experienced users, as it is easier to navigate and information about each option is displayed)
  • make gconfig: Graphical interface configuration similar to xconfig but based on gtk2, glib2 and libgladeAUR.

The chosen method should be run inside the kernel source directory, and all will either create a new .config file, or overwrite an existing one where present. All optional configurations will be automatically enabled, although any newer configuration options (i.e. with an older kernel .config) may not be automatically selected.

There are three possible configuration options for each kernel option:

  • y for enabled (the module is built in the kernel)
  • m for enabled as kernel module (the module is loaded only when necessary).
  • n for disabled (the module is unavailable).
Tip: Unless you want to see a lot of extra messages when booting and shutting down with the custom kernel, it is a good idea to deactivate the relevant debugging options.

Once the changes have been made, save the .config file. It is a good idea to make a backup copy outside the source directory to prevent it from getting accidentaly overwritten.

You may need to do this multiple times before you get all the options right. If unsure, only change a few options between compilations.

Comment: Is there a list of modules required by Arch other than the Systemd dependencies?
Tip: If you cannot boot your newly built kernel, ensure all options necessary for Arch to run are enabled. Arch notably requires cgroups support, as it is necessary for Systemd to run. Also see [2], Gentoo:Kernel/Gentoo Kernel Configuration Guide and Gentoo:Intel#Kernel or Gentoo:Ryzen#Kernel for Intel or AMD Ryzen processors

Giving a name to the custom kernel

To prevent overwriting one of your existing kernels by mistake, it is recommended to give the custom kernel a name by manually setting the CONFIG_LOCALVERSION option to an unique string:

edit .config
CONFIG_LOCALVERSION="-YOURKERNELNAME"

This will be displayed when uname --kernel-name will be run on your custom kernel.

Compilation

Once the .config file has been set for the custom kernel, launch the all Make target to start the compilation:

$ make all

Compilation time will vary from as little as fifteen minutes to over an hour, depending on your kernel configuration and processor capability.

Tip: To compile faster, make can be run with the -jX argument, where X is an integer number of parallel jobs. The best results are often achieved using the number of CPU cores in the machine; for example, with a 2-core processor run make -j2. See Makepkg#Improving build times for more information.

Compile the documentation

You can additionally compile the documentation with the htmldocs target:

$ make htmldocs

Installation

The installation of a new Linux kernel on Arch Linux consists of three steps:

  1. #Installing modules
  2. #Installing the kernel

Installing modules

Set the INSTALL_MOD_PATH environment variable to /usr, so that modules are installed in the correct directory:

# export INSTALL_MOD_PATH="/usr"

Also set INSTALL_MOD_STRIP to 1, so that modules are stripped before being installed:

# export INSTALL_MOD_STRIP=1

Then, copy the compiled modules into /usr/lib/modules/A.B.C-LOCALVERSION/ with the modules_install target:

# make modules_install
Note: If your system requires modules which are not distributed with the regular Linux kernel, you need to compile them for your custom kernel when it is finished. Such modules are typically those which you explicitly installed separately for your running system. See NVIDIA#Custom kernel for an example.

Installing the kernel

Systemd expects the kernel to be available in the /usr/lib/modules/A.B.C-LOCALVERSION/ directory at the filename vmlinuz.

Determine the filename of the kernel image that was compiled with the image_name target:

$ make image_name

Then, copy it to the expected directory:

# cp IMAGENAME /usr/lib/modules/A.B.C-LOCALVERSION

Copy the kernel to /boot directory

Note: Ensure that the bzImage kernel file has been copied from the appropriate directory for your system architecture. See below.

The kernel compilation process will generate a compressed bzImage (big zImage) of that kernel, if it does not, you may have to run

make bzImage

This file must be copied to the /boot directory and renamed in the process. Provided the name is prefixed with vmlinuz-, you may name the kernel as you wish. In the examples below, the installed and compiled A.B.C kernel has been copied over and renamed to vmlinuz-linuxAB:

# cp -v arch/x86/boot/bzImage /boot/vmlinuz-linuxAB

Make initial RAM disk

Note: You are free to name the initramfs image file whatever you wish when generating it. However, it is recommended to use the linuxAB convention. This convention will make it easier to maintain multiple kernels, regularly use mkinitcpio, and build third-party modules.
Tip: If you are using the LILO bootloader and it cannot communicate with the kernel device-mapper driver, you have to run modprobe dm-mod first.

If you do not know what making an initial RAM disk is, see Initramfs on Wikipedia and mkinitcpio.

Automated preset method

An existing mkinitcpio preset can be copied and modified so that the custom kernel initramfs images can be generated in the same way as for an official kernel. This is useful where intending to recompile the kernel (e.g. where updated). In the example below, the preset file for the stock Arch kernel will be copied and modified for kernel A.B.C, installed above.

First, copy the existing preset file, renaming it to match the name of the custom kernel specified as a suffix to /boot/vmlinuz- when copying the bzImage:

# cp /etc/mkinitcpio.d/linux.preset /etc/mkinitcpio.d/linuxAB.preset

Second, edit the file and amend for the custom kernel. Note (again) that the ALL_kver= parameter also matches the name of the custom kernel specified when copying the bzImage:

/etc/mkinitcpio.d/linuxAB.preset
...
ALL_kver="/boot/vmlinuz-linuxAB"
...
default_image="/boot/initramfs-linuxAB.img"
...
fallback_image="/boot/initramfs-linuxAB-fallback.img"

Finally, generate the initramfs images for the custom kernel in the same way as for an official kernel:

# mkinitcpio -p linuxAB

Manual method

Rather than use a preset file, mkinitcpio can also be used to generate an initramfs file manually. The syntax of the command is:

# mkinitcpio -k kernel_version -g /boot/initramfs-file_name.img
  • -k (--kernel kernel_version): Specifies the modules to use when generating the initramfs image. The kernel_version name will be the same as the name the modules directory for it, located in /usr/lib/modules/ (alternatively, a path to the kernel image can be used).
  • -g (--generate file_name): Specifies the name of the initramfs file to generate in the /boot directory. Again, using the naming convention mentioned above is recommended.

For example, the command for the A.B.C custom kernel installed above would be:

# mkinitcpio -k A.B.C -g /boot/initramfs-linuxAB.img

Copy System.map

The System.map file is not required for booting Linux. It is a type of "phone directory" list of functions in a particular build of a kernel. The System.map contains a list of kernel symbols (i.e function names, variable names etc) and their corresponding addresses. This "symbol-name to address mapping" is used by:

  • Some processes like klogd, ksymoops, etc.
  • By OOPS handler when information has to be dumped to the screen during a kernel crash (i.e info like in which function it has crashed).
Tip: EFI system partitions are formatted using FAT32, which does not support symlinks.

If your /boot is on a filesystem which supports symlinks (i.e. not FAT32), copy System.map to /boot, appending your kernel's name to the destination file. Then create a symlink from /boot/System.map to point to /boot/System.map-linuxAB:

# cp System.map /boot/System.map-linuxAB
# ln -sf /boot/System.map-linuxAB /boot/System.map

After completing all steps above, you should have the following 3 files and 1 soft symlink in your /boot directory along with any other previously existing files:

  • Kernel: vmlinuz-linuxAB
  • Initramfs: initramfs-linuxAB.img
  • System Map: System.map-linuxAB
  • System Map kernel symlink: System.map (which symlinks to System.map-linuxAB)

Bootloader configuration

Add an entry for your new kernel in your bootloader's configuration file. See Arch boot process#Feature comparison for possible boot loaders, their wiki articles and other information.

Tip: Kernel sources include a script to automate the process for LILO: it can be safely ignored if you are using an other bootloader.

See also