Compile linux kernel from Ubuntu

Compile linux kernel from Ubuntu

This guide explain how to compile linux kernel from ubuntu.

This guide start from a minimal Ubuntu, just installed.

Install required package

This pakage are optional but suggested

net-tools: Add ifconfig command, check ip address.
vim: Text editor, could be skipped and use nano

sudo apt install net-tools vim

Necessary pakagge

build-essential: Install base compiler gcc, and other related development tools, like make bison.....
git: Version control system used by Linux kernle
libelf-dev: Library for handle elf file
libssl-dev: Library with cryptographic functions, used by kernel x86 for some purpose

sudo apt install build-essential git libelf-dev libssl-dev bc

Dowload Linux source code

Dowload all kernel git archive with:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git

This operation could be long time

When `git clone` terminated is moment to pek which kernel version compile

uname -r

With uname check which Kernel is running, in my case:

4.15.0-54-generic

Compile same kernel release then v4.15.X, pek last stable version.

git tags | grep v4.15

Check which tags are available at the moment.

At now tag are:

v4.15
v4.15-rc1
v4.15-rc2
v4.15-rc3
v4.15-rc4
v4.15-rc5
v4.15-rc6
v4.15-rc7
v4.15-rc8
v4.15-rc9
v4.15.1
v4.15.10
v4.15.11
v4.15.12
v4.15.13
v4.15.14
v4.15.15
v4.15.16
v4.15.17
v4.15.18
v4.15.2
v4.15.3
v4.15.4
v4.15.5
v4.15.6
v4.15.7
v4.15.8
v4.15.9

Choose last available stable version in this case v4.15.18.

The run

git checkout v4.15.18

This will checout source code at requested version

Could be that git return a warning

You are in 'detached HEAD' state.

This is not a problem, ingnore this error

Compile source code

Enter inside the kernel folder with:

cd linux

Kernel build system are done with Make, and KConfig.
Kconfig provide a kernel configuration and Make provide a build system.

make required-target

Prepare kernel build with, this will cleanup all configuration and delete all object file compiled.

make mrproper

After run target `mrproper` is time to configure kernel.
Choose all required driver, subsystem, architecture....

This is a minimal guide, then take a configuration just done.

Can use a configuration load over Ubuntu just installed.

Is usual find kernel configuration inside itself.
Obtain it running config with:

cp -v /boot/config-$(uname -r) .config

Then use `oldconfig` for adapt your config at choosed kernel, may be that in new kernel version config file are a bit different.
Oldconfig try to choose default value new feature.

make oldconfig

Maybe that Kconfig ask you about some driver, conflict that cannot be resolved with default. Repete for every driver asked by Kconfig.

With a Texteditor, vim / nano edit

Makefile

Then add EXTRAVERSION string wiht your name this will set a good name for grub.

The a valid config done. Ready for compile.
compile with:

make bzImage -j 4
make modules -j 4

L'opzione -j permette a Gnu make di compilare piu file conteporaneamente. Una volta compilato tutto si passa alla installazione.

Installare il kernel appena compilato

sudo make modules_install
sudo make install

Riavviamo e da grub scegliamo la versione da noi compilata, ora dovrebbe partire il kernel da noi compilato.