Tuesday, October 08, 2019

Compiling the Linux Kernel 5.3.5

In Ubuntu 18.04 we need to install some tools first:

sudo apt install build-essential libncurses5-dev gcc libssl-dev bc flex bison

Then we download and decompress the kernel source code:

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

tar xvf linux-5.3.5.tar.xz

After that from inside the decompressed directory we run and do the preferred configuration:

make menuconfig

Other options for the configuration would be "oldconfig" and "xconfig"; for this last one we need QT 4.8 or 5.x.

sudo apt install qt5-default

After saving the configuration we run "make" with the option "-j" with the number of processors to speed up the compilation process:

make -j 8
 or
make -j8

My machine has 2 sockets with 4 cores each (Xeon), cache L1d and L1i of 32K each, L2 of 256K and L3 of 10240K; and a total of 16 GB of RAM
Just running plain "make" it took above 2 hours and 10 mins to finish the compilation. When using the -j8 option it took about 32 minutes.

The uncompressed executable that I got is saved directly in the linux-5.3.5 directory with a size of over a 665 MB with the name of "vmlinux".
The compressed executable kernel image is in arch/x86/boot/ with the name of "bzImage" and a size close to 9 MB.

If wanting to get a compiled kernel in the form of a Debian package we need to copy the configuration file from our running boot directory and into the linux-5.3.5 directory.

cp  /boot/config-4.15.0-65-generic  .config

We need to install the "pkg-config" and the "kernel-package" packages:

sudo  apt  install  pkg-config  kernel-package

And then compile it with the deb-pkg command:

make -j8 deb-pkg

Or doing the following steps:

make-kpkg clean

fakeroot make-kpkg -j8 --initrd --append-to-version=-custom kernel_image kernel_headers

After --append-to-version= you can write any string that helps you identify the kernel, but it must begin with a minus (-) and must not contain whitespace.

And to install the kernel in this case would be:

sudo dpkg -i linux-*.deb



Checking the linux kernel source code for special functions, we use the "grep" command to search for specific text inside the kernel source files:

 grep -iRl "sem_init" .

From the list that we get we can check for that text term:

vim ipc/sem.c

Other examples:

grep -iRl "pthread_create" .
vim tools/usb/testusb.c

grep -iRl "pthread_mutex" .
vim tools/lib/lockdep/include/liblockdep/mutex.h

grep -iRl "= fork(" .
vim tools/lib/subcmd/run-command.c

grep -iRl " signal(" .
vim tools/lib/subcmd/sigchain.c




-------------///