This page looks best with JavaScript enabled

Kernel Compilation

 ·  🎃 kr0m

Most people think that kernel compilation is a complicated and difficult process, but if we have certain concepts clear, it will be much easier. The kernel is nothing more than a collection of drivers for our hardware. By compiling it, we will achieve a lower RAM consumption footprint since we will not load hardware drivers that we do not have.

After these clarifications, we are now ready to continue. The first thing will be to find out what HW we have installed on our machine, useful commands for this purpose can be:

  • dmesg –> prints on the screen all the system boot messages.
  • lspci –> shows us information about the devices connected to the PCI ports.

The kernel sources can be obtained in different ways:

We decompress the sources and generate a symbolic link to them:

bzip2 -dc KERNEL.tar.bz2 | tar -xv
ln -s /usr/src/KERNEL linux

We proceed with the configuration:

cd /usr/src/linux
make menuconfig

There may be options that we do not know what they are for, we can read the help but I recommend including everything that is not known in a first compilation and then debugging it.

The options can be compiled in two different ways:

  • In the kernel itself –> Drivers necessary for the OS boot
  • As a module –> Drivers that we use only sporadically

NOTE: If you suffer from extreme paranoia, you can decide to compile everything inside the kernel and disable the possibility of loading modules. This way, the use of most rootkits in our system will no longer be possible.

IMPORTANT: Never, but never, set drivers that the system needs to boot as modules, such as the root partition file system or the IDE, SATA driver.

Once everything is ready, proceed to exit and save the changes.

Compile and install the kernel image and modules:

make && make modules && make install && make modules_install

Configure GRUB to boot the new kernel:

If it is GRUB 1.97:

vi /boot/grub/grub.conf

default 0
timeout 30
splashimage=(hd0,0)/boot/grub/splash.xpm.gz

title Gentoo Linux 3.4.9
root (hd0,0)
kernel /boot/kernel-3.4.9-gentoo root=/dev/sda3

Changing the lines:

  • root() –> Partition where boot is located
  • root= –> Partition where root is located
grep -v rootfs /proc/mounts > /etc/mtab
grub-install --no-floppy /dev/sda

If it is GRUB2:

grub2-install /dev/sda
grub2-mkconfig -o /boot/grub/grub.cfg

NOTE: If it is a virtual machine with virtio disk:

vi /boot/grub/device.map

(fd0) /dev/fd0
(hd0) /dev/vda
grub-install --no-floppy /dev/vda

Restart and a new entry will appear in the boot menu. Select it and you’re done.

PS: Human knowledge belongs to the world ;)

If you liked the article, you can treat me to a RedBull here