Calculate kernel

Updated 31 January 2020

Linux

Kernel versions

Our kernel comes as the sys-kernel/calculate-sources package and has several versions and variants described by USE flags. Example:

eix calculate-sources

[I] sys-kernel/calculate-sources [1]
     Available versions:      
     (4.19.96) 4.19.96^bs
     (4.19.97) ~4.19.97^bs
     (5.4.12) 5.4.12^bs{tbz2}
     (5.4.13) ~5.4.13^bs
       {build desktop firmware fsync +grub minimal muqss pae symlink themes uksm +vmlinuz KERNEL="linux"}
     Installed versions:  5.4.12(5.4.12)^bs{tbz2}(02:38:06 18.01.2020)(desktop firmware fsync grub minimal symlink themes vmlinuz -build -muqss -pae -uksm KERNEL="linux")
     Home page:     http://www.calculate-linux.org
     Description:              Calculate Linux kernel image

[1] "calculate" /var/db/repos/calculate

Stable kernel versions, as well as the latest one comes in binary both for desktop and server applications. When we were writing this, the stable kernel versions were 5.4.12 and 4.19.96, while the latest version was 5.4.13.

To switch between the desktop and the server versions, toggle the desktop USE flag. Here is the list of all other kernel USE flags:

  • firmware adds sys-kernel/linux-firmware or sys-firmware/eth-firmware to dependencies
  • fsync improves performance in games that use Proton by Valve
  • grub add Grub to dependencies
  • minimal does not install the sources when installing the kernel
  • muqss adds support for MuQSS
  • symlink creates a symlink pointing to the sources files, /usr/src/linux
  • themes adds the Plymouth theme to dependencies
  • uksm is an alternative to ksm, a technology of merging identical memory pages into one.
  • vmlinuz compiles and installs the kernel at package installation time

Downgrading the kernel

In case of problems due to system instability, as well as hardware or driver support problems, you can downgrade the kernel. To move to version 4.19.x, run:

cl-update --sync-only

echo ">sys-kernel/calculate-sources-5" >> /etc/portage/package.mask/custom

emerge -a sys-kernel/calculate-sources

Now restart your computer. Select Advanced options in the bootloader menu and select kernel version 4.19. Once the system has loaded, complete the update procedure by running:

cl-update

Cl-update will remove the ~5.4 kernel and rebuild packages containing kernel modules.

Installing the latest kernel version

Installing the latest kernel version is justified only if you need some functionality that is not provided by your kernel version, or add support for new hardware. To install the latest version of the Calculate sources, run:

echo "sys-kernel/calculate-sources" >> /etc/portage/package.keywords/custom

cl-update

It will install the latest version of the kernel, then update all modules. One of the packages might fail to compile with the new kernel. If this is a module you do not use, feel free to delete the failing package.

Custom kernel compilation

When building a kernel from the sys-kernel/calculate-sources package, it is configured using relevant Calculate Utilities templates. You can create your template with modified settings in the most convenient way. Your template will be applied later on whenever you recompile the kernel.

First, you need to fetch source files for the kernel. To do this, run:

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

To edit the settings and recompile the kernel, run:

cl-kernel

The program will launch Calculate Utilities to generate the default settings, then run make menuconfig to call the configuration interface. When completed, a template will be created that includes the changes made, and then the kernel compilation and installation processes will launch:

Linux kernel configuration

By default, the kernel sources pointed to by symlink /usr/src/linux will be used. If you have multiple versions of sys-kernel/calculate-sources installed on your system, or other packages with kernel source code (such as sys-kernel/gentoo-sources, sys-kernel/vanilla-sources~, etc.), you may need to specify the kernel version to be compiled. To view available versions, run:

cl-kernel --kver list

 * 4.19.97-gentoo
 * 4.19.96-calculate *

Here is an example of how you specify a kernel version:

cl-kernel --kver 4.19.96-calculate

If you do not want the kernel to recompile after configuring it, use the --skip-build option.

You may need to update the kernel modules once you have updated the kernel itself. Make sure that symlink /usr/src/linux points to the relevant kernel version, then run:

emerge @module-rebuild

