Patching the kernel: step by step approch

Patching a kernel refers to upgrading to the latest ‘n greatest by small increments.

Rather than downloading a 25 meg source, it is possible, indeed desirable from your point of view, to simply apply the changes in source code that occur between revisions. These changes can be as little as half a megabyte. A considerable saving in download time.

First and foremost the kernel source you upgrade must be ‘pristine’. It means that at some time previous, you have obtained a pristine kernel source from or one of it’s mirrors. Redhat distribution source code is NOT pristine (nor is any other distro). You must work from pristine source, or, have the ability to reverse patch what ever changes a distributor made. If this is the case for you, bite the bullet, download a pristine kernel, and use these steps for ever more.

“pristine source” is exactly that. Unmodified source code. It does not apply to the compiled binaries or any extraneous additions that may be present in the directory. Thus, you can safely compile pristine source, create and boot the kernel image derived from it, and regardless of all the now extra bits ‘in there’ , it is still, pristine source. This means you can conveniently port your current kernel configuration into the new kernel, without problems. It is not, “source code”.

Now be warned. One slip, one typo on your part, can devastate the source you have. Read every step carefully.

The examples used assume pristine kernel source 2.6.31 located in
and the symlink
/usr/src/linux pointing to it.

One: Obtain the patch.

The patch you require is one more than your current pristine kernel.

download patch-2.6.32.bz2 from

place this patch file into /usr/src

repeat this step for each patch above 2.6.32

Two: Prepare a new linux folder.

Type the following

cd (space) /usr/src

cp (space) -r (space) linux-2.6.31 (space) linux-2.6.32

rm (space) linux

ln (space) -s (space) linux-2.6.32 (space) linux

Three: dry run

bzcat (space) patch-2.6.32.bz2 (space) | (space) patch (space) -p0 (space) –dry-run (space) | (space) more

What this command does is unpack the compressed patch file and automatically apply it to the “linux” directory. the –dry-run is test mode, it won’t damage the source if there’s an error. The verb “more” allows you to check thru the processing looking for a FAIL message. A pristine source will NEVER fail No ifs, no buts. If you get FAILS, you dont have a pristine source, simple as that.

Alternatively, you can type the following
bzcat (space) patch-2.6.32.bz2 (space) | (space) patch (space) -p0 (space) –dry-run (space) > (space) dump

You can then edit the “dump” file to your heart’s content, looking for fail messages.

Four: the Full Monty.

bzcat (space) patch-2.6.32.bz2 (space) | (space) patch (space) -p0

Five: iterative

You are now ready to complle kernel 2.6.32

You do so in the normal manner (see kernel compile step by step on this site)

If however you have a series of patches (say up to 2.6.99) simply iterate thru steps two to four before compiling. Remember that the criteria is to change the symbolic link, thus, the second iteration would be

cp (space) -r (space) linux-2.6.32 (space) linux-2.6.33

rm (space) linux

ln (space) -s (space) linux-2.6.33 (space) linux

and as each patch succeeds

rm linux-2.6.32

Naturally, in the examples above, you would NEVER

rm linux-2.6.31

2.6.31 (in this example) is your lifeline. You can always revert and start over if things went seriously awol.

Six: Revert a patch

To back a patch out, simply add the -R (reverse) switch:

bzcat patch-2.6.xx | patch -p0 -R

Hope this will help.

Bhaskar Chowdhury

About unixbhaskar
GNU/Linux Consultant

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: