Compiling Your Own Linux Kernel (Debian)

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
344
Reaction score
377
Credits
11,920
Many people may be fine using the standard default kernel that comes with your distro. There are some out there who may want to make their own kernel.

For a special system in your life, you know what hardware is on the system. You know everything about the system (at least you hope). You can optimize the kernel to use less space on the drive, but especially in memory.

Kernel Version

From the website 'www.kernel.org', we can find the source code available of present and past kernels. The version I am going to use is the current stable version. The current stable version is usually a clickable link in a yellow square, as shown in Figure 1.

Figure 1.JPG

FIGURE 1

In the case, as shown in Figure 1, I am going to download Kernel version 6.6.3. Currently, I am using Ubuntu 22.04 Workstation as a VirtualBox machine so I do nothing too problematic and cause my regular system to fail. Which, even if I did, I can boot into the GRUB Menu and load a different kernel version to get back into my system.

If you put the cursor on the yellow button of the newest stable version, you can see in the Status Bar the location of the file on the server. In my case, the file is 'https://cdn.kernel.org/pub/linux/kerel/v6.x/linux-6.6.3.tar.xz'.

Getting the System Ready

Now that we know the kernel we will use, we can start getting things ready and also download the source code of the kernel.

So, we need to install some essential packages:

sudo apt install bc bison build-essential fakeroot flex git libelf-dev libssl-dev ncurses-dev rsync wget xz-utils -y

The usual utility used to enable/disable or change an option to be a module is text based. The program is called 'menuconfig'. If you want a Graphical User Interface (GUI) based program, then you'll need:

sudo apt install libgtk2.0-dev libglib2.0-dev libglade2-dev libcanberra-gtk-module libcanberra-gtk3-module -y

This should make the GUI program usable. Now, we need to get the source code for the kernel. To do all of this, we need root privileges, so we should run 'sudo -i'. It might ask you for the password. Once you have elevated privileges, then continue.

Now, let's get the source code:

wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.3.tar.xz

It should download the file into the current folder, which should be '/root'.

After it downloads the file, it needs to be extracted, since it is a compressed file. Use the command:

tar xvf linux-6.6.3.tar.xz

It should extract the files into a folder with the same name as the file name. Switch into this folder.

We now need to copy the information on the system that was used by the kernel. The command is:

cp -v /boot/config-$(uname -r)* .config

The local file will be called '.config' and it is this file we change that will help to compile our new kernel.

We can make changes using scripts already setup. Run the following to make changes:

scripts/config --disable DEBUG_INFO
scripts/config --set-str SYSTEM_TRUSTED_KEYS ""
scripts/config --set-str SYSTEM_REVOCATION_KEYS ""


You can also edit the 'Makefile' file and add a suffix to the line 'EXTRAVERSION', such as '-test'. When we create a kernel, it will add the suffix to the kernel version name. There is another way to do this, which I will cover later. You should only do one option and not both, or you will append two suffixes to the files that are created.

The system is now ready to start the creation of a new kernel.

Making Changes

As mentioned before, there are two ways that you can use to change the '.config' file. One is text-based, while the other is a GUI. Let's use both to get an idea.

The text-based option lets you search for specific entries, which the GUI does not. We can start with the text-based and finish with the GUI program.

To start the text-based program, use:

make menuconfig

Figure 2 shows the editor.

Figure 2.JPG

FIGURE 2

You can use the arrow keys to move around. Up and down will change the highlighted entry of the menus. Us the 'ENTER' key to open a sub-menu. These have a text section with the '--->' after the menu item, such as 'General Setup' that is already highlighted.

For some options, you only have ‘enable’ or ‘disable’ as options (asterisk or no asterisk in brackets). The less than and greater than used as brackets signifies three options. An asterisk for enabled, no asterisk for disabled, and an 'M' to be a Loadable Module. You can enable or disable the Loadable Modules from the Operating System as needed.

Once you enter a sub-menu, you can go back up one level by pressing 'ESC' twice.

It listed the options at the top. You can see there is a search option as well. Let's try this out. Press '/' and you should see a search box appear. Enter 'ISDN' and press 'ENTER'. It shows the results of the search shown in Figure 3.