You can restart the configuration to make sure that your modifications have been saved. The result is saved in the directory of custom templates, /var/calculate/templates, in kernel subdirectory.

tree -a /var/calculate/templates

/var/calculate/templates
└── kernel
    ├── 10-calculate-x86_64-4.19
    └── .calculate_directory

1 directory, 2 files

To use the new configuration later on when installing new versions of the kernel, you should install sys-kernel/calculate-sources as a non-binary package. You may enable the -minimal USE flag for calculate-sources:

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

Now when you install the package, the kernel will compile with your modifications:

emerge -a sys-kernel/calculate-sources

Switching to another kernel

As mentioned above, cl-kernelhelps you build and install sys-kernel/calculate-sources+ as well as any other kernel such as sys-kernel/gentoo-sources+, sys-kernel/vanilla-sources+, etc.

For example, install the latest version of sys-kernel/vanilla-sources from the original source with no third-party patches:

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

emerge -a sys-kernel/vanilla-sources

View all available kernels:

cl-kernel --kver list

 * 5.4.15
 * 5.4.12-calculate *

Note that 5.4.12-calculate is marked with red. This means that it contains no source code, since sys-kernel/calculate-sources is installed by default with the disabled minimal USE flag.

To configure, compile and install any kernel, you only have to run something like that:

cl-kernel --kver 5.4.15

Obviously, building a kernel from scratch is quite time-consuming, and anyway, you probably already have your custom .config configuration file. If this is the case, you can copy it to the kernel source directory, /usr/src/linux-5.4.15/.config, before actually building up the kernel. To apply the actual kernel settings, run:

cl-kernel --kver 5.4.15 --convert

In this case, if there is no .config in the directory containing kernel sources, the settings will be fetched from the currently running kernel. Then themake oldconfig command will be run to set values in the new parameters, followed by make menuconfig to configure the kernel. When the configuration process is complete, the kernel will be compiled and installed on your system. A kernel configuration template will be created:

head -1 /var/calculate/templates/kernel/10-vanilla-x86_64-5.4

# Calculate format=kernel name=.config os_install_arch_machine==x86_64&&merge(sys-kernel/vanilla-sources)>=5.4

When installing sys-kernel/vanilla-sources, this template will be used to create a .config file.

Building kernel without initramfs

The initramfs file is used to load kernel modules at an early stage of system boot. After the first boot of an installed system, initramfs is optimized and the modules that are not loaded into memory are removed from it, reducing the kernel load time. Removing initramfs may make your kernel start even faster, with a noticeable impact on the overall system boot time.

To rebuild your kernel without initramfs, two conditions have to be met: you must disable initramfs creation in the kernel settings and enable including filesystem and hardware drivers into the kernel.

Launch the kernel configuration process:

cl-kernel

Go to the General setup section and disable +Initial RAM filesystem and RAM disk (initramfs/initrd) support.

Screenshot

Initial RAM filesystem and RAM disk (initramfs/initrd) support

Include the root file system in the kernel. To find out which file system is being used:

findmnt /
TARGET SOURCE         FSTYPE OPTIONS
/      /dev/nvme0n1p2 btrfs  rw,noatime,ssd,discard,space_cache,subvolid=5,subvol=/

Go to the File systems section and specify Y for your file system.

Screenshot

Btrfs filesystem support

This must be sufficient for a desktop running on a M.2 hard drive. If you are using SATA/SAS or SCSI controllers, RAID volumes, etc., you may need to compile additional drivers into the kernel that are responsible for supporting your device. Once the setup is complete, exit the program. Kernel compilation will be launched.

Adding patches

Kernel patches can add hardware support or, in some cases, improve system responsiveness or make other improvements. To add this, save the patch to /var/calculate/templates and add the template header as the first line of the file to convert it to a template:

/var/calculate/templates/file_patch

# Calculate format=diff env=install ac_install_patch==on&&merge(sys-kernel/calculate-sources)>=4.19
...

The header contains the diff format, the condition for the patch to be applied and the version check for the package to be installed.

Example

