Compiling Linux kernel under Ubuntu or Debian

From Thomas-Krenn-Wiki
Jump to navigation Jump to search

For most administrators, it is not a daily taks to compile a Linux kernel. It is more important to know the right tools, when the time comes. The following article shows examples of how to compile the Mainline/Vanilla kernel and the distribution-specific kernel.

Installation of required software

The following packages are required for compiling:[1]

$ sudo apt-get install fakeroot build-essential devscripts libncurses5 libncurses5-dev
$ sudo apt-get build-dep linux

The packages are for the following purposes:

  • fakeroot: tool for the simulation of root-privileges; enables users to generate .deb-archives, that are received by root-files
  • build-essential: list of packages that are required for generating .deb-archives
  • devscripts: development-scripts for Debian developers

The ncurses library only needs to be installed when make menuconfig is used.

Download of Linux kernel source

You can get to the kernel source code via different ways. Distribution-kernels always receive more patches of the respective kernel-team or distribution. The Mainline/Vanilla kernel does not contain these patches. For Ubuntu, there is a Kernel Mapping Table (canonical.com), that represents an assignment between Mainline and Ubuntu kernel.

Traditionally, kernels have mostly been compiled under /usr/src as root. It is currently not required to use root rights for compiling. Therefore, the tar-archive can be unpacked as normal user in the home-directory. The Linux source code is installed via apt as package. However, /usr/src remains in use. [2] Here,root rights are needed again.

Mainline/Vanilla kernel

The Mainline kernel, without patches or distributions, is administrated on kernel.org. A tar-archive, XZ, is compressed availabe for download.

$ wget https://kernel.org/pub/linux/kernel/v3.x/linux-3.17.2.tar.xz
$ tar xf linux-source-3.17.tar.xz

Kernel packages in Repos

The Linux source code is located in the repositories as package. The Linux sources are installed via apt-get

$ apt-cache search linux-source
linux-source - Linux kernel source with Ubuntu patches
linux-source-3.13.0 - Linux kernel source for version 3.13.0 with Ubuntu patches
$ sudo apt-get install linux-source
[...]
/usr/src$ sudo tar xjf linux-source-3.2.tar.bz2

Kernel teams

The steps for compiling the kernel are identical for the tar.gz as well as for the linux-source package. When the kernel is compiled in /usr/src, which is the case at linux-src per default, the root rights are required.

Creation of configuration

The kernel configuration defines how and which parts of the kernel are compiled. It is best to use the available .config file of the distribution for starting. For editing the configuration, there are multiple make targets available.[3]

make menuconfig is an interactive menu for adjusting the kernel config file
$ cp /boot/config-`uname -r` .config
$ make silentoldconfig
$ make menuconfig
  • make silentoldconfig: same as oldconfig, but quietly, additionally update deps
  • Attention: When make localmodconfig is used, all modules, that are not loaded, are deactivated in the configuration! It may be, for example, that the USB-module driver falls out, when there is no USB-medium connected. This make target is only used, if you want to create a customized kernel for the corresponding system.
  • make menuconfig: update current config utilising a menu based program

Compilation and installation

The kernel is either compiled via make or via make deb-pkg. The .deb creates packages and simplifies the installation of the kernel via multiple systems.

make and install

make creates the kernel image bzImage, the targets make modules_install and make install installs the kernel modules, the kernel, the initramfs and performs a GRUB-update.

In the following example, the kernel is compiled via Linux-3.17.2.tar.xz archive:

$ make -j5
  SYSTBL  arch/x86/syscalls/../include/generated/asm/syscalls_32.h
  SYSHDR  arch/x86/syscalls/../include/generated/asm/unistd_32_ia32.h
  SYSHDR  arch/x86/syscalls/../include/generated/asm/unistd_64_x32.h
[...]
  LD [M]  sound/pci/hda/snd-hda-intel.ko
  LD [M]  sound/pci/snd-als300.ko
  LD [M]  sound/soundcore.ko
