Custom Kernel

From Alpine Linux
This material is work-in-progress ...

Do not follow instructions here until this notice is removed.
(Last edited by Orson Teodoro on 5 Mar 2018.)

This process of building a custom configured kernel assumes you are running on Alpine Linux utilizing abuild & aports.

But why?

You want to build a custom kernel to enable experimental hardware or features or outdated hardware, to reduce bloat further, to tune the kernel to the hardware.

The vanilla kernel for most Alpine ARCHs uses defaults to balance throughput at the expense of some responsiveness, and support for many devices. You can tweak the kernel for desktop use and low latency and responsiveness.

Setting up the Alpine Build System

First, you need to follow the steps in Setup your system and account for building packages. You also need to configure your /etc/apk/repositories so that they search locally for your apks. See Testing the package locally for details.

Working with aports

There are several ways to maintain a kernel. The first option is to create a new kernel package. The other option is to just use the existing vanilla kernel just tweaking the configure-vanilla.ARCH file.

Switching to the proper release version

You need to switch to the proper branch that matches the release so that the kernel compiles against the dependencies properly.

Alpine version Remote branch
Edge master
3.7.0 3.7-stable

The following is required to get access to the APKBUILD released for that version of Alpine and which you will create a commit for.

If you are on 3.7 do:

 git checkout -b 3.7-stable origin/3.7-stable

If you are on Edge do:

 git checkout master

Option A: Creating a new kernel package

Use this option only if you want to want to be a kernel package maintainer or have custom patches.

What you need to do is copy main/linux-vanilla folder to testing/linux-NAME, where NAME could be anything but usually initials or a project name to a special kernel patch or patchset. Just use your last name if you don't know what to put. Rename all files from vanilla to NAME. Rename everything in APKBUILD from vanilla to NAME.

Option B: Vanilla with native settings and minimal edits

Most users will want to use this option.

You can use linux-vanilla but what you should do is create a local branch by doing:

For Alpine Edge:

 git checkout -b my-custom-kernel

For Alpine 3.7:

 git checkout -b my-custom-kernel origin/3.7-stable

Doing it this way, you do less work in maintaining. All you need to do is keep master or 3.7-stable in sync[1][2] and merge any conflicts.

First switch to the branch by doing git checkout my-custom-kernel. Then, you need to navigate to the main/linux-vanilla folder where you should see a APKBUILD and some config- files. When you are done with your edits either by editing directly the APKBUILD and copyting the config-vanilla.ARCH as .config in the linux-4.15 folder. You will then move the .config back overriding the config-vanilla.ARCH generated by make menuconfig (discussed below in the Configuring kernel section). After generating your config, you need to abuild checksum. Then, do git add APKBUILD config-vanilla.ARCH where ARCH is whatever architecture (x86, x86_64, ...) you use. Then, you need to do git commit APKBUILD config-NAME.ARCH -m "Enabled these options ...." for your custom _flavor and the ARCHitecture of your system. You do this so that git can keep your code separate from Alpine's and so your changes float forward between kernel updates.

Adding custom patches

Custom patches should be added to sources=.

After you added the URL, you need to produce a checksum by doing abuild checksum.

The custom patches may not be autopatched, due to being distributed as an archive or different patch level, so you need to define what to do with it in the prepare().

Configuring kernel

Attempt to build the kernel first. To do that, you do abuild -rK to install most of the dependencies. If it complains about a dependency like elfutils-dev use -rKd. Then, when it prompts for values for new found config options just hold enter till it starts compiling the kernel. There should be two sets one for -vanilla and the other for the -virt. Just Ctrl+C out of the compilation process after the second set so you can further customize the config. Then you go into the src/linux-VER and edit the config file. Copy the .config file overriding the config-NAME.ARCH in the srcdir.

The alternative is to use the kernel configuration menu in the build-NAME folder, but before yo do that you need to sudo apk add ncurses-dev

After you are done using the menu in the build-NAME folder by doing make menuconfig, you want to remove ncurses-dev. When you are done, it will be stored in .config which you need to again override the config-NAME.ARCH file. When you are done updating the config-NAME.ARCH, you need to do abuild checksum.

The options in the kernel config are typically defaults. If your device is old, it may be set to n by default.

For the kernel package maintainer

Each config-NAME.ARCH should be updated for all ARCHes so that it doesn't disrupt the building process. To do this:

 cd src/linux-4.15
 cp ../../config-NAME.ARCH .config
 make ARCH=x86 oldconfig
 (hold enter to accept all default options)
 make ARCH=x86 menuconfig (optional to tweak it for the particular arch)
 cp .config ../../config-NAME.ARCH

Don't forget to abuild checksum when you have processed all ARCHs.

The supported archs are in the linux-4.15/arch which you replace x86 or one of the kernel ARCHs below. The mapping is as follows:

Alpine ARCH Kernel ARCH
x86 x86
x86_64 x86
s390x s390
aarch64 arm64
ppc64le powerpc
ppc powerpc
armhf arm

The kernel package name should not have the number in the name.

Building

You should then do an abuild -r to attempt to build it.

Installing

To install it you do a sudo apk add linux-NAME where NAME is your custom kernel release name or vanilla if you used option B.

Bootloader

You need to configure your bootloader to use the kernel. Add a new entry but do not replace the old kernel. The old kernel is your way back if the kernel config was a bad one. The naming scheme should be similar but with the tag. You should make sure that the _flavor name is attached for new kernel packages and do not override the existing vanilla kernel and the existing vanilla initramfs.

For grub the new entry should look something like

 menuentry 'Alpine Linux (ck1)' {
         set root=(hd0,7)
         linux /vmlinuz-ck1 root=/dev/sda17 rw modules=sd-mod,usb-storage,ext4
         initrd /initramfs-ck1
 }

To install the bootloader with grub, you do something like grub-install --force /dev/sda7 where /dev/sda7 is your bootable drive found with fdisk -l or grub-install --force /dev/sda depending on how you set up grub.

Testing

To test, first you should make a bootable Alpine USB image. Then, when you have your rescue USB done, you sudo reboot the computer.

To test it, you basically do trial and error. Sometimes your config is missing something if you want to have a bare minimum setting.