Optimizing the Kernel


The kernel is central to SUSE Linux. In many ways, it is Linux. Most of the other software that composes the operating system can (and does) run just as well on any other version of Unix. The kernel is what makes Linux truly unique.

The Linux kernel handles all of the system's hardware I/O and provides a way of managing various aspects of your software setup. Because of this, optimizing it can bring performance dividends, although, as with other tweaks, the measure of the benefits depends on the nature of your system.

Optimizing the kernel is done by compiling your own kernel: taking the source code, adding in (or removing) the options you don't want or need, and then compiling the source code into a binary file. This usually has the effect of making the kernel smaller, freeing more memory, and also reducing the amount of clutter on your hard disk. In addition, some speed boosts come from choosing options specific to your system, such as matching the CPU that is in use.

As you might expect, compiling your own kernel requires significant knowledge of the way the kernel works and shouldn't be undertaken by beginners. There are a great many caveats and "gotchas." However, perhaps surprisingly, the actual task of compiling a new kernel is elementary and usually involves inputting a few commands.

I won't explain each of the configuration options involved in creating your own kernel. It's not an exaggeration to say that explaining such things could easily fill an entire book on its own. However, I will describe how to make simple changes that can improve the kernel's performance.

Caution 

Compiling your own kernel can be productive and educational, but be aware that it's not how SUSE Linux is designed to work. SUSE anticipates that you will use its precompiled kernel. This means that when you perform an update across the Internet, your homemade kernel will probably get replaced with a ready-made one, and you'll need to download the new sources and compile once again from scratch.

Downloading and Installing the Kernel Source Code

To compile your own kernel, you need to download the kernel source code. As with most components of SUSE Linux, this comes in a prepackaged RPM format and can usually be downloaded from the SUSE FTP site. You should avoid the temptation to download a kernel from the official Linux kernel site, www.kernel.org. The SUSE packaged version of the kernel has been tailored to run on your system, and it's therefore the best choice, even if it might lag a few version numbers behind the latest "official" release.

The first step is to find out the exact version number of the kernel currently in use so you can find the kernel source code RPM to match. Issue the following command at the command-line shell, when logged in as root:

cat /proc/version

On my test SUSE Linux system, which had been updated online since it was first installed, the kernel version number was 2.6.5-7.108-default.

You should download and install a matching source file in order to match the other system components and keep up with the security improvements in the latest release.

The source kernel can be downloaded directly from the SUSE FTP update site:

ftp://ftp.suse.com/pub/suse/i386/update/9.1/rpm/i586/

The source file will be named along the lines of kernel-source and will have the standard .rpm filename extension. Don't download files that have an .srpm extension, because they're designed for other uses. The RPM necessary for my test system was kernel-source-2.6.5-7.108.i586.rpm.

After you download the RPM, install it as the root user at the command-line shell, as shown in Figure 31-5:

rpm –Uvh kernel-source-2.6.5-7.108.i586.rpm

image from book
Figure 31-5. By downloading and installing the kernel source, you can build your own optimized kernel.

Note 

The Linux kernel is constantly being improved, and each major release is given a new version number. SUSE Linux 9.1, as supplied with this book, was one of the first Linux distros to incorporate version 2.6 of the kernel. Prior to this, most distros used version 2.4. The changes between the two are multiple, but are mostly hidden and not something you need to be concerned with. However, you should be aware that in the steps here, you're effectively compiling a 2.6 kernel. A 2.4 kernel will present compatibility issues with SUSE Linux 9.1. You can discover your kernel version by opening a Konsole window and typing uname -r.

Configuring the Kernel

Ordinarily, you would now run the kernel configuration program and work your way through the hundreds of settings. However, SUSE kindly provides a ready-made configuration file that you can import and simply tweak in places. This is based on the configuration file used to create the ready-made binary kernels available from the main SUSE site. It's located in the /proc folder and is a compressed file called config.gz. You can copy it to the relevant folder, as well as base your configuration on it, by typing the following as root user:

zcat /proc/config.gz > /usr/src/linux/.config  cd /usr/src/linux  make oldconfig

After this, you can run the graphical configuration program, named xconfig (there are other GUI configuration programs, but xconfig is the most established and easiest to use). However, first you'll need to switch back to an ordinary user and issue the following command, which gives the root user permission to run programs via the GUI:

xhost +

Then switch back to root user and, making sure you're in the /usr/src/linux directory, issue the following command:

make xconfig 

This will start the basic but functional graphical configuration program. On the left is a tree view of the settings that can be altered. At the top right of the program window are the options that can be changed, while helpful messages will appear at the bottom right. These should give you guidance in relatively plain English as to what each option does.

Tip 

There are many guides to configuring the Linux kernel available on the Internet, including a very good one written by The Linux Documentation Project (TLDP). The guide is called The Kernel Rebuild Guide and can be found at www.digitalhermit.com/linux/Kernel-Build-HOWTO.html.

One option definitely worth configuring is the processor type. Click Processor Type and Features in the list on the left, and then select your CPU from the list on the right, as shown in Figure 31-6. Changing this setting will let the kernel take full advantage of your processor. (The ready-made SUSE kernels anticipate working on any processor from a Pentium upwards, and are thus unable to take advantage of architecture improvements in later chips.)

