blog posts

Kernel

How to Compile a Linux Kernel

How to Compile a Linux Kernel

Compiling the Linux kernel is one of the essential things that we must do for our system. When we add hardware to our system, we need to compile it.

The kernel is the main part of any operating system. Of course, the old operating systems lacked a kernel, and the user communicated directly with the hardware. This method increased the speed, but it had many disadvantages, including the fact that if the user made a mistake, the hardware might have a problem.

Core tasks

  • parts management;
  • Memory Management;
  • processor management;
  • communication between the user and the hardware;

Linux kernel

The name of the most important part of the Linux operating system is “Linux Kernel,” derived from its name.
Linux kernel compilation

Once upon a time, updating the Linux kernel scared many users. Back then, updating the kernel involved many steps and took time. But now, installing a new kernel can be easily controlled with a package manager like apt. Adding custom repositories allows you to install demo or special kernels (like real kernels for sound generation) without any hassle.

Here it is better to review the Linux kernel development process

Warning: It is better to practice this method on a virtual machine. Until you learn to work with it well.

Update your kernel

Whether you have a brand new laptop with a fancy new graphics card or you just got a new printer, the operating system (GNU + Linux or just Linux, which also refers to the kernel) needs a driver. Establish communication channels with that new component (graphics card, chip, printer, or other).

This can be a bit confusing. When you connect a new device to the operating system, it may be easy for your computer to verify it. Sometimes this process is what you need, but in other cases, your operating system will use public protocols to check the connected device.

For example, your computer may be able to recognize your new network printer. Still, sometimes the network card in the printer is programmed to identify itself on a network to obtain a DHCP address. This does not mean that your computer knows in advance what instructions to send to print a page of printed text. You might say that the computer doesn’t really “know” it’s a printer. Here, the connected fragment may indicate that a device exists on the network at a particular address and identify the device with a set of characters p-r-i-n-t-e-r.

Human language conventions are meaningless to computers. A computer is like a car that needs a driver.

Kernel developers, hardware manufacturers, support technicians, and hobbyists know that new hardware is constantly being developed. Many directly submit their information to the kernel development team for inclusion in Linux. For example, drivers for NVIDIA graphics cards are often written in the Nouveau kernel module. Since NVIDIA cards are common, the code is usually included in any kernel distributed for general use (such as the kernel you get when downloading Fedora or Ubuntu). There are similar modules for many other devices: printers benefit from Foomatic and CUPS, wireless cards have b43, ath9k, the wl module, etc.

Linux kernel compilation steps

Before compiling the kernel, we must first know our system’s hardware. Usually, people are unaware of their system hardware, but there are tools with the help of which we can find out about our system hardware. The first thing we need to gather information about is RAM information.

You can use these commands to find out RAM information:

PCI parts: To obtain PCI parts, we use the following commands:

The first thing to do is download the kernel source file. This can be done by finding the URL of the kernel you want to download (from Kernel.org). Once you have the URL, load the source file with the following command (shown here with 4.17 RC2 kernel):

wget https://git.kernel.org/torvalds/t/linux-4.17-rc2.tar.gz

Installation requirements:

To compile the kernel, we first need to install a few things. This can be done with a single command:

 

sudo apt-get install git fake root build-basic Ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison

Note: You need at least 12GB of free space on your local drive to go through the kernel compilation process. So make sure you have enough space.

1. Source extraction

Extract the newly downloaded kernel from inside the directory with the following commands. The filename is Linux-x.y.z.tar.xz, where x.y.z is the actual Linux kernel number. For example, the file Linux-5.6.9.tar.xz represents the Linux kernel version 5.6.9. Use the widget command to download the Linux kernel source code:

$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.6.9.tar.xz

Kernel compilation steps

Step 2. Get the xz file.

You don’t need to get the source code in /usr/src. You can extract the source code to your $HOME folder or any other directory using the UNIX command or the xz command:

$ unxz -v Linux-5.6.9.tar.xz

Or

$ xz -d -v linux-5.6.9.tar.xz

First get PGP for linux-5.6.9.tar:

$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.6.9.tar.sign

$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.6.9.tar.sign

Output:

gpg: assuming signed data in ‘Linux-5.6.9.tar.’

gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT

gpg: using RSA key 79BE3E4300411886

jpg: Can’t check signature: No public key

To verify the signature, for example, RSA key ID 79BE3E4300411886 (from the above output), get the public key from the PGP server.

You have to choose different options based on your needs. Each configuration option has a HELP button, so select the Help button to get help. Please note that the ‘make menu config is optional. I used it here only for demonstration purposes. With this option, you can enable or disable specific features or kernel drivers. If you remove the device or option driver support, you will have a broken kernel. For example, if the ext4 driver is removed from the kernel configuration file, a system may not boot. When in doubt, leave support in the core.

3. Kernel configuration

Before we compile the kernel, we first need to do the necessary configuration and specify which modules should be installed on our kernel. There is an easy way to do this. You can copy the kernel configuration file with one command and then use the tried and true menuconfig command to make the necessary changes. To do this, issue the command:

$ cd Linux-5.6.9
$ cp -v /boot/config-$(name -r) .config

Step 4. Install required compilers and other tools

You must install development tools such as GCC compilers and related tools to compile the Linux kernel.

Sudo yum group install "Development Tools."

Or

Sudo yum group install "Development Tools."

Step 5. Kernel configuration

You can now start configuring the kernel by typing one of the following commands in the source code folder:

$make

You have to choose different options based on your needs. Each configuration option has a HELP button, so select the Help button to get help. Please note that the ‘make menu config is optional. I used it here only for demonstration purposes. With this option, you can enable or disable specific features or kernel drivers. It seems convenient if you remove device driver support or similar options and end up with a broken kernel. For example, if the ext4 driver is removed from the kernel configuration file, a system may not boot. If in doubt about anything, leave support in core.

Step 6. Update the grub configuration

You need to modify the Grub 2 loader settings. As per your Linux distribution, type the following command at a shell prompt:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg
sudo grubby –set-default /boot/vmlinuz-5.6.9

Conclusion

When new hardware is added to the system, your Linux will need to compile the Linux kernel. With these steps, you have finished building the Linux kernel from source code, and the compiled kernel should run on your system. Always try to backup essential data and visit kernel.org for more information.
Frequently Asked Questions

What is the Linux kernel?

The Linux kernel is the most important part of the Linux operating system.

How do I compile my Linux kernel?

We can update our Linux kernel by knowing the system hardware and through the above steps.

How long does it take to compile the Linux kernel?

It depends on the number of modules and other things, but the process will probably take anywhere from one to five hours. One hour for the core and maybe 3-4 hours for the modules.

Is it possible to compile the core? The kernel cannot be compiled without a compiler but can be installed from a compiled binary. If you want to compile the kernel, you need the source, compiler, and other tools. Even in “source-based” distributions like Gentoo, they start from running the compiled binary.