DEV Community

DavidJones18
DavidJones18

Posted on

How to build the Linux kernel from the source code

In this article I'll tell you how to pre-build the Linux kernel, probably many already know this, but I'm sure that there will also be those for whom this material will be useful ..

The reasons for collecting the Linux kernel from the sources are generally the same - getting the latest version of the kernel, urgent application of security patches, optimizing for specific tasks and specific hardware, as well as the desire to participate in the development of the kernel, even in the role of QA.

Important! Following the instructions in this post may result in the loss of your data. Make backups and remember that you are doing everything solely at your own peril and risk. Everything described below was tested on Ubuntu 14.04 LTS. But on other versions of Ubuntu, as well as other Linux distributions, the differences should be minimal.

Configure the loader
The / etc / default / grub rule is something like this:

GRUB_DEFAULT = 0

GRUB_HIDDEN_TIMEOUT = 10

GRUB_HIDDEN_TIMEOUT_QUIET = true

GRUB_TIMEOUT = 10
GRUB_DISTRIBUTOR = lsb_release -i -s 2> / dev / null || echo Debian
GRUB_CMDLINE_LINUX_DEFAULT = "quiet splash"
GRUB_CMDLINE_LINUX = ""
After editing we say:

sudo update-grub
As a result, before starting the system for 10 seconds, you will be prompted to select the kernel with which you want to boot. It is very convenient if you mess with the kernel configuration and want to boot from the previous version!

We put dependencies
We need at least the following packages:

sudo apt-get install git gcc make bc fakeroot dpkg-dev \
  libncurses5-dev libssl-dev
On many systems, all of them, however, will already be present.

Get the source
The kernel sources can be downloaded from the link on the kernel.org home page. At the time of writing, the last stable version of the kernel was 4.6.4:

wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.6.4.tar.xz
tar --xz -xvf linux-4.6.4.tar.xz
cd linux-4.6.4
Or, if you need the very-very svezhak, you can take the source directly from Git:

Mirror: https://github.com/torvalds/linux

git clone 'git: //git.kernel.org/pub/scm/linux/kernel/git/' \
'torvalds / linux.git'
cd linux
Judging by the fact that the v4.6.4 tag was not found in Git, the Linux kernel releases are made exclusively in the form of compressed tar archives.

If instead of a vanilla kernel you would like to assemble a kernel with patches from Canonical:

git clone git: //kernel.ubuntu.com/ubuntu/ubuntu-trusty.git
cd ubuntu-trusty
git tag | less
git checkout Ubuntu-lts-4.4.0-31.50_14.04.1
In my experience, if you use Ubuntu, you can safely use a vanilla kernel. It's unlikely that you will have any problems with it.

Note: It is interesting that from existing relatively popular Linux distributions the kernel without its own patches seems to use only Gentoo, Slackware and Arch Linux.

Anyway, now you have the source.

We collect and install the kernel
Choose the options with which the kernel will be built:

make menuconfig
If necessary, change the settings, click Save, then Exit. As a result, a .config file will be created containing the parameters we selected.

When updating the kernel (you already use any kernel anyway?) It is convenient to take the configuration of the current kernel, and set the new options to the default values:

zcat /proc/config.gz> ./.config
make olddefconfig
Finally, we collect:

make -j4 bindeb-pkg LOCALVERSION = -custom
The nucleus is going to take a long time. On my laptop, the assembly took 1 hour and 15 minutes. However, most of this time is spent on building a giant kernel package with debugging symbols. The assembly of this package can be disabled by commenting out the CONFIG_DEBUG_INFO parameter in the config. Just note that this package requires SystemTap and other useful tools.

In addition to the kernel itself, you can also collect the following documentation:

else there is make pdfdocs and others, seemake help

make htmldocs
chromium-browser Documentation / DocBook / index.html
At the end of the assembly in the child directory, you see something like:

linux-firmware-image-4.4.13-custom_4.4.13-custom-1_amd64.deb
linux-headers-4.4.13-custom_4.4.13-custom-1_amd64.deb
linux-image-4.4.13-custom_4.4.13-custom-1_amd64.deb
linux-image-4.4.13-custom-dbg_4.4.13-custom-1_amd64.deb
linux-libc-dev_4.4.13-custom-1_amd64.deb
We put all deb-packages except the dbg-version of the kernel with the usual sudo dpkg -i and reboot. After rebooting, look at the output of the uname -a command. We are sure that we are really loaded with a new kernel. If there is something wrong with the new kernel, in the bootloader we simply select the one with which the system was booted before. After loading with the old kernel, we quickly remove the packages of the new kernel, and voila - the system returned to its previous state.

Additionally, after loading a new kernel, you can run tests on it:

sudo make kselftest
sudo make kselftest-clean

Congratulations, now you can build a Linux kernel!I worked for many Software Companies in Dubai , and I was amazed to see that most coders and developers do not know these lines of Linux.Keep this article sure you need it

Top comments (0)