5 Useful Tips When Compiling Your Own Linux Kernel

Read this first before you started to compile your own Linux kernel.

5 Kernel Tips 00 Featured Image

The Linux Kernel is a brilliant piece of software engineering. At more than a million lines of code, it is arguably one of the most complex programs that is still in active development today. Since the late 1990s, the Linux kernel has been used in both computationally intensive projects as well as barebones embedded applications.

What is a Kernel and Why Configure It?

Despite all of that, the Linux kernel is just a program that serves as a link between the hardware in your computer and the software that you use everyday. It is what allows you to use a wide range of devices for the programs that you use on a daily basis.

One example of this hardware-software linking is the Advanced Linux Sound Architecture (ALSA). ALSA is a sound driver framework that is built-in to the Linux kernel. It allows you, among other things, to easily install a sound card and configure it to immediately run with your favorite program.

5 Kernel Tips 02 Alsamixer Window

Knowing that, the Linux kernel is program that is largely unknown to the regular user. For the most part, whenever you install a distribution you often get a kernel that will run out of the box. This can be useful if you are just looking to make your system work.

However, if you are using distributions such as Arch and Gentoo, being able to configure your kernel is beneficial in the long run. For example, only enabling the features that you want can speed up boot times.

With that, this article aims to provide five useful tips when compiling your own Linux kernel.

Note: Check out this article to find out how to get started compiling your own kernel.

1. Use the Help System

One of the most useful features when configuring your Linux kernel is the Help system. This is a built-in feature that you can access while inside the Kernel Configuration Menu. In that, this allows you to view a description of every feature and driver that you can install in your kernel.

5 Kernel Tips 03 Sample Kernel Help

To use this system, all you need to do is to press Shift + / while highlighting a particular kernel option. From there, the Configuration Menu will then print a brief slide of what that feature does. For example, I can press Shift + / to the “PCI Support” option to look at all the options that I need to enable to use it.

5 Kernel Tips 04 Linux Pci Sample Help

With that, you can also use the same Help system to search for information about particular kernel features. This can be especially useful if you want to know more about all the options that a specific feature requires.

To do this, you can press / while inside the Configuration Menu. This will, then, bring up a small prompt where you can type the name of a particular option. For example, I can press / then type “PCI” to search for options that use the PCI flag.

5 Kernel Tips 05 Kernel Search Prompt

2. Disable Unnecessary HID Support

The Linux Kernel often touts itself as a software with near universal hardware support. In that, you are often sure that it will be able to drive any device that you connect to it.

5 Kernel Tips 06 Sample Linux Hardware Support

However, one issue with this approach is that including every bit of support increases the overall size and footprint of the kernel. This, in turn, can lead into slow binaries that malicious hackers can easily exploit and break into.

One way to deal with this issue is to remove hardware support that you do not use in your machine. For example, the Linux kernel includes Firewire support in its default configuration. Despite that, most machines nowadays do not include a Firewire port by default.

5 Kernel Tips 07 Linux Firewire Support

Knowing that, one quick way to look at which hardware devices you can disable is by going through the “Special HID Drivers” section. This is a category that you can access under “Device Drivers” and “HID Support” in the Configuration Menu. In here, the kernel will list all of the rare and unusual devices that it can support.

5 Kernel Tips 08 Special Hid Category

With that, you can also use Shift + / and / to search for each option listed in this section. For example, I can select “ELECOM devices” and press Shift + / to look what that option does.

5 Kernel Tips 09 Sample Elecom Help

You can then select the devices that you do want and press N to disable their features. In my case, I will be disabling all Special HID support since I do not use any of these devices.

5 Kernel Tips 10 Disabled Special Hid Drivers

3. Disable Unnecessary Filesystem Support

Another quick way to reduce the size of your kernel is to remove support for filesystems that you do not use. By default, the Linux kernel is built with optional support for most types of filesystems. These include the old BSD FFS as well as some of the high-performance systems such as BTRFS.

5 Kernel Tips 12 Linux Filesystem Support

For the most part, a regular user only really uses a handful of filesystems for daily use. For example, a Linux system generally uses EXT2, EXT4, FAT32 and NTFS for all of its data storage. As such, it is possible to remove support for other filesystems and gain a reasonable amount of extra performance from the kernel.

