Skip to main content

How to (re)build your kernel

posted onAugust 16, 2000
by hitbsecnews

By: L33tdawg@hackinthebox.org

One of the questions I get asked most of the time by newbies or even users
that have been using Linux for perhaps 3 - 6 months is "How do I go about
building my own kernel" - or a derivative thereof. It seems that building
a kernel is considered by most to be a rather daunting task to say the
least - but never fear, this month, I'll take you step by step through
what you need to do in order to rebuild your kernel. Alright... if you
haven't a clue what the heck a kernel is to begin with - then perhaps you
shouldn't be reading this article.

Either way, the kernel is pretty much the brain of your Linux operating
system - its where everything takes place... from your networking all the
way to the specifications and timings of your video card (more or
less). Perhaps it is for this reason that many fear kernel building - one
false move, and you've pretty much hosed the entire box... well your Linux
partition anyway. But the truth of the matter is, that rebuilding your
kernel isn't brain surgery - as long as you're careful and don't screw up
MAJORLY - you should be alright.

Before we get started, lets just take a quick look at a few reasons why
you would want to be rebuild your kernel (or build a new one for that
matter.)

1.) You have a new piece of hardware that your kernel does not support

2.) You want to use a certain system facility or software package for
which kernel support is disabled

3.) You want to use a smaller kernel image in order to save on disk
space. When disk resources are running out, building a kernel that contains
support for only the features that you require can actually save you quite
a bit of space.

4.) You like to live on the bleeding edge of technology - and you wish to
try out the latest and greatest kernel.

5.) You just want to brag to your friends that you're 31337. *grin*.

For whatever the reason you might have - build a kernel is pretty easy,
and something you will HAVE TO LEARN sooner or later. Might as well be
sooner anyway.

Getting Started

Before we begin to build a new kernel, a few prerequisites must be
met. Firstly you need to have a development environment installed. By this
I'm talking about your C compiler and so on. You also need the source code
for the kernel headers - this one is usually installed by default. You'll
obviously need the kernel source as well. If you need to check what type
of kernel headers and source code you have, you could issue this command
on a Red Hat system :

# rpm -query -a | grep kernel kernel-source kernel-headers

If the kernel source is not present, you'll need to copy them off the CD-ROM - or download them from the net. The CD-ROM method works best
for me, since I don't have a super fast net connection. However if you're
going to build a new kernel - you'll obviously need to download it. If
you're blessed to have high speed net connectivity from your house then
good for you... other than that, I guess you'll need to find another way
around it - Uni net connection?

If you're installing your kernel source from your CD-ROM, then its just a
matter of issuing the following command from your bash prompt.

rpm -install /cdlocation/SRPMS/kernel-source-2.2.x.i386.rpm

cdlocation is the mount point for /dev/cdrom and x just completes your
kernel number. The i386 part should also be replaced by whatever
architecture number your system uses.

When the source is installed, it’s generally placed in
/usr/src/linux-2.2.x. There is usually a directory /usr/src/linux which is
a symbolic link to the target kernel source tree. This link is normally
created when you install the source RPM, but in some cases you need to
create it manually (or just change its target from an older kernel source
tree).

The build

Lets get started then shall we. cd to /usr/src/linux and if you need to
apply any patches to your kernel source, then do so with the following
command

# zcat /patch-location/patcN.gz | patch -p0 -e

N is your patch number. You generally only need patches when adding
additional software facilities obtained from the Internet. The next step
is to save your existing configuration files and then clean up the kernel
source tree.

# cp .config .config-save
# make mrproper

If this is your first time building a kernel from this source then the
file .config may not exsist.

The next step is to specify the characteristics of the kernel, and indicate what hardware and software
support should be included within it. This step is automated, and you have a choice of three methods to
accomplish it... Depending on the parameter you include on the next make command.

# make config

or

#make menuconfig

or

#make xconfig