Here is an example of adding the [kernel_gcc_patch] (https://github.com/graysky2/kernel_gcc_patch) that extends the list of processors the kernel can be optimized for. Fetch the repo content first:

git clone https://github.com/graysky2/kernel_gcc_patch.git

Copy the patch file to the kernel template directory:

cp kernel_gcc_patch/enable_additional_cpu_optimizations_for_gcc_v9.1+_kernel_v4.13+.patch /var/calculate/templates/kernel_gcc_patch

Add the template header to the patch:

/var/calculate/templates/kernel/patch/kernel_gcc_patch

# Calculate format=diff env=install ac_install_patch==on&&merge(sys-kernel/calculate-sources)>=4.19
...

Install the kernel sources. --jobs=1 will start installation in a single process. You can check the log for the successful patch application message:

USE="-vmlinuz -minimal" emerge -a --jobs=1 sys-kernel/calculate-sources
...
>>> Source configured.
 * Applying Calculate patches with tools for calculate-sources ...
 * Applying patch enable_additional_cpu_optimizations_for_gcc_v9.1+_kernel_v4.13+.patch
 * Calculate Utilities modified files:
 *      .config
 *      arch/x86/Kconfig.cpu
 *      arch/x86/Makefile
 *      arch/x86/Makefile_32.cpu
 *      arch/x86/include/asm/module.h
>>> Compiling source in /var/calculate/tmp/portage/sys-kernel/calculate-sources-5.4.12/work/linux-5.4.12-calculate ...
...

Configure the kernel by selecting your CPU from the new list:

cl-kernel

Go to Processor type and features ---> Processor family (Generic-x86-64). You will see a much longer list of available CPUs.

Screenshot

Linux/x86 Kernel Configuration

Once the configuration process completed, the new kernel will be built. The patch and the updated kernel settings will be used as well when compiling sys-kernel/calculate-sources+.

Kernel modules

A module is an object that contains some code providing additional functionality to the running kernel. Run lsmod to view the loaded modules in real time. To view full info on a module, run modinfo <module_name>. To make the kernel module load automatically during system boot, create a file with the .conf extension in /etc/modules-load.d. In this directory, you can list custom modules and, if necessary, the parameters for them as separate lines. Example:

/etc/modprobe.d/custom.conf

options [HTML_REMOVED] [HTML_REMOVED]=[HTML_REMOVED]

Use the modinfo tool to view the available module options. Here is how to view the parameters available for a module (snd-intel8x0 in this example):

modinfo -p snd-intel8x0

index:Index value for Intel i8x0 soundcard. (int)
id:ID string for Intel i8x0 soundcard. (charp)
ac97_clock:AC'97 codec clock (0 = whitelist + auto-detect, 1 = force autodetect). (int)
ac97_quirk:AC'97 workaround for strange hardware. (charp)
buggy_semaphore:Enable workaround for hardwares with problematic codec semaphores. (bool)
buggy_irq:Enable workaround for buggy interrupts on some motherboards. (bint)
xbox:Set to 1 for Xbox, if you have problems with the AC'97 codec detection. (bool)
spdif_aclink:S/PDIF over AC-link. (int)
inside_vm:KVM/Parallels optimization. (bint)
enable: (bool)
joystick: (int)

For instance, here is how to load snd-intel8x0 with the set ac97_clock value:

/etc/modprobe.d/snd-intel8x0.conf

options snd-intel8x0 ac97_clock=48000

You can add modules to blacklist.conf to disable them. For instance, this is how to disable module usblp:

/etc/modprobe.d/blacklist.conf

blacklist usblp

Removing a kernel

If you delete sys-kernel/calculate-sources, the Calculate kernel and all its modules will be removed from your system. If a kernel (either a Calculate one or not) was built with cl-kernel, the installed kernel will be removed together with its modules when this package is removed.

If you want to prevent removing a kernel package from removing /boot and its modules, proceed as follows:

/etc/calculate/ini.env

[system]
remove_old_kernel = off

Note

When updating packages with cl-update, the old kernel will not be removed if the system is running on it.

For faster kernel compilation

Use ccache to compile your kernel in a much faster way:

emerge -a dev-util/ccache

Now kernel 5.4.12 builds up in 1min 25sec with ccache on a test PC with AMD Ryzen 5 3600 processor, instead of ~~10 min 24 sec before.