$ sudo make modules_install
  INSTALL arch/x86/crypto/aes-x86_64.ko
  INSTALL arch/x86/crypto/aesni-intel.ko
[...]
  INSTALL sound/pci/snd-als300.ko
  INSTALL sound/soundcore.ko
  DEPMOD  3.17.2
$ sudo make install
sh ./arch/x86/boot/install.sh 3.17.2 arch/x86/boot/bzImage \
		System.map "/boot"
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 3.17.2 /boot/vmlinuz-3.17.2
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 3.17.2 /boot/vmlinuz-3.17.2
update-initramfs: Generating /boot/initrd.img-3.17.2
run-parts: executing /etc/kernel/postinst.d/pm-utils 3.17.2 /boot/vmlinuz-3.17.2
run-parts: executing /etc/kernel/postinst.d/update-notifier 3.17.2 /boot/vmlinuz-3.17.2
run-parts: executing /etc/kernel/postinst.d/zz-update-grub 3.17.2 /boot/vmlinuz-3.17.2
Generating grub configuration file ...
Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.
Found linux image: /boot/vmlinuz-3.17.2
Found initrd image: /boot/initrd.img-3.17.2
[...]

make deb-pkg

As mentioned, make deb-pkg creates Debian packages after compiling. To install the kernel, it is enough to install the generated packages via dpkg.

In the following example, the linux-source package is installed and the kernel is compiled in /usr/src and therefore, sudo is used:

$ sudo make -j5 deb-pkg LOCALVERSION=-custom KDEB_PKGVERSION=1
  CHK     include/config/kernel.release
  UPD     include/config/kernel.release
make KBUILD_SRC=
  SYSHDR  arch/x86/syscalls/../include/generated/uapi/asm/unistd_32.h
  SYSHDR  arch/x86/syscalls/../include/generated/uapi/asm/unistd_64.h
[...]
dpkg-deb: building package `linux-headers-3.13.11.8-custom' in `../linux-headers-3.13.11.8-custom_1_amd64.deb'.
dpkg-deb: building package `linux-libc-dev' in `../linux-libc-dev_1_amd64.deb'.
dpkg-deb: building package `linux-image-3.13.11.8-custom' in `../linux-image-3.13.11.8-custom_1_amd64.deb'.
dpkg-deb: building package `linux-image-3.13.11.8-custom-dbg' in `../linux-image-3.13.11.8-custom-dbg_1_amd64.deb'.
$ cd ..
$ sudo dpkg -i linux-image-3.13.11.8-custom_1_amd64.deb
[...]
Preparing to unpack linux-image-3.13.11.8-custom_1_amd64.deb ...
Unpacking linux-image-3.13.11.8-custom (1) ...
Setting up linux-image-3.13.11.8-custom (1) ...
update-initramfs: Generating /boot/initrd.img-3.13.11.8-custom
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.13.11.8-custom
Found initrd image: /boot/initrd.img-3.13.11.8-custom
[...]

Other ways to a new kernel

There are also other possibilities to install new kernels without compiling on your own.

Ubuntu

Debian

  • Under Debian, a newer kernel can be installed via Backport-Repository. Attention: Security-updates at backports are only distributed on a best-effort basis![4]
# echo "deb http://http.debian.net/debian wheezy-backports main 
deb-src http://http.debian.net/debian wheezy-backports main" > /etc/apt/sources.list.d/backports.list
# apt-get update
# apt-get install -t wheezy-backports linux-image-amd64 linux-headers-amd64

References


Author: Georg Schönberger

Translator: Alina Ranzinger

Alina has been working at Thomas-Krenn.AG since 2024. After her training as multilingual business assistant, she got her job as assistant of the Product Management and is responsible for the translation of texts and for the organisation of the department.


Related articles

Creation of Sudo User in Debian Linux
Debian GNU/Linux
Perl warning Setting locale failed in Debian