LPI Exam 201 Prep:: Linux Kernel
LPI Exam 201 Prep:: Linux Kernel
29 Aug 2005
Updated 20 Sep 2005
In this tutorial, David Mertz begins preparing you to take the Linux Professional
Institute Intermediate Level Administration (LPIC-2) Exam 201. In this first of eight
tutorials, you learn to understand, compile, and customize a Linux kernel.
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 1 of 15
developerWorks
ibm.com/developerWorks
Topic 201
Linux kernel (weight 5). The focus of this tutorial.
Topic 202
System startup (weight 5).
Topic 203
Filesystem (weight 10).
Topic 204
Hardware (weight 8).
Topic 209
File and service sharing (weight 8).
Topic 211
System maintenance (weight 4).
Topic 213
System customization and automation (weight 3).
Topic 214
Troubleshooting (weight 6).
The Linux Professional Institute does not endorse any third-party exam preparation
material or techniques in particular. For details, please contact info@lpi.org.
Trademarks
Page 2 of 15
ibm.com/developerWorks
developerWorks
kernels.
2.201.3 Patching a kernel (weight 2)
You will learn how to properly patch a kernel for various purposes including
how to implement kernel updates, implement bug fixes, and add support for
new hardware. You will also learn how to properly remove kernel patches from
existing production kernels.
2.201.4 Customizing a kernel (weight 1)
You will learn how to customize a kernel for specific system requirements by
patching, compiling, and editing configuration files as required. You will learn
how to assess requirements for a kernel compile versus a kernel patch as well
as build and configure kernel modules.
This tutorial is one of the few in this series that is about Linux itself, strictly speaking.
That is, a variety of tools for networking, system maintenance, manipulating files and
data, and so on, are important for a working Linux installation and are part of almost
every Linux distribution. But the base kernel -- the bit of software that mediates
between contending programs and access to hardware -- is the software managed
by Linus Torvalds, and that is properly called "Linux itself."
One of the best things about the Linux kernel is that it is Free Software. Not only
have many brilliant people contributed to making the Linux kernel better, but you, as
system administrator, have access to the kernel source code. This gives you the
power to configure and customize the kernel to fit your exact requirements.
Prerequisites
To get the most from this tutorial, you should already have a basic knowledge of
Linux and a working Linux system on which you can practice the commands covered
in this tutorial.
Trademarks
Page 3 of 15
developerWorks
ibm.com/developerWorks
modules. In many or most cases, the base kernel and a large collection of kernel
modules are compiled at the same time and installed or distributed together, based
on the code created by Linus Torvalds or customized by Linux distributors. A base
kernel is always loaded during system boot and stays loaded during all uptime;
kernel modules may or may not be loaded initially (though generally some are), and
kernel modules may be loaded or unloaded during runtime.
The kernel module system allows the inclusion of extra modules that are compiled
after, or separately from, the base kernel. Extra modules may be created either
when you add hardware devices to a running Linux system or are sometimes
distributed by third parties. Third parties sometime distribute kernel modules in
binary form, though doing so takes away your capability as a system administrator to
customize a kernel module. In any case, once a kernel module is loaded, it becomes
part of the running kernel for as long as it remains loaded. Contrary to some
conceptions, a kernel module is not simply an API for talking with a base kernel, but
becomes patched in as part of the running kernel itself.
Kernel files
The Linux base kernel comes in two versions: zImage, which is limited to about 508
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 4 of 15
ibm.com/developerWorks
developerWorks
KB, and bzImage for larger kernels (up to about 2.5 MB). Generally, modern Linux
distributions use the bzImage kernel format to allow inclusion of more features. You
might expect that since the "z" in zImage indicates gzip compression, the "bz" in
bzImage might mean bzip2 compression is used there. However, the "b" simply
stands for "big" -- gzip compression is still used. In either case, as installed in the
/boot/ directory, the base kernel is often renamed as vmlinuz. Generally the file
/vmlinuz is a link to a version names file such as
/boot/vmlinuz-2.6.10-5-386.
There are a few other files in the /boot/ directory associated with a base kernel
that you should be aware of (sometimes you will find these at the file system root
instead). System.map is a table showing the addresses for kernel symbols.
initrd.img is sometimes used by the base kernel to create a simple file system in
a ramdisk prior to mounting the full file system.
Kernel modules
Kernel modules contain extra kernel code that may be loaded after the base kernel.
Modules typically provide one of the following functions:
Device drivers: Support a specific type of hardware
File system drivers: Provide the optional capability to read and/or write a
particular file system
System calls: Most are supported in the base kernel, but kernel modules
can add or modify system services
Network drivers: Implement a particular network protocol
Executable loaders: Parse and load additional executable formats
Trademarks
Page 5 of 15
developerWorks
ibm.com/developerWorks
Archives (kernel.org; see Resources for a link). The provider of your distribution
might also provide its own updated kernel sources that reflect vendor-specific
enhancements. For example, you might fetch and unpack a recent kernel version
with commands similar to these:
Listing 1. Fetching and unpacking kernel
%
%
%
%
cd /tmp/src/
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.12.tar.bz2
cd /usr/src/
tar jxvfy /tmp/src/linux-2.6.12.tar.bz2
You may need root permissions to unpack the sources under /usr/src/. However,
you are able to unpack or compile a kernel in a user directory. Check out kernel.org
for other archive formats and download protocols.
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 6 of 15
ibm.com/developerWorks
developerWorks
base kernel; (2) include it as a kernel module; (3) omit the capability entirely.
Generally, there is no harm (except a little extra compilation time) in creating
numerous kernel modules, since they are not loaded unless needed. For
space-constrained media, you might omit capabilities entirely.
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 7 of 15
developerWorks
ibm.com/developerWorks
Further information
The kernel.org site contains a number of useful links to more information about
kernel features and requirements for compilation. A particularly useful and detailed
document is Kwan Lowe's Kernel Rebuild Guide. You'll find links to both in the
Resources section.
Obtaining a patch
Linux kernel sources are distributed as main source trees combined with much
smaller patches. Generally, doing it this way allows you to obtain a "bleeding edge"
kernel with much quicker downloads. This arrangement lets you apply
special-purpose patches from sources other than kernel.org.
If you wish to patch several levels of changes, you will need to obtain each
incremental patch. For example, suppose that by the time you read this, a Linux
2.6.14 kernel is available, and you had downloaded the 2.6.12 kernel in the prior
section. You might run:
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 8 of 15
ibm.com/developerWorks
developerWorks
bzip2 -d patch2.6.13.bz2
bzip2 -d patch2.6.14.bz2
cd /usr/src/linux-2.6.12
patch -p1 < /path/to/patch2.6.13
patch -p1 < /path/to/patch2.6.14
Once patches are applied, proceed with compilation as described in the prior
section. make clean will remove extra object files that may not reflect the new
changes.
About customization
Much of what you would think of as customizing a kernel was discussed in the
section of this tutorial on compiling a kernel (specifically, the make
[x|menu]config options). When compiling a base kernel and kernel modules, you
may include or omit many kernel capabilities in order to achieve specific capabilities,
run profiles, and memory usage.
This section looks at ways you can modify kernel behavior at runtime.
Trademarks
Page 9 of 15
developerWorks
ibm.com/developerWorks
Linux (and other UNIX-like operating systems) uses a special, generally consistent,
and elegant technique to store information about a running kernel (or other running
processes). The special directory /proc/ contains pseudo-files and subdirectories
with a wealth of information about the running system.
Each process that is created during the uptime of a Linux system creates its own
numeric subdirectory with several status files. Much of this information is
summarized by userlevel commands and system tools, but the underlying
information resides in the /proc/ file system.
Of particular note for understanding the status of the kernel itself are the contents of
/proc/sys/kernel.
CMD
bash
ps
maps
mem
mounts
root@
stat
statm
status
This tutorial cannot address all the information contained in those process
pseudo-files, but just as an example, let's look at part of status:
Listing 6. A look at the status pseudo-file
$ head -12 /proc/17268/status
Name:
bash
State: S (sleeping)
Tgid:
17268
Pid:
17268
PPid:
17266
TracerPid:
0
Uid:
0
0
0
Gid:
0
0
0
FDSize: 256
Groups: 0
VmSize:
2640 kB
VmLck:
0 kB
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
0
0
Trademarks
Page 10 of 15
ibm.com/developerWorks
developerWorks
msgmni
osrelease
ostype
overflowgid
overflowuid
panic
printk
random/
real-root-dev
rtsig-max
rtsig-nr
sem
shmall
shmmax
shmmni
swsusp
sysrq
tainted
threads-max
version
The contents of these pseudo-files show information on the running kernel. For
example:
Listing 8. A look at the ostype pseudo-file
% cat /proc/sys/kernel/ostype
Linux
% cat /proc/sys/kernel/threads-max
4095
Size
8096
25096
34176
34636
29488
16920
13100
103284
33856
31648
74256
14952
83192
Used by
Not tainted
0
1
1 [lp parport_pc]
0 (autoclean) (unused)
0 (autoclean) (unused)
0 (autoclean) (unused)
0 (autoclean) (unused)
4 (autoclean) [sg st sr_mod sd_mod]
0 (autoclean)
0 (autoclean) [sr_mod ide-cd]
8 (autoclean)
1 (autoclean)
0 (autoclean)
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 11 of 15
developerWorks
floppy
8139too
mii
supermount
usb-uhci
usbcore
rtc
ext3
jbd
ibm.com/developerWorks
55132
17160
3832
15296
24652
72992
8060
59916
38972
0
1
0
2
0
1
0
2
2
(autoclean)
(autoclean) [8139too]
(autoclean)
(unused)
[usb-uhci]
(autoclean)
[ext3]
In this case, there are no dependencies. In other cases, dependencies might exist
(which would be handled by modprobe if run without -n). For example:
Listing 11. More modprobe
% modprobe -nv snd-emux-synth
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/drivers/sound/
soundcore.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/core/
snd.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/synth/
snd-util-mem.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/core/seq/
snd-seq-device.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/core/
snd-timer.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/core/seq/
snd-seq.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/core/seq/
snd-seq-midi-event.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/core/
snd-rawmidi.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/core/seq/
snd-seq-virmidi.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/core/seq/
snd-seq-midi-emul.o.gz
/sbin/insmod /lib/modules/2.4.21-0.13mdk/kernel/sound/synth/emux/
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 12 of 15
ibm.com/developerWorks
developerWorks
snd-emux-synth.o.gz
Suppose you want to load a kernel module now. You can use modprobe to load all
dependencies along the way, but to be explicit you should use insmod.
From the information given above, you might think to run, for example, insmod
snd-emux-synth. But if you do that without first loading the dependencies, you will
receive complaints about "unresolved symbols." So let's try Reiser file system
instead, which stands alone:
Listing 12. Loading a kernel module
% insmod reiserfs
Using /lib/modules/2.4.21-0.13mdk/kernel/fs/reiserfs/reiserfs.o.gz
Happily enough, your kernel will now support a new file system. You can mount a
partition, read/write to it, and so on. For other system capabilities, the concept would
be the same.
However, if a kernel module is eligible for removal, rmmod will unload it from
memory, for example:
Listing 14. Unloading modules with no dependencies
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 13 of 15
developerWorks
ibm.com/developerWorks
% rmmod -v reiserfs
Checking reiserfs for persistent data
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 14 of 15
ibm.com/developerWorks
developerWorks
Resources
Learn
At the LPIC Program, find task lists, sample questions, and detailed objectives
for the three levels of the Linux Professional Institute's Linux system
administration certification.
Read Kwan Lowe's Kernel Rebuild Guide for more details on building a kernel.
Find more resources for Linux developers in the developerWorks Linux zone.
Get products and technologies
Get the Linux kernel source at kernel.org, the Linux Kernel Archives.
Order the SEK for Linux, a two-DVD set containing the latest IBM trial software
for Linux from DB2, Lotus, Rational, Tivoli, and WebSphere.
Build your next development project on Linux with IBM trial software, available
for download directly from developerWorks.
Discuss
Participate in the discussion forum for this content.
KernelNewbies.org has lots of resources for people who are new to the kernel:
an FAQ, an IRC channel, a mailing list, and a wiki.
KernelTrap is a Web community devoted to sharing the latest in kernel
development news.
At Kernel Traffic you can find a newsletter that covers some of the discussion
on the Linux kernel mailing list.
Get involved in the developerWorks community by participating in
developerWorks blogs.
Linux kernel
Copyright IBM Corporation 2005. All rights reserved.
Trademarks
Page 15 of 15