DISCLAIMER: This is just what the name says, notes. This is not official guide.
Kernel compilation involves the following:
- Obtaining source
- Installing essential packages for kernel compilation
- Compiling and Installing
The procedure varies from distro to distro. I will go through steps for a general compilation with distro specific notes added.
1. Obtaining source
Linux kernel source is available from kernel’s homepage https://www.kernel.org/. Some distro specific commands are as follows:
- Gentoo linux:
- Debian linux:
apt-get source linux
- Ubuntu linux:
apt-get source linux-image-$(uname -r)
It’s usually recommended (even by Linus, I guess) to extract the source to user’s home under a directory say, kernel ($HOME/kernel/). But on Gentoo and Slackware, we usually extract to /usr/src/ and configured, compiled and installed as root. I’m usually on Gentoo or Slackware. Never bothered to ask the folks why they do it the way that’s frowned upon. It works. So be it.
2. Installing essential packages
At a minimum we need gcc. Depending on distro we may need other packages as well. For eg., in Debian we need, fakeroot, kernel-package, linux-sources, libncurses5-dev and build-essential. Since in Gentoo everything is compiled and in Slackware recommended install method is to choose ‘full’ all necessary softwares are already present.
For generic compilation, we can start with,
make mrproper (this was two step in v2.4 kernel as “
make dep; make clean“)
Above step is not essential but when executed cleans-up source and deletes any customization including deleting .config file. So don’t forget to backup a working .config file.
Then if we already have a working .config file we can use it as safe point or for upgrade.
Actual configuration is started with any of the following commands:
make config – text based question and answer type, very minimal, best suited for ultra geeks 🙂
make menuconfig – menu driven, ncurses based method, good on any desktop or no desktop. This needs libncurses package
make xconfig – graphical method, Qt based, best fits on KDE desktop
make gconfig – graphical method, GTK based, best fits on GTK based desktops (GNOME, XFCE).
4. Compiling and Installing
Compiling is done with just one command:
This is (now) equivalent of ‘make all’ and ‘make bzImage modules vmlinux’ where,
- vmlinux – uncompressed linux kernel
- bzImage – compressed linux
- modules – any modules compiled.
This will compile linux kernel and create a bzImage under arch/x86/boot/ (assuming we are compiling for x86 or x86_64 platform). We can copy this bzImage file to /boot and update the bootloader. The other method is to run
This will install/copy the following to /boot
- bzImage as vmlinuz-x.y.z
- .config as config-x.y.z
- System.map as System.map-x.y.z
- x-major linux version,
- y-minor version (odd number being development version and even number being stable version) and
- z-patch version
If we have configured some features as modules, we can do
This will install/copy all the modules to /lib/modules/<kernel-version>/ directory.
We can then update our bootloaders and reboot. If all has been done right we will have a working system with new kernel. If not necessary troubleshooting has to be done.
Now we will examine kernel compilation specific to distros.
A) Debian way
Debian way is to create a .deb package for kernel and modules which can then be installed (and maintained) with dpkg. As mentioned before, configuring and compiling as root is discouraged. Only time root is used is to actually install the new kernel and modules. Debian uses a method called ‘fakeroot’ for this purpose. This simulates the root user. Another script called make-kpkg is used which automates make dep; make clean (or make mrproper), make bzImage, make modules etc.
The format for make-kpkg is “
make-kpkg <options> <target>” where target is kernel image usually called kernel_image. Options include,
–initrd : asking to create an initrd
–append-to-version : our name; avoid generic names like 686, x86, amd64 etc which are usually reserved by distro packaging
–revision : how many times compiled/recreated? changes package name not kernel.
So, the Debian way will be something like this:
$ make-kpkg clean : equivalent of make mrproper and perhaps similarly not mandatory.
$ fakeroot make-kpkg --initrd --revision=custom.1.0 kernel_image
This step will create .deb packages which can be installed with dpkg.
# dpkg -i *.deb
B) Ubuntu way
We obtain kernel source with the following command. However note that we need to execute the following as normal user. Otherwise we will not be able to use fakeroot.
$ apt-get source linux-image-$(uname -r)
The necessary packages required for a kernel compilation can be installed in Ubuntu as follows:
$ sudo apt-get build-dep linux-image-$(uname -r)
Now do the following:
$ chmod a+x debian/scripts/*
$ chmod a+x debian/scripts/misc/*
$ fakeroot debian/rules clean
$ fakeroot debian/rules editconfigs
To build kernel, cd to the root of the kernel source tree
$ fakeroot debian/rules clean
$ fakeroot debian/rules binary-headers binary-generic
If build is successful there will be three deb files above the build root directory.
$ cd ..
$ ls *.deb
$ linux-headers-$(uname -r)_all.deb linux-headers-$(uname -r)_amd64.deb linux-image-$(uname -r)_amd64.deb
Install the kernel with dpkg
$ sudo dpkg -i linux-*.deb