Compiling the Linux Kernel

From SuperbHosting.net Support Wiki

Jump to: navigation, search

Contents

Introduction

Purpose

This document was written to assist you in compiling a Linux kernel so you can understand a few of the simple concepts behind the process. Compiling a kernel could be necessary where a machine kernel panics due to poorly written code or limited kernel support for newer hardware. In most cases it is used to improve the performance by cutting out the extra code which you don't need on a webserver or gaming server.

Why?

You may ask "Why don't I simply YUM UPDATE for the latest kernel?" To which I would answer, not all Linux distributions will supply the latest up-to-date kernel and YOU will need to compile it yourself. Fedora and Ubuntu kernels come with too much extra code for graphical interfaces such as Gnome, KDE, or X as you may have noticed with default FC6/FC7 manual installs. Cutting out code for Joystick support, Video for Linux, Gigabit network, APPLEFS, WIFI, Token-Ring, ETC, ETC.. will greatly increase the server's performance. However it takes experience to understand which options to exclude from the kernel's configuration.

When?

The most common situation in which you might need to recompile a kernel for Superb is when a client orders more than 3GB of RAM. Most default kernels are limited to 4GB maximum, which sometimes comes out to only 3.3GB. So we need to recompile the current kernel or compile the latest one from http://kernel.org which is currently 2.6.24.3

Getting Started

Prerequisites

These are some common packages I have come across in my kernel adventures in both Debian and RedHat distributions. Some of these packages may not need to be installed depending on how vanilla your OS could be.

RedHat-ish:

yum install gcc ncurses ncurses-devel

Debian-ish: (Use 'sudo apt-get' for Ubuntu distros)

apt-get update
apt-get install kernel-package libncurses5-dev fakeroot wget bzip2 build-essential

You will need wget on VERY basic Debian net-installs.

Kernel Recompile

(This is a work in progress here, please go on to the next step and use a newer kernel)

Newer Kernel Compile

Find which kernel best suits your needs. If you are experiencing SATA controller issues on some of the newest motherboards try 2.6.23 or later. For this example we will use 2.6.24.2 so download it to your /usr/src/ directory and untar it.

cd /usr/src/
wget http://kernel.org/pub/Linux/kernel/v2.6/Linux-2.6.24.2.tar.gz
tar xvf Linux-2.6.24.2.tar.gz
cd /usr/src/Linux-2.6.24.2/

For both RedHat and Debian distros you should create a symbolic link for the new kernel as /usr/src/Linux.

ln -s /usr/src/Linux-2.6.24.2 /usr/src/Linux
cd /usr/src/linux/

And now you're ready to start configuring the kernel for compiling.

Kernel Configuration

Base for your config

The best way to go about configuring the kernel is to start from a known working configuration. You don't want to take the chance of rebooting into a panicking kernel unless the machine is within your reach. Issue a 'make oldconfig' and press enter for all the new options that appear. This will compare the old config to the new kernel and ask you which of the new features you want to include. Many new options are still experimental so they will default to "No" which is fine.

make oldconfig

If you don't see any questions on your screen followed by a "configuration written to .config then you will have to manually copy the old config file from the /boot directory and start from there. This is not recommended however, because it doesn't automatically enable new features that could be required. This next command is only necessary if 'make oldconfig' does not work.

cp /boot/config-`uname -r` ./.config -f

Next you issue a 'make menuconfig' and take a gander at all of the gizmos and gadgets of the Linux Kernel. (Note that this is only available for 2.6 kernels. 2.4 kernels use the old nasty 'make config' which is no fun).

make menuconfig

Memory Size

This is the main reason you should know how to compile a kernel. Some default kernels will only allow up to 4GB of RAM. Currently I've only noticed this with Fedora Core, Debian and non-servercd CentOS distros. So be sure to browse to the following section:

Processor type and features  --->
(And look for...)
High Memory Support (4GB)  --->

Change this to 64MB to allow for 4GB and higher RAM.

Performance (CPU/Timer/Preemption)

Choosing the proper CPU

You can change the processor type for better efficiency rather than using a generic or "Pentium-Pro" setting. You can see increased performance in Celerons(P4 Based), P4's, EMT64 Xeons and newer Core2Duo/Multicore Xeons if you change it to the most appropriate setting. Browse to the following section:

Processor type and features  --->
(And look for...)
Processor family (Pentium-Pro)  --->

Then choose the most appropriate processor type for the hardware profile. In my example I will be using this option:

(X) Pentium-4/Celeron(P4-based)/Pentium-4 M/older Xeon

Timer Frequency

The theory behind changing the timer frequency is that the OS can cycle more times per second and allow more services to run simultaneously without increasing the load average. Older distros will use 100HZ or 250HZ in server applications. Desktop distros will use 1000HZ for a more fluid desktop feel so imagine what it does in a server environment. Look for the following or similar and change it to 1000HZ:

Timer frequency (250 HZ)  ---> 

You could hack the kernel's code and change the USER_HZ and KERN_HZ to far more than 1000HZ if you find a practical use. It's not recommended in most situations. Some types of gaming servers require 500HZ all the way up to 1500HZ and NEED to be modified manually.

Kernel Preemption

Last setting we should always look for (especially in Fedora and other desktop Kernels) is the Preemption Model. Default Fedora kernels tend to use a "Voluntary kernel" or "Preemptible kernel" for their desktop application. Look for the following...

Preemption Model --->

We want this to be set as "No Forced Preemption" for server applications.

Advanced Performance (Cutting the Fat)

Note: Skip this section if you want to compile a "highmem" kernel only. (This is a work in progress but can still be used as a reference).

Theory

The Linux kernel these days comes with so much extra junk. Most of Fedora Core OS's have many useless features enabled that are only useful for a desktop application. Removing the unused features will keep a small and tidy kernel on disk and in memory.

[ ] or < > is disabled.
[*] or <*> is enabled.
[M] or <M> means it's a module.

Device Drivers

This is the section where you can remove much of the useless bulk you won't need in a Superb SLS server. Browse to the following section.

Device Drivers  --->

Here is a quick list of things you can disable without question. Note that some of these will have a sub menu you have to browse into to disable their features. If you can't turn it off completely, just leave it as <M> for module.

< > Parallel port support  --->
--- Plug and Play support  --->
[ ] Macintosh device drivers  --->
< > ISDN support  --->
< > Telephony support  --->
< > Dallas's 1-wire support  --->
    Multimedia devices  ---> 
    Graphics support  --->
    Sound  --->
< > MMC/SD card support  --->
--- LED Support  ---> 
< > InfiniBand support  --->
[ ] Auxiliary Display support  --->

Next browse to the following section.

 [*] Network device support  --->

We will want to disable every networking device that will not be used in an SLS application. Here is a list of network devices you probably won't need.

[ ]   Ethernet (1000 Mbit)  ---> (Assuming 100Mbit on your NIC)
[ ]   Ethernet (10000 Mbit)  ---> 
[ ]   Token Ring driver support  --->
      Wireless LAN  --->  
[ ]   PCMCIA network device support  --->
[ ]   Wan interfaces support  --->
[ ]   ATM drivers  --->
< >   PPP (point-to-point protocol) support
< >   PLIP (parallel port) support
< >   SLIP (serial line) support
[ ]   Fibre Channel driver support
< >   Traffic Shaper (OBSOLETE)

Use your head while disabling ANY options. You may need "Ethernet (1000 Mbit)" if you're NIC is an Intel 1000GT like many of Superb's internal servers. Or if you're using RAID/LVM for whatever reason make sure to include "Multiple devices driver support (RAID and LVM)"

To Do List (work in progress)

Networking Support

File Systems

Bus Options

Compiling and Installing the Kernel

Compiling

This next step can take between 15 minutes and 90 minutes depending on how much fat you cut and how fast the CPU/RAM/HDD combination is. So let's issue a 'make all' to begin compiling. This will not install anything. (Dual/Quad core xeons are ultra slow with SATA mode set to compatible in the BIOS)

make all

If you see any errors take a quick note of it. Most of the time these errors are for depreciated code which is no longer needed. However if you see anything that might scare you, google the error and find out more. Go back into the menuconfig and disable it if turns out to be a bad error and you know it's not needed by this system.

Installing

Once the compiling has finished issue a 'make modules_install' and a 'make install' if you don't see any errors. This will install the kernel in your /boot directory and add it to your /boot/grub/menu.lst config.

make modules_install
make install

For Debian distros you will need to execute the following to create the ramdisk image in your /boot directory because 'make install' will not do so on it's own.

depmod 2.6.24.2
apt-get install yaird
mkinitrd.yaird -o /boot/initrd.img-2.6.24.2 2.6.24.2
update-grub

When you 'make install' it builds the ramdisk/vmlinuz and adds the new kernel to the grub config but it keeps the old kernel as the default. Use your favorite text editor and change the "default 1" or similar value to 0 which is what your new kernel should be.

vi /boot/grub/menu.lst
------Snippet of an example menu.lst file------
default         0

title           Debian GNU/Linux, kernel 2.6.24.2
root            (hd0,0)
kernel          /boot/vmlinuz-2.6.24.2 root=/dev/sda1 ro 
initrd          /boot/initrd.img-2.6.24.2
savedefault

title           Debian GNU/Linux, kernel 2.6.18-5-686
root            (hd0,0)
kernel          /boot/vmlinuz-2.6.18-5-686 root=/dev/sda1 ro 
initrd          /boot/initrd.img-2.6.18-5-686
savedefault

title           Debian GNU/Linux, kernel 2.6.18-5-686 (single-user mode)
root            (hd0,0)
kernel          /boot/vmlinuz-2.6.18-5-686 root=/dev/sda1 ro single
initrd          /boot/initrd.img-2.6.18-5-686
savedefault
------Snippet of an example menu.lst file------

Reboot & Cleanup

By now you should have rebooted the machine. Next copy the .config file to your boot directory so we have a future reference in case something goes wrong when the server's load picks up.

cp /usr/src/linux-`uname -r`/.config /boot/config-`uname -r`

If the file is not found because of a different kernel naming scheme, no problem. Your .config file is located in /usr/src/linux/.config for RedHat distros and in /usr/src/linux-2.6.24.2/.config for most anything else. Follow the same naming scheme for other "config-" files in your /boot directory to keep it neat. Optionally you may delete your kernel source directory and remove the symbolic link for RedHat distros.

rm -f /usr/src/linux && rm -R -f /usr/src/linux-2.6.24.2

And you're done.

Personal tools