How-to Kernel Compile (with Optional Real-Time (RT) Patch)

Summary

   wget <k_ver>.tar.gz && tar xzf <k_ver>.tar.gz OR git clone <rep>  // Get sources 
   wget http://www.kernel.org/pub/linux/kernel/projects/rt/<>/patch-<>.patch.bz2  // (optional) Get RT patch 
   cd <kernel_src_root_dir>
   bzcat ../patch-<kernel_version>-rt.bz2 | patch -p1   // (optional) Get RT patch 
   cp /boot/config-$(uname -r) .config  // Copy configuration if supported by the current kernel
   make oldconfig
   make menuconfig  // Select Processor type and features -> Preemption model -> Fully Preemptive Kernel (RT)
   make-kpkg clean
   CONCURRENCY_LEVEL=$(getconf _NPROCESSORS_ONLN) fakeroot make-kpkg --initrd --revision=0 kernel_image kernel_headers
   cd .. && sudo dpkg -i linux-image* linux-headers*
   sudo rm /lib/modules/`uname -r`/build  //Fix broken link
   ln -s  /usr/src/linux-headers-`uname -r` /lib/modules/`uname -r`/build

On a Debian-based system, make-kpkg utility not only compiles the kernel but also generates the .deb packages to ease installation.

Prerequisites

   sudo apt-get install kernel-package fakeroot build-essential libncurses5-dev

Switch to a directory (you don't need to have superuser privileges) and get the source from here. You can download the full source file or use git clone to fetch from the repository. Extract source code and enter into the new directory named after the kernel version.

Enter the new directory

    cd <kernel_src_root_dir>

(optional) If you want to compile the full preemptive kernel, you need the RT patch. You can download the appropriate version from | here. The version of the patch should match the kernel version. You have to patch the sources with patch -p1

   bzcat ../patch-<kernel_version>-rt.bz2 | patch -p1

The current kernel configuration can be used by coping the config-<kernel_ver> file to the src_root of the new kernel. This is not supported from all kernels but it is the default case on the latest Debian/Ubuntu kernels

Copy old configuration

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

(optional) Set the new compilation options. If you don't do that the default options are going to be used.

     make oldconfig

(optional) You can specify more options by using the menuconfig utility.

    make menuconfig

Important on RT kernels to set under the

    Processor type and features -> Preemption model -> Fully Preemptive Kernel (RT)

You can compile the kernel by running

    make-kpkg clean
    CONCURRENCY_LEVEL=$(getconf _NPROCESSORS_ONLN) fakeroot make-kpkg --initrd --revision=0 kernel_image kernel_headers

If you encounted an error like

   /etc/kernel/postinst.d/apt-auto-removal: cannot create /etc/apt/apt.conf.d//01autoremove-kernels.dpkg-new: Permission denied 

install the updated kernel-build package to fix the bug.

At this point two packages one directory up should have been created. One for the kernel binaries and one for the kernel headers. You can install finally the new kernel by

    cd .. && sudo dpkg -i linux-image* linux-headers*

An issue when following the make-kpkg is that the resulting kernel points to the headers and source of the directory that you compiled from. E.g. if you used ~/kernel/3.4.10 then under /lib/modules/3.4.10/build points to ~/kernel/3.4.10 instead of /usr/src/linux-headers-3.4.10. That may lead to problems if you modify (e.g. apply an other patch) or move the code to a different location. You should fix the link when you boot to the new kernel by

   sudo rm /lib/modules/`uname -r`/build
   ln -s  /usr/src/linux-headers-`uname -r` /lib/modules/`uname -r`/build

Kernel Configuration

  Offload RCU callback processing from boot-selected CPUs (EXPERIMENTAL (RCU_NOCB_CPU) [N/y/?] y 
  Proc types and features -> Preemption Model ->  FULL_RT
  Proc types and features -> Timer Frequency -> Timer_Freq 1000
  Power Management and ACPI option -> ACPI enable
  Power Management and ACPI option -> ACPI -> enable only Power Management Timer Support
  Power Management and ACPI option -> CPU Freq scaling disable 
  Power Management and ACPI option -> CPU Idle disable 
  Gen Setup -> Timers Subsystem -> High res timer
  Gen Setup -> Timers Subsystem -> Idle dynticks system (tickless idle)

Test Real-Time Kernel

Use cyclictest to check the latency of the RT kernel after installation.

Alternative src compilation and kernel installation procedure (obsolete)

   make bzImage 
   make modules 
   sudo make modules_install 
   sudo make install (or copy bzImage to /boot)
   sudo update-grub

RT Kernel and NVidia graphics card

  • Uninstall the proprietary if there are already installed
   sudo apt-get remove --purge nvidia-*
  • Download driver and rt-patch from | here.
  • Extract files and patch with nvidia-rt
   bash NVIDIA-Linux-x86_64-*.run --extract-only
   cd NVIDIA-Linux-x86_64-*/kernel
   patch -p1 < ../../patch-3.0-rt-nvidia.patch
  • If you want your module to be compiled for each kernel update (hopefully skipping this procedure) install Dynamic Kernel Module Support (DKMS)
    sudo apt-get install dkms
  • Exit all X seesions and run nvidia installer. If you wish approve registration to DKMS
   bash NVIDIA-Linux-x86_64-*/nvidia-installer.sh

Note: if DKMS fail to compile the modules you can update them manually by

   sudo dkms autoinstall -k <kernel_name>

  

zum Seitenanfang