Figure 3.JPG

FIGURE 3

The first option listed is the one we want. You can see that there is information for the sub-menu option, such as its location. It is located under 'Device Drivers', 'Network Device Support', and finally 'ISDN Support'. To the left, you see a '(1)'. If you scroll down a way, you can see a second on and so on. So, if I want this first one, I can press '1' on the keyboard and jump to the specified entry. If you do not use 'ISDN', then highlight it and press the spacebar to disable the ISDN Support from the kernel.

Since we came from the Search menu, use the right arrow to highlight 'Exit' and press 'ENTER' for exiting the Search Menu.

You can now look around the editor. Let's try one more option to remove. Arrow down to 'Networking Support'. Arrow down twice to 'Amateur Radio Support' and press the spacebar to remove the asterisk. You can arrow to the right and highlight 'Help', then press 'Enter'. You will see a little help with the option that you removed. Press 'Enter' to exit the 'Help' information.

There are a few other options to search for and disable, unless your system requires it:

Arcnet
EISA
PCMCIA
Radio


You can also go into 'Device Drivers' and then 'Network Device Support' and finally 'Ethernet Driver Support'. You can disable any drivers you do not need. If you plan on moving the kernel to another system, be sure to leave its network drivers enabled. For any options that you must make an '*' or an 'M', leave as an 'M'.

Arrow over to 'Save' and save the changes to '.config', which should be the default. Choose 'Exit' to close the 'Save' information. Select 'Exit' twice to close 'menuconfig'.

Now, let's look at the 'gconfig' GUI program, shown in Figure 4. Use the command 'make gconfig'.

Figure 4.JPG

FIGURE 4

Looks similar, but this program responds to mouse clicks. You should also see that the lower portion of the window shows 'Help' information when you choose an entry. If you open 'Network Support', see 'Amateur Radio Support' is unchecked. You can select the option and see the same help you saw in 'menuconfig'.

The 'gconfig' program does not allow for searching, but it is easier to get around and enable or disable items.

Once you have made your changes, 'Save' the changes and exit the program.

Next, we can compile the code.

Compiling and Installing the Kernel

The next step is to compile all your changes that are saved in the '.config' file. Run the command:

make -j# bindeb-pkg LOCALVERSION=-test

If you have more than one core, then you can multi-process the files among the cores. Just add the parameter '-j#', where '#' is the number of cores you want to be used. For example, if you want to use two cores, the parameter is '-j2'.

The '-test' can be anything you wish, which will become part of the filename. This is the same as the suffix placed in the 'Makefile' file.

The command can take quite a while, so it is usually best to use multiple cores.

NOTE: If you get any errors, then you may have skipped a step. It is imperative that you edit the two lines in the '.config' file if they exist. Also, you cannot simply run 'make' again. You need to run 'make clean' to remove any files created during the initial 'make' command.

After the system creates the 'deb' files, they will exist in '/root/' folder.

You can move the kernel to another system if you need to after you test it.

To install the new kernel, use the command 'dpkg -i *.deb' to install all the files created.

Reboot your system and make sure you are getting the new kernel with the command at the terminal:

uname -mrs

If it loads the wrong kernel, the reboot and press the SHIFT key when the screen blanks before Linux loads. In the GRUB Menu, select 'Advanced' and then select the kernel version.

If the system doesn't boot correctly with the new kernel, then reboot again and press the SHIFT key at boot to show the GRUB Menu. If needed, select a different Kernel version to boot from to get the system up again. It may be preferable to start up the original kernel that you used when you compiled the new kernel.

Conclusion

This is a great way to streamline a kernel to get only the drivers and other options that are specific only to your system. I left in the Ethernet Adapter drivers for the VirtualBox machine (Realtek) and also the drivers for a desktop system using Intel hardware (Intel). I installed the 'deb' files onto the desktop system running Debian 12.2. It worked just fine and booted without a hitch.

For best results, use the GUI based 'gconfig' to get help about individual options so you do not remove an option that may be necessary.
 

Members online


Latest posts

Top