To remove unnecessary filesystem support in the kernel, all you need to do is to go to the “File Systems” category.

5 Kernel Tips 13 Kernel Filesystem Category

From there, you can then select the filesystems that you want to remove from your system by highlighting them and pressing N. In my case, I will only be leaving support for EXT2, EXT3, EXT4 and JFS.

5 Kernel Tips 14 Few Linux Filesystems Enabled

4. Disable Unnecessary Graphics Drivers

Aside from disabling both Special HID and Filesystems support, you can also use the Configuration Menu to remove any graphics drivers that you do not use. This is because the Linux kernel bundles support for most modern graphics devices. This means that even if your machine does not use a graphics card, your kernel will still run as if you have one.

5 Kernel Tips 15 Linux Graphics Support

Similar to above, this results in a kernel binary that runs features that you do not necessarily need. This, in turn, can be an issue if you are running Linux on either old or weak hardware.

In order to remove unnecessary graphics support, you need to first go to the “Device Drivers” section of the Configuration Menu. From there, you then need to scroll down and select “Graphics support”.

5 Kernel Tips 16 Kernel Graphics Category

Once done, the kernel will then display a list of all the graphics hardware that it provides a driver for. From here, you can then either press Y or N to enable or disable a particular driver.

In my case, I will only be enabling both the “Intel 8XX Graphics” and “Intel GMA500 Framebuffer” since these are the only graphics devices present in my laptop.

5 Kernel Tips 17 Intel Only Graphics Support

5. Install Drivers as Modules

Lastly, one of the best feature of the Linux kernel is its ability to remove parts of itself and reload them as modules. This approach not only reduces the size of the kernel but also allows you to be flexible with kernel features.

For example, you can turn your wireless driver into a module which allows you to update it without recompiling the kernel. As such, modules can be especially useful for users that want to have a fast kernel but also want to retain Linux’s stellar hardware compatibility.

5 Kernel Tips 18 Linux Module Files

To convert built-in features to modules, you need to go to the section and option that you want to convert to a module. For example, to convert my wireless driver I need to go to the “Networking Support” section then “Wireless”.

5 Kernel Tips 19 Kernel Wireless Category

From there, I can then press M on the “cfg80211” drivers to convert them to a kernel module.

5 Kernel Tips 20 Linux Kernel Module Feature

Once done, you can press Esc six times to exit the Configuration Menu. This will, in turn, bring up a prompt that will ask if you want to save the changes that you made. In my case, I will be selecting “Yes”.

5 Kernel Tips 21 Save Kernel Configuration

With that done, you can now compile a version of the Linux kernel with the features that you want by running the following command:

make && make modules_install
make install
5 Kernel Tips 11 Linux Kernel Compile

Frequently Asked Questions

Is it possible know the device drivers that my Linux kernel needs?

Yes! One hurdle for users that are just starting to compile their own kernel is knowing the exact kind of hardware that they are using. While this might seem like a difficult task, there are a bunch of programs that you can now use to ease this process.

For example, you can use both lspci and lsusb to look at all the connected hardware peripherals in your machine. Further, you can also run the command sudo dmesg to check whether the kernel properly reads your devices.

Is it possible to bring back a feature that I removed from the Linux kernel?

Yes! It is possible to reinstall a feature once you have removed it from the Linux kernel. To do this, you need to go back to the Configuration Menu and press either Y or M on the feature that you want to re-enable. Doing this will tell the kernel to include that particular feature either as a built-in or a module.

With that done, you then need to recompile the kernel to create a binary with all the new features. To do this, you need to save and exit the configuration menu by pressing Esc six times. From there, you can now start recompiling the kernel by running make.

When should I install a Linux kernel feature as a module?

Installing features as modules will largely depend on what you need out of your kernel. Knowing that, a good rule of thumb is that you should only create modules for features that are not critical for the boot process.

This is because, at its core, the Linux kernel relies on being able to boot as a single image. For example, removing critical parts such as multi-processor support can lead to a situation where your kernel becomes unbootable.

Because of that, the kernel components that commonly become modules are either graphics and other hardware drivers or network interfaces.

Image credit: Unsplash

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox