1.3. Patching a Kernel

Incremental upgrades can be applied to an existing source tree. If you have downloaded the linux-2.4.21.tgz kernel source and you want to update to a more recent kernel linux-2.4.22 for example, you must download the patch-2.4.22.gz patch.

1.3.1. Applying the Patch

The patch file attempts to overwrite files in the 2.4.21 tree. One way to apply the patch is to proceed as follows:

cd /usr/src
zcat patch-2.4.22.gz | patch -p0

The -p option can strip any number of directories the patch is expecting to find. In the above example the patch starts with:

--- linux-2.4.21/...
+++ linux-2.4.22/...

This indicates that the patch can be applied in the directory where the linux-2.4.21 is.

However if we apply the patch from the /usr/src/linux-2.4.21 directory then we need to strip the first part of all the paths in the patch. So that

--- linux-2.4.21/arch/arm/def-configs/adsagc     
+++ linux-2.4.22/arch/arm/def-configs/adsagc

becomes

--- ./arch/arm/def-configs/adsagc     
+++ ./arch/arm/def-configs/adsagc

This is done with the -p1 option of patch effectively telling it to strip the first directory.

cd /usr/src/linux-2.4.21
zcat patch-2.4.22.gz | patch -p1

1.3.2. Testing the Patch

Before applying a patch one can test what will be changed without making them:

 
patch -p1 --dry-run  < patchfile

1.3.3. Recovering the Old Source Tree

The patch tool has several mechanisms to reverse the effect of a patch.

In all cases, make sure the old configuration (.config file) is saved. For example, copy the .config file to the /boot directory.

cp .config /boot/config-kernelversion
  1. Apply the patch in reverse

    The patch tool has a -R switch which can be used to reverse all the operations in a patch file

    Example: assuming we have patched the 2.4.21 Linux kernel with patch-2.4.22.gz

    The next command will extract the patch:

    cd /usr/src
    zcat patch-2.4.22.gz | patch -p0 -R
    
  2. You can backup the old changed file to a directory of your choice:

    mkdir oldfiles
    patch -B oldfiles/ -p0 < patch-file
    

    This has the advantage of letting you create a backup patch that can restore the source tree to its original state.

    diff -ur linux-2.4.21 oldfiles/linux-2.4.21  > recover-2.4.21-patch
    
    [Note]Note

    Applying this recover-2.4.21-patch will have the effect of removing the 2.4.22 patch we just applied in the previous paragraph

  3. You can apply the patch with the -b option:

    By default this option keeps all the original files and appends a .orig to them.

    patch -b -p0 < patch-file
    

    The patch can be removed with the following lines:

    for file in $(find linux-2.4.29 | grep orig) 
    do 
    FILENAME=$(echo $file | sed 's/\.orig//')
    mv -f $file $FILENAME
    done
    

1.3.4. Building the New Kernel after a patch

Simply copy the old .config to the top of the source directory.

cp /boot/config-kernelversion /usr/src/linux-kernelversion/.config

Next make oldconfig will only prompt for new features.

make oldconfig
make dep
make clean bzImage modules modules_install