image from book
Figure 31-6. SUSE provides a graphical configuration tool that lets you choose which kernel options you want to include.

Feel free to look through the other options available in the kernel configuration list on the left. A tick in the option box indicates that the configuration choice will be compiled into the kernel program, which is to say it will be included in part of the large kernel file. A circle blob in the option box indicates that the function will be compiled as a module—a separate piece of code that is loaded only when needed. Because the default SUSE configuration file is designed to create a kernel that nearly every type of system can use, a lot of the options will be compiled as modules. There's no harm in this, apart from the fact that each module takes up some disk space.

If you're sure you won't need a particular function, you can remove the tick and/or blob from the option box. For example, in the section relating to network cards, you can do away with most choices if you either don't have a network card or if you know the make and model of the one in your machine. Don't forget to read the helpful guidance at the bottom right. This will tell you if some seemingly unnecessary options are actually vital for the correct operation of the kernel.

If you're not sure if something needs to be compiled into the kernel, consider including it as a module. When the kernel is up and running, modules are loaded and unloaded automatically by the kernel, without the user having to do anything, so this is a safe choice.

Once you're finished, select File ® Save, and then quit the program. After this, it's time to build your kernel.

image from book
Rescuing Your System

Compiling a kernel from scratch is something that only an experienced user should undertake. Although the process is essentially simple, there are many factors that need to be kept in mind, and neglecting even the smallest detail can result in a system that is unable to boot.

If you cannot boot your system, you can reinstall one of the preconfigured SUSE kernels. Boot from the SUSE installation DVD and, on the boot menu, select Rescue System. After choosing your keyboard, a login prompt will appear. Enter root as the user (no password is necessary). You should then find yourself logged in to a SUSE Linux system.

Although you might appear to have an entire file system at your fingertips, you're actually accessing a RAM disk that is held in memory. Therefore, the next step is to create a mount directory and mount the SUSE Linux file system on your hard disk (the rescue system works in a RAM disk, so these changes aren't written to an actual hard disk):

mkdir SUSE  mount /dev/hda1 SUSE

This assumes that your SUSE Linux partition is the first on the disk (hda1). If it's the second or third, then you should substitute hda2 or hda3 as appropriate (you can view your hard disk partition list by typing fdisk –l). Then type the following command, which will make the SUSE folder the root of the file system. This will make it seem as if you have booted into the SUSE partition.

chroot SUSE

The last step is to restore a previous version of the kernel. If you've updated your system since installing SUSE Linux, you'll probably find an updated kernel in the /var/lib/YaST2/you/mnt/i386/update/9.1/rpm/ i586/ directory.

To restore the kernel, a strange maneuver is necessary: the kernel must be uninstalled so that it can be rein-stalled. This is done with the following commands:

rpm –e kernel-default  rpm –Uvh <kernel filename>.rpm
image from book

Building and Installing the Kernel

Building the kernel involves issuing the following commands in sequence, waiting for each to complete before the next is issued:

make clean  make bzImage  make modules

This will take some time to work through, depending on the capabilities of your system. The command-line shell window will also fill with output, but there's no need to worry about this.

Once the commands have finished, the new kernel must be installed. The following commands, issued as root, will do the trick:

cp /boot/vmlinuz /boot/vmlinuz.old  cp /boot/initrd /boot/initrd.old  cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz  cp /usr/src/linux/System.map /boot/

In addition, the kernel modules need to be installed, with the following command:

make modules_install

Also, you should make a new boot RAM disk with the following command:

mk_initrd

The boot RAM disk is necessary to load essential system drivers to help the kernel start.

Configuring the Boot Loader Menu

After this, you should configure the GRUB boot loader menu. An entry needs to be added for the old kernel. Why for the old kernel? Well, if the new kernel doesn't work for whatever reason, then having the old one as a backup will prove invaluable. It will let you boot your system and effect repairs, such as having another attempt at compiling a kernel!

Note 

The boot menu will already be configured to use your new kernel, because when you copied the files, you used the standard filenames for kernels under SUSE Linux. This means that the existing boot menu entry should be enough to boot the new kernel.

To add an entry for the old kernel, start YaST2 and select System, then Boot Loader Configuration. Then type the following beneath the first entry in the list (this assumes that the SUSE Linux partition is the first on your hard disk; if you dual-boot with Windows, it will most likely be the second, so you should replace /dev/hda1 with /dev/hda2):

title Old Linux kernel    kernel (hd0,1)/boot/vmlinuz.old root=/dev/hda1     initrd (hd0,1)/boot/initrd.old

Figure 31-7 shows this addition.

image from book
Figure 31-7. Adding a boot menu entry for your old kernel will let you boot back into SUSE Linux if your new kernel doesn't work.




Beginning SUSE Linux from Novice to Professional
Beginning SUSE Linux: From Novice to Professional
ISBN: 1590594584
EAN: 2147483647
Year: 2005
Pages: 293
Authors: Keir Thomas

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net