Compiling Linux Kernel from scratch

0 Comments

Getting the latest Kernel Source code:

I have downloaded the latest version of Linux from the URL: https://cdn.kernel.org/

As of this writing latest version is 6.14.3

Extract the kernel in a folder, for this demo we are going to name our linux kernel extract folder name as Linux.

I am using Ubuntu24.04.2 for this demo.

To be able to compile Linux Kernel we will need below libraries for successfully compiling the Linux Kernel

sudo apt install build-essentials flex libncurses5-dev bison bc libelf-dev

Linux Kernel is built using make tool which expects a .config file in the main root directory.

We will make default config to build a default config file inside root folder of Linux Kernel

make defconfig

We then use

make menuconfig

Make menuconfig opens interactive TUI panel were required setting can be chosen to be part of compilation.

Now it is time to start Kernel compilation, let us use all the cores of processors during Kernel compilation to speed up compilation process

make -j $(nproc)

Once Linux Kernel compilation is successful we are going to see success message as like in below screenshot from my local system

Downloading and compiling Busybox

We are going to download BusyBox source code from the url: https://busybox.net/downloads/

Using BusyBox we are going to create a minimal file system for the Linux Kernel.

I am using version 1.37.0, please download BusyBox compressed version and extract it on your local system.

Similar to Linux compilation we are again going to use make command to compile and make executable version of BusyBox.

Inside the BusyBox source code root folder, please run

make defconfig && make menuconfig

During build menuconfig when the TUI panel opens please select settings and under it select Build static binary (no shared libs) as true by scrolling to that option and selecting the option by click Y key as shown in below screenshot

Exit and save the config

Inside the .config file i had to make below setting as no

CONFIG_TC=n

and then the compilation was successful by using below command:

make -j $(nproc)

compilation is successful and the command

file busybox

gives me below output

Now are going to run

make install

This has created a folder with name _install that contains below directories by default

Inside the same folder we are going to run below command to create below directories that are going to be utilized by Linux Kernel

mkdir dev

Create a file with name init inside the _install directory and add below code in it, custom coding also can be done here in this file as per requirement

#!/bin/sh
mount -t devtmpfs none /dev
echo "Welcome Custom Linux!!"
exec /bin/sh


Now we need to make this init file executable with below command:

chmod +x init

Please run the below command to create a file system in the root folder of BusyBox

find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz

Now Linux Kernel Image and filesystem created with help of BusyBox is ready.

Now i am moving to the parent directory that contains both kernel and BusyBox created file system initramfs.cpio.gz

Now we need to execute them, one way to load them is via bootloader and another way is to execute it on emulator.

As part of this article we are going to execute it on QEMU Emulator, i will covering how to create a bootable version using GRUB boot loader in next article.

Use the below code to launch Linux Kernel in QEMU environment:

qemu-system-x86_64 -kernel linux/arch/x86_64/boot/bzImage -initrd busybox/initramfs.cpio.gz

After running the above command we have filesystem running with Linux kernel ready as shown in below screenshot:

With above step this article comes to an end.

Leave a Reply

Your email address will not be published. Required fields are marked *