2.8 Customize your kernel with cl-kernel

Introduction

Calculate Linux uses LTS kernels. Most drivers are built as modules, thus making the kernel smaller without sacrificing functionality. Different options and patches are applied in desktop and server versions. Unlike the equivalents provided by Portage, the default Calculate Linux kernel, sys-kernel/calculate-sources, is installed like a regular package. Once compilation is complete, the source code is usually removed.

You can modify your kernel for optimization purposes, if you want to make it more resource-efficient, add support of a specific hardware, reduce memory usage or power consumption, or simply for faster booting. It is also an excellent opportunity to better understand how a Linux system works.

cl-kernel

The cl-kernel script, provided by sys-apps/calculate-toolkit, compiles the kernel. It is written in Bash and transparently integrated with Calculate templates.

Features

cl-kernel:

  1. supports all kernels, such as sys-kernel/calculate-sources, sys-kernel/gentoo-sources, sys-kernel/vanilla-sources, and others;
  2. lets you build a kernel with or without initramfs;
  3. generates a configuration template based on your custom kernel, to be used later on;
  4. imports preconfigured options;
  5. offers choice between multiple kernel versions while the existing configuration remains untouched;
  6. as part of Calculate Utilities, validates the templates that were created for calculate-sources at installation time;
  7. backups the current configuration;
  8. is available in English, French, and Russian.

The sources will be unpacked to /usr/src. Make sure your have enough disk space before continuing:

df -h

Remember to always have a backup in case of failure. You should only experiment on alternative kernels to avoid ending up with an unbootable system, or on a non-installed version of calculate-sources.

Kernel compilation

Select a kernel among those available in the Portage tree. You can view the full list by typing:

eix -c sys-kernel/*sources

First let us try the so-called "vanilla" kernel, supported by Linus Torvalds himself.

In Portage, the package is marked as masked. Unmask it before doing anyting else:

echo sys-kernel/vanilla-sources ~amd64 ~x86 >> /etc/portage/package.keywords/custom
USE="symlink" emerge sys-kernel/vanilla-sources

Enable the "symlink" USE flag if you intend to use proprietary drivers, namely nvidia-drivers, ati-drivers, virtualbox-bin or else broadcom-sta. Once the kernel is ready, recompile the modules by running:

emerge @module-rebuild

Now you can check it for availability:

cl-kernel --kver list
 * 3.19.0 *
 * 3.18.7-calculate

Unlike calculate-sources and alternatives, the vanilla kernel is not expressly identified as such in this output. The red asterisk to the left of the version indicated that the installed kernel is slimmer compared to the original code. The asterisk on the right labels the default version, defined by /usr/src/linux.

Convert the current settings for the first booting:

cl-kernel --kver 3.19.0 --convert

It is important that you understand the functioning of cl-kernel, which differs considerably from its former implementation.
  1. The script processes the kernel configuration file it fetches from the relevant template.
  2. Once the kernel is compiled, cl-kernel creates a new custom template based on the last user-generated modifications.
  3. As no configuration file is provided for non-Calculate kernels, the outcoming template lists the differences from the default options, if you use any such kernel.
  4. When launched with option "--convert", cl-kernel takes after the current kernel configuration (from /boot or /proc) if ".config" is not to be found in the sources directory.

The kernel gets configured (with make menuconfig), compiled and installed. If you have not set CONFIG_BLK_DEV_INITRD off, initramfs will be created in addition.

Update the modules after compilation time:

emerge @module-rebuild

Done! Now reboot your computer and enjoy the new kernel. If you touched nothing besides entering the commands we have covered here, your system should be running smoothly.

Kernel update

Here is the first line of the template cl-kernel has generated from the original configuration file:

head -n 1 /var/calculate/templates/kernel/10-vanilla-x86_64-3.19
# Calculate format=kernel name=.config os_install_arch_machine==x86_64&&merge(sys-kernel/vanilla-sources)>=3.19

This is the header. It includes the template's format and the edited file's name. It also says if architecture must be checked, along with the kernel name and version. As you can see, our template will be effective beginning with Linux kernel 3.19.

To install v3.19.1, all you need to do is run, after you emerged the package:

cl-kernel --kver=3.19.1

The '--kver' option can be omitted if the kernel is the one by default. If it is, /usr/src/linux will point to it. To guarantee this behaviour, enable symlinking with the "symlink" USE flag before installing the package, as follows:

echo sys-kernel/vanilla-sources symlink >> /etc/portage/package.use/custom

Before updating to an newer version, for example 3.20 4.0.0, you would probably like to visualize the modifications it offers (with make oldconfig). To do so, please execute:

cl-kernel --kver 4.0.0 --kver-old=3.19.1

Configuring calculate-sources

With vanilla-sources, we learned how to install and compile a kernel. But what about designing a custom configuration, with the patches you need, that would be consistently applied each time the kernel is being updated? Nothing is easier.

1. Disable the "minimal" flag for the kernel package:

echo sys-kernel/calculate-sources -minimal >> /etc/portage/package.use/custom

2. Install the sources without compiling them:

USE="-vmlinuz" emerge sys-kernel/calculate-sources

3. Adjust the kernel parameters:

cl-kernel

The latter command does not refer to a version, because there is no need: /usr/src/linux is rewritten each time the package is (re)installed. To check this, run:

cl-kernel --kver list
 * 3.19.0
 * 3.18.7-calculate *

Note that the version is now marked with a green asterisk.

If in doubt, you can always specify the version explicitly, as shown below:

cl-kernel --kver=3.18.7-calculate

While compilation goes on, feel free to look into the configuration template of the kernel thus obtained: you will see that it only contains the differences from the standard settings.

For instance, if you do not want your kernel to support ReiserFS and say so, cl-kernel will write the following lines to the template:

cat /var/calculate/templates/kernel/10-calculate-x86_64-3.18 
# Calculate format=kernel name=.config os_install_arch_machine==x86_64&&merge(sys-kernel/calculate-sources)>=3.18
!CONFIG_REISERFS_FS=m

Warning: repeated launching of cl-kernel will result in commiting all of your modifications. If you prefer to cancel, simply delete the template.

Optimization

Under construction

Patching

Under construction

Development

Under construction

Thank you!