All of these three commands will create the .config file in the current directory. The file specifies
the settings of a large number of system variables which control various aspects of the kernel behavior
and hardware/software support. The make config command will take you through each option using a series
of text prompts - really inconvenient if you ask me, as if you make a wrong selection, there is no way for
you to go back and make changes - you'll have to start all over again. The make menuconfig and make
xconfig commands are a little better - both are menu based meaning they're a lot easier to navigate and
settings are a whole lot easier to get done. The xconfig command results in the menu being generated
within an X session - the choice is yours.

Most of the settings you can choose from have three possible values - m, y and n. The y and n should be
pretty obvious and the m one means modules. This indicates that support for the item should be included
however it will be provided by a loadable module to the kernel rather than being compiled into the
kernel image itself. Be aware that module support itself must also be enabled via the Loadable Module
Support settings group (this one is generally turned on by default).

Once you have things set up nicely - just save the settings and exit. Save the file to
/usr/src/linux/.config - this is necessary for your kernel build to proceed as expected. Next, you will
have to run two more make commands :

#make dep
#make clean

The first command creates files indicating the dependencies among various components in the kernel build
process, and the second command removes any unneeded files from the source tree prior to the actual
kernel compile.

Lets build

Now we are all set and prepped to build the kernel. The typical command to do so is

#make bzImage

This will produce a bzipped kernel executable file (that is, the file will be compressed by bzip). If
you have added or modified any kernel modules or if this is the first time you have built the kernel
from this source version, you must next build the module executables:

#make modules

After this process completes, you should save the previous versions of the kernel modules you are
building a new kernel with the same version number as the current kernel. You can get this done by
renameing the moduels directory with a command like this :

# mv /lib/modules/2.2.x /lib/modules/2.2.x.save

Once again, x completes the kernel number. Next, its time to install the modules.

# make modules_install

You must perform this step every time you build a module-enabled kernel, whether or not you have
actually built any new modules, since it performs other required tasks needed for module support. Time to
install your kernel.

# cp /boot/vmlinuz /boot/vmlinuz.save
# mv arch/i386/boot/bzImage /boot/new_vmlinuz

First thing we just did was make a copy of the old kernel (it pays to be a little paranoid
sometimes). Then we moved the newly built image to the /boot directory and named it new_vmlinuz. In this
way, you've just protected yourself twice over since you have both the old kernel and a copy of it.

At this point, you'll need to add an entry into /etc/lilo.conf for the new kernel. an easy way to do so
is to copy and existing entry and then change the label= and image= fields. Finally just run the lilo
command to install the new configuration and you're all done! This is done by :

# lilo

Time to reboot and test out this new kernel mate. So just issue the reboot or shutdown -r now command
from your bash prompt and enter the name of the new kernel you compiled at the lilo prompt after the
system reboots. If the system boots without any problems then test out all the system facilities. When
you are ready to make the new kernel the default one, copy of move the new kernel to boot/vmlinuz (or if
this file is a symbolic link, make the link point at the new kernel) and remove the old kernel file
(but not the copy!). You'll need to edit /etc/lilo.conf once again, removing the entry for the new
kernel (which has now become the default kernel) and adding one for the saved copy of the old reliable
kernel - just incase. Then run lilo again to install the configuration.

Well as you can see - building a new kernel isn't really that daunting is it? So go ahead and give it a
try - just don't come yelling to me if you skip some of these steps and you're left with a non-bootable
machine.

Peace.

L33tdawg.

1.) Are you a hacker? - JesterS

2.)X-Mail - JesterS

3.)Getting Under the GUI - Liquid Sphear

4.)Opening Simple Ports on Win X - madirish

5.)Commentary on Napster and the Digital Age -
SiLeRePrAeSes

6.)Commentary on the Political Aspects of the Internet
-
SiLeRePrAeSes

7.)How to (re)build your kernel
- L33tdawg

8.)A 101 Bytez team article for Hackinthebox mag

- OZONE

9.)Examining PE Files - abrams!metaray

Source

Tags

Intel

You May Also Like

Recent News

Friday, November 29th

Tuesday, November 19th

Friday, November 8th

Friday, November 1st

Tuesday, July 9th

Wednesday, July 3rd

Friday, June 28th

Thursday, June 27th

Thursday, June 13th

Wednesday, June 12th

Tuesday, June 11th