LinuxHomeNetworking.com Forums - Linux forums for real Linux users

Home  News  Forums  Alerts  Corrections

 

Chapter 33

Modifying The Linux Kernel To Improve Performance

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

In This Chapter

Chapter 33

Modifying The Linux Kernel To Improve Performance

Download and Install The Kernel Sources Package

Kernel Modules

Creating A Custom Kernel

Updating GRUB

Updating The Kernel Using RPMs

Conclusion

 

(c) Peter Harrison, www.linuxhomenetworking.com

 

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Like a government that rules a nation and all its provinces, the Linux kernel is the central program that not only governs how programs interact with one another, but also provides the guidelines on how they should use the computer's core infrastructure, such as memory, disks, and other input/output (I/O) devices for the user's benefit.

Linux drivers, the programs that manage each I/O device, are the staff that keeps all the government departments running. Continuing with the analogy, the more departments you make the kernel manage, the slower Linux becomes. Large kernels also reduce the amount of memory left over for user applications. These may then be forced to juggle their memory needs between RAM and the much slower swap partitions of disk drives, causing the whole system to become sluggish.

The Fedora installation CDs have a variety of kernel RPMs, and the installation process autodetects the one best suited to your needs. For this reason, the Fedora Linux kernel installed on your system is probably sufficient. The installation process chooses one of several prebuilt kernel types depending on the type of CPU and configuration you intend to use (Table 33-1).


 

Table 33-1: Kernels Found On Fedora Installation CDs

Processor Type

Configuration

i586

Single processor

i586

Multiprocessor (SMP)

i686

Single processor

i686

Multiprocessor (SMP)

The Pros And Cons Of Kernel Upgrades

Despite this best fit installation, you may want to rebuild the kernel at times. For example, there is no installation RPM for multiprocessor systems with large amounts of memory. You may also want to experiment in making a high-speed Linux router without support for SCSI, USB, Bluetooth, and sound but with support for a few NIC drivers, an IDE hard drive, and a basic VGA console. This would require a kernel rebuild.

Rebuilding the kernel in a small business environment is usually unnecessary. If your system starts to slow down and you can't afford to replace hardware or are unable to add more RAM, however, you may want to tune the kernel by making it support only necessary functions or updating built-in parameters to make it perform better. Sometimes new features within the new kernel are highly desirable; for example, the version 2.6 kernel has much more efficient data handling capabilities than the older version 2.4, providing new life for old hardware.

Kernel tuning on a production server shouldn't be taken lightly, because the wrong parameters could cause your system to fail to boot, software to malfunction, or hardware peripherals to become unavailable. Always practice on a test system and keep a backup copy of your old kernel. Whenever possible, hire a consultant with kernel experience to help, and use this chapter and other references as a guide to prepare you for what to expect.

This chapter provides only an overview of the steps to take. It won't make you an expert, but it will expose you to the general process and provide greater confidence when you need to research the task with a specialized guide.

Download and Install The Kernel Sources Package

The Fedora Linux installation process installs a precompiled standardized version of your kernel for your system to use. If you need to compile a new customized version of your kernel, you must install the kernel source code files located in the kernel-source RPM package. (See Chapter 6, "Installing RPM Software," for details on downloading and installing.).

 

1.      Start with a Linux system that has a standard kernel. This should be true if you have only used RPMs or the yum utility for kernel updates.

2.      Determine what version of the kernel you are using with the uname command. In this case we are running version 2.6.5-1.358.

 

[root@bigboy tmp]# uname -r

2.6.5-1.358

[root@bigboy tmp]#

 

3.      Install the matching kernel-source package using the rpm command from your CDs or by using the yum utility. You may also have to also install a "C" programming language compiler package such as gcc32 to convert the source text files into an executable kernel.

If you can't wait for the updated RPM to be created, or your system doesn't support them, then you can also consider downloading the latest Linux kernel code from the kernel.org website.

Kernel Modules

Over the years the Linux kernel has evolved, in the past device drivers were included as part of this core program, whereas now they are loaded on demand as modules.

Reasons For Kernel Modules

There are a number of advantages to this new architecture:

o       Updates to a driver, such as a USB controller module, don't require a complete recompilation of the kernel; just the module itself needs recompiling. This reduces the likelihood of errors and ensures that the good, working base kernel remains unchanged.

o       An error in a device driver is less likely to cause a fault that prevents your system from booting. A faulty device driver can be prevented from loading at boot time by commenting out its entry in the /etc/modprobe.conf file, or by using the rmmod command after boot time.  In the past, the kernel would have to be recompiled.

o       Updates to a driver don't require a reboot either, just the unloading of the old and reloading of the new module.

o       You can add new devices to your system without requiring a new kernel, only the new module driver is needed. This adds a great deal of flexibility without a lot of systems administrator overhead.

There are some drivers that will always need to be compiled into the kernel to make sure your system boots correctly. For example, routines for basic system functions used in reading and writing files, are an indispensable integrated part of any kernel.

Loadable kernel modules now include device drivers to manage various types of filesystems, network cards, and terminal devices to name a few. As they work so closely with the kernel, the modules need to be compiled specifically for the kernel they are intended to support. The kernel always looks for modules for its version number in the /lib/modules/<kernel version> directory and permanently loads them into RAM memory for faster access. Some critical modules are loaded automatically, others need to be specified in the /etc/modprobe.conf file.

The kernel recompilation process provides you with the option of compiling only the loadable modules. I won't specifically cover this, but simultaneous recompilation of all modules will be covered as part of the overall recompilation of your kernel.

How Kernel Modules Load When Booting

One question that must come to mind is "How does the kernel boot if the disk controller modules reside on a filesystem that isn't mounted yet?"

As stated in Chapter 7, "The Linux Boot Process," the GRUB boot loader resides on its own dedicated partition and uses the /boot/grub/grub.conf file to determine the valid kernels and their locations. The grub.conf file not only defines the available kernels, but also the location of the root partition and an associated ramdisk image that is automatically loaded into memory and that contains just enough modules to get the root filesystem mounted.

Note: In Fedora Linux, the /boot/grub/grub.conf file can also be referenced via the symbolic link file named /etc/grub.conf.

Modules And The grub.conf File

In this example of the /boot/grub/grub.conf file, the kernel in the /boot directory is named vmlinuz-2.6.8-1.521, its RAM disk image file is named initrd-2.6.8-1.521.img, and the root partition is (hd0,0).

 

#

# File: /boot/grub/grub.conf

#

default=0

timeout=10

splashimage=(hd0,0)/grub/splash.xpm.gz

title Fedora Core (2.6.8-1.521)

        root (hd0,0)

        kernel /vmlinuz-2.6.8-1.521 ro root=LABEL=/

        initrd /initrd-2.6.8-1.521.img

 

The .img file is created as part of the kernel compilation process, but can also be created on demand with the mkinitrd command.

The (hd0,0).disk definition may seem strange, but there is a file that maps the GRUB device nomenclature to that expected by Linux in the /boot/grub/device.map file.

 

#

# File: /boot/grub/device.map

#

(fd0)     /dev/fd0

(hd0)     /dev/hda

 

During the next phase of the boot process, the loaded kernel executes the init program located on the RAM disk, which mounts the root filesystem and loads the remaining modules defined in the /etc/modprobe.conf file before continuing with the rest of the startup process.

Loading Kernel Modules On Demand

It is possible to load add-on modules located under the /lib/modules/<kernel version> directory with the modprobe command. For example, the iptables firewall application installs kernel modules that it uses to execute NAT and pass FTP traffic. In this example: these modules are loaded with the modprobe command with the aid of the /etc/rc.local script.

 

#

# File: /etc/rc.local

#

# Load iptables FTP module when required

modprobe ip_conntrack_ftp

 

# Load iptables NAT module when required

modprobe iptable_nat

 


Kernel module drivers that are referenced by the operating system by their device aliases are placed in the /etc/modprobe.conf file and are loaded automatically at boot time. In the example, you can see that devices eth1 and eth0 use the natsemi and orinoco_pci drivers respectively.

 

#

# /etc/modprobe.conf

#

alias eth1 natsemi

alias eth0 orinoco_pci

 

Linux has a number of commands to help you with modules. The lsmod command lists all the ones loaded. In the example, you can see that iptables, NFS, and the Orinoco drivers are all kernel modules. You can use the modprobe command to load and unload modules or use the insmod and rmmod commands. See the man pages for details.

 

[root@bigboy tmp]# lsmod

Module                  Size  Used by

...

...

iptable_filter          2048  0

ip_tables              13440  1 iptable_filter

...

...

exportfs                4224  1 nfsd

nfs                   142912  0

lockd                  47944  3 nfsd,nfs

autofs4                10624  1

sunrpc                101064  20 nfsd,nfs,lockd

...

...

natsemi                18016  0

orinoco_pci             4876  0

orinoco                31500  1 orinoco_pci

hermes                  6528  2 orinoco_pci,orinoco

...

...

[root@bigboy tmp]#

 

Finally, when in doubt about a device driver, try using the lspci command to take a look at the devices that use your PCI expansion bus. Here you can see that the natsemi module listed in the lsmod command has a high probability of belonging to the 01:08.0 Ethernet controller: device made by National Semiconductor.

 

[root@bigboy tmp]# lspci

...

...

01:07.0 Network controller: Intersil Corporation Prism 2.5 Wavelan chipset (rev 01)

01:08.0 Ethernet controller: National Semiconductor Corporation DP83815 (MacPhyter) Ethernet Controller

01:0c.0 Ethernet controller: 3Com Corporation 3c905C-TX/TX-M [Tornado] (rev 78)

[root@bigboy tmp]#


 

Creating A Custom Kernel

The installation of the kernel sources creates a file called README in the /usr/src/linux-2.6.5-1.358 directory that briefly outlines the steps needed to create a new kernel. Take a look at a more detailed explanation of the required steps.

Make Sure Your Source Files Are In Order

Cleaning up the various source files is the first step. This isn't so important for a first time rebuild, but it is vital for subsequent attempts. You use the make mrproper command to do this; it must be executed in the Linux kernel version's subdirectory located under /usr/src. In this case, the subdirectory's name is /usr/src/linux-2.6.5-1.358.

 

[root@bigboy tmp]# cd /usr/src/linux-2.6.5-1.358/

[root@bigboy linux-2.6.5-1.358]# make mrproper

...

...

...

[root@bigboy linux-2.6.5-1.358]#

 

The ".config" File

You next need to run scripts to create a kernel configuration file called /usr/src/linux-2.6.5-1.358/.config. This file lists all the kernel options you wish to use.

Backup Your Configuration

The .config file won't exist if you've never created a custom kernel on your system before, but fortunately, RedHat stores a number of default .config files in the /usr/src/linux-2.6.5-1.358/configs directory. You can automatically copy the .config file that matches your installed kernel by running the make oldconfig command in the /usr/src/linux-2.6.5-1.358 directory.

 

[root@bigboy tmp]# cd /usr/src/linux-2.6.5-1.358

[root@bigboy linux-2.6.5-1.358]# ls .config

ls: .config: No such file or directory

[root@bigboy linux-2.6.5-1.358]# make oldconfig

...

...

...

[root@bigboy linux-2.6.5-1.358]#

 

If you've created a custom kernel before, the .config file that the previous custom kernel build used will already exist. Copy it to a safe location before proceeding.


 

Customizing The ".config" File

Table 33-2 lists three commands that you can run in the /usr/src/linux-2.6.5-1.358 directory to update the .config file.

 

Table 33-2 Scripts For Modifying The .config File

Command

Description

make config

Text based utility that prompts you line by line. This method can become laborious.

make menuconfig

Text menu based utility.

make gconfig

X-Windows based utility.

 

Each command prompts you in different ways for each kernel option, each of which generally provides you with the three choices shown in Table 33-3. A brief description of each kernel option is given in Table 33-4.

Table 33-3 Kernel Option Choices

Kernel Option Choice

Description

M

The kernel will load the drivers for this option on an as needed basis. Only the code required to load the driver on demand will be included in the kernel.

Y

Include all the code for the drivers needed for this option into the kernel itself. This will generally make the kernel larger and slower but will make it more self sufficient. The "Y" option is frequently used in cases in which a stripped down kernel is one of the only programs Linux will run, such as purpose built home firewall appliances you can buy in a store.

There is a limit to the overall size of a kernel. It will fail to compile if you select parameters that will make it too big.

N

Don't make the kernel support this option at all.

 


 

Table 33-4 Kernel Configuration Options

 

Option

Description

Code maturity level options

Determines whether Linux prompts you for certain types of development code or drivers.

Loadable module support

Support for loadable modules versus a monolithic kernel. Most of the remaining kernel options use loadable modules by default. It is best to leave this alone in most cases.

Processor type and features

SMP, Large memory, BIOS and CPU type settings.

General setup

Support for power management, networking, and systems buses such as PCI, PCMCIA, EISA, ISA

Memory technology devices

Linux subsystem for memory devices, especially Flash devices

Parallel port support

Self explanatory

Plug and Play configuration

Support of the automatic new hardware detection method called plug and play

Block devices

Support for a number of parallel-port-based and ATAPI type devices. Support for your loopback interface and RAM disks can be found here too.

Multidevice support (RAID, LVM)

Support for RAID, 0, 1, and 5, as well as LVM.

Cryptography support (CryptoAPI)

Support for various types of encryption

Networking options

TCP/IP, DECnet, Appletalk, IPX, ATM/LANE

Telephony support

Support for voice to data I/O cards

ATA/IDE/MFM/RLL support

Support for a variety of disk controller chipsets

SCSI support

Support for a variety of disk controller chipsets. Also sets limits on the maximum number of supported SCSI disks and CDROMs.

Fusion MPT support

High speed SCSI chipset support.

I2O device support

Support for specialized Intelligent I/O cards

Network device support

Support for Ethernet, Fibre Channel, FDDI, SLIP, PPP, ARCnet, Token Ring, ATM, PCMCIA networking, and specialized WAN cards.

Amateur Radio support

Support for packet radio

IrDA subsystem support

Infrared wireless network support

ISDN subsystem

Support for ISDN

Old CD-ROM drivers (not SCSI, not IDE)

Support for non-SCSI, non-IDE, non ATAPI CDROMs

Input core support

Keyboard, mouse, and joystick support in addition to the default VGA resolution.

Character devices

Support for virtual terminals and various serial cards for modems, joysticks and basic parallel port printing.

Multimedia devices

Streaming video and radio I/O card support

Crypto Hardware support

Web-based SSL hardware accelerator card support

Console drivers

Support for various console video cards

Filesystems

Support for all the various filesystems and strangely, the native languages supported by Linux.

Sound

Support for a variety of sound cards

USB support

Support for a variety of USB devices

Additional device driver support

Miscellaneous driver support

Bluetooth support

Support for a variety of Bluetooth devices

Kernel hacking

Support for detailed error messages for persons writing device drivers

 

Configure Dependencies

As I mentioned before, the .config file you just created lists the options you'll need in your kernel. In version 2.4 of the kernel and older, the make dep command was needed at this step to prepare the needed source files for compiling. This step has been eliminated as of version 2.6 of the kernel.

Edit The Makefile To Give The Kernel A Unique Name

Edit the file Makefile and change the line "EXTRAVERSION =" to create a unique suffix at the end of the default name of the kernel.

For example, if your current kernel version is 2.6.5-1.358, and your EXTRAVERSION is set to -6-new, your new additional kernel will have the name vmlinuz-2.6.5-6-new.

Remember to change this for each new version of the kernel you create.

Compile A New Kernel

You can now use the make bzImage command to create a compressed version of your new kernel and its companion .img RAM disk file. This could take several hours on a 386 or 486 system. It will take about 20 minutes on a 400MHz Celeron.

 

[root@bigboy linux-2.6.5-1.358]# make bzImage

...

...

...

[root@bigboy linux-2.6.5-1.358]#


 

Build The Kernel's Modules

You can now use the make modules command to create all the modules the kernel needs. This command compiles the modules, but locates them within the Linux kernel source directory tree under the directory /usr/src/. The next step relocates them to where they should finally reside under the /lib/modules/<kernel version> directory.

 

[root@bigboy linux-2.6.5-1.358]# make modules

...

...

...

[root@bigboy linux-2.6.5-1.358]#

 

Install the Kernel Modules

The make modules_install command copies the newly created modules to the conventional module locations. This step allows you to test the module compilation process before placing them in directories that will make them immediately available to the kernel. The splitting of these two steps can make troubleshooting much easier.

 

[root@bigboy linux-2.6.5-1.358]# make modules_install

...

...

...

[root@bigboy linux-2.6.5-1.358]#

 

Copy The New Kernel To The /boot Partition

The kernel and the .img you just created needs to be copied to the /boot partition where all your systems active kernel files normally reside. This is done with the make install command.

This partition has a default size of 100MB, which is enough to hold a number of kernels. You may have to delete some older kernels to create enough space.

 

[root@bigboy linux-2.6.5-1.358]# make install

...

...

...

[root@bigboy linux-2.6.5-1.358]#

 

Here you can see that the new kernel vmlinuz-2.6.5-1.358-new is installed in the /boot directory.

 

[root@bigboy linux-2.6.5-1.358]# ls -l /boot/vmlinuz*

lrwxrwxrwx 1 root root       22 Nov 28 01:20 /boot/vmlinuz -> vmlinuz-2.6.5-1.358-new

-rw-r--r-- 1 root root  1122363 Feb 27  2003 /boot/vmlinuz-2.6.5-1.358

-rw-r--r-- 1 root root  1122291 Nov 28 01:20 /boot/vmlinuz-2.6.5-1.358-new

[root@bigboy linux-2.6.5-1.358]#

 

Updating GRUB

You should now update your /etc/grub.conf file to include an option to boot the new kernel. The make install command does this for you automatically.

In this example, default is set to 1, which means the system boots the second kernel entry, which happens to be that of the original kernel 2.6.5-1.358. You can set this value to 0, which makes it boot your newly compiled kernel (the first entry).

 

default=1

timeout=10

splashimage=(hd0,0)/grub/splash.xpm.gz

title Red Hat Linux (2.6.5-1.358-new)

        root (hd0,0)

        kernel /vmlinuz-2.6.5-1.358-new ro root=LABEL=/

        initrd /initrd-2.6.5-1.358-new.img

title Red Hat Linux (2.6.5-1.358)

        root (hd0,0)

        kernel /vmlinuz-2.6.5-1.358 ro root=LABEL=/

        initrd /initrd-2.6.5-1.358.img

 

Kernel Crash Recovery

Sometimes the new default kernel will fail to boot or work correctly with the new kernel. A simple way of recovering from this is to reboot your system, selecting the old version of the kernel from the Fedora splash screen. Once the system has booted with this stable version, edit the grub.conf file and set the default parameter to point to the older version instead. If this fails, you may want to boot from a CD with the original kernel. You can then try to either reinstall a good kernel RPM or rebuild the failed one over again after fixing the configuration problem that caused the trouble in the first place.

How To Create A Boot CD

The Fedora Core 2 kernel is too big to fit on a floppy disk, so you'll have to create a boot CD instead. Here are the steps.

 

1.      Each installed kernel has a dedicated subdirectory for its modules in the /lib/modules directory. Get a listing of this directory. Here there are two installed kernels; versions 2.6.5-1.358custom and 2.6.8-1.521.

 

[root@bigboy tmp]# ls /lib/modules/

2.6.5-1.358custom  2.6.8-1.521

[root@bigboy tmp]#

 

2.      Select the desired kernel and use the mkbootdisk command to create a CD ISO image named /tmp/boot.iso of one of the kernels, in this case 2.6.8-1.521:

 

[root@bigboy tmp]# mkbootdisk --iso --device /tmp/boot.iso \

                      2.6.8-1.521

 

3.      Burn a CD using the image. This creates a boot CD with the specified kernel, named vmlinuz, and a scaled-down version of the grub.conf configuration file named isolinux.cfg, both located in the isolinux subdirectory of the CD. This example mounts the newly created CD-ROM and takes a look at the isolinux.cfg file to confirm that everything is okay.

 

[root@bigboy tmp]# mount /mnt/cdrom

[root@bigboy tmp]# ls /mnt/cdrom/isolinux/

boot.cat  boot.msg  initrd.img  isolinux.bin  isolinux.cfg   TRANS.TBL  vmlinuz

[root@bigboy tmp]# cat /mnt/cdrom/isolinux/isolinux.cfg

default linux

prompt 1

display boot.msg

timeout 100

label linux

        kernel vmlinuz

        append initrd=initrd.img ro  root=/dev/hda2

[root@bigboy tmp]#

 

When you reboot your system with the CD, the boot process automatically attempts to access your files in the /root partition and boot normally. The only difference being that the kernel used is on the CD.

Updating The Kernel Using RPMs

It is also possible to install a new standardized kernel from an RPM file. As you can see, it is much simpler than creating a customized one.

To create an additional kernel using RPMs, use the command

 

[root@bigboy tmp]# rpm -ivh kernel-file.rpm

 

To replace an existing kernel using RPMs, you need only one line

 

[root@bigboy tmp]# rpm -Uvh kernel-file.rpm

 

Conclusion

Building a customized Linux kernel is probably something that most systems administrators won't do themselves. The risk of having a kernel that may fail in some unpredictable way is higher when you modify it, and, therefore, many system administrators hire experts to do the work for them. After reading this chapter, at least you will have an idea of what is going on when the expert arrives, which can help considerably when things don't go according to plan.

 

 

 

 

LinuxHomeNetworking.com Forums - Linux forums for real Linux users

 

 
Quick HOWTO: Linux Home Networking and Linux Forums
  • ARP broadcast storm in Linux - Networking (Wired & Wireless)
  • using high availability in General - Technical
  • Trying to route via two default gateways in Linux - Networking (Wired & Wireless)
  • Traffic speed in Linux - Networking (Wired & Wireless)
  • WMP54GS not working (anymore) in Linux - Networking (Wired & Wireless)
  • MPlayer on Mandrake in Linux - Software (Applications)
  •  

    Bite Size Tech News
  • Indian firm unveils low-cost Linux-based PCs
  • How to catch a thief with your PC
  • E3 to showcase next-gen consoles
  • Open-Source Software Slashes School I.T. Budgets
  • BBC invests in a world of endless repeats
  • Sony, Toshiba presidents to meet on new DVD format
  • Rapid healing trick falls foul of anti-doping rules
  •