Purpose: If you have been using Linux for over two to three years than chances are that you may have been in a situation where you need to modify your Kernel’s Makeconfig or Kconfig files or adding a new device driver for your hardware from your hardware vendor which is not yet into the mainstream kernel.

Once you do all of the above then you may need to generate a patch (for the changes that you just made) so that other users can simply apply that patch rather than going into the kernel sources and manually modify all those files. This blog posting will explain in a simple way how you can do that. Although I think there might be several postings which will explain you how to do that but they may not be clear or may not cover your case specifically.

Let’s start with an example:

I downloaded Kernel source 2.6.24 into my /usr/src/linux directory. Suppose I need to add a framebuffer driver support, say for example, for my VIA’s Unichrome Graphic chipset. I copy the framebuffer driver source directory (provided by VIA) into my kernel directory as follow:
#cp -a /usr/src/fbdev-via-unichrome /usr/src/linux/driver/video/via

Note: The directory /usr/src/linux/driver/video/via did not existed when we installed 2.6.24 kernel source initially.

After this suppose we need to make changes to files Kconfig and Makefile in /usr/src/linux/drivers/video directory.

Say I add the following line in the Makefile:

obj-$(CONFIG_FB_VIA) += via/

and the following line in the Kconfig:

config FB_VIA
tristate “VIA UniChrome/Chrome 9 HC display support”
depends on FB && PCI
select FB_CFB_FILLRECT
select FB_CFB_COPYAREA
select FB_CFB_IMAGEBLIT
select FRAMEBUFFER_CONSOLE
help
This is the frame buffer device driver for the VIA CLE266, CN400, CN700, CN800,
CN896, CX700, PMN800, PMN880, P4M800CE-Pro, P4M890, P4M900, VN800, VN896 and
VX700 chipsets.

The above are examples from the www.viaarena.com website.

That’s it! Our kernel is now ready to be compiled and installed to get the framebuffer support for the VIA graphic chipset. However, what if there are several users who want to do the same since they also happen to have the same VIA graphic chipset and the driver has still not made into the mainstream kernel.

Here are the steps to generate a patch after your making your own Kernel modifications:

Step 1: Download and prepare kernel sources
Download again the 2.6.24 kernel source (same as you did above) against which you want to build the patch and name directory to something like linux-pristine. For example, your /usr/src directory should now look something like this:
# ls -l
total 57316
drwxr-xr-x 20 root root 4096 2008-02-11 03:48 linux-pristine
drwxr-xr-x 20 root root 4096 2008-02-11 03:48 linux-source-2.6.24
lrwxrwxrwx 1 root src 19 2008-04-06 00:20 linux -> linux-source-2.6.24

Basically, you now have two kernel source 2.6.24 directories:

One with changes that you made to incorporate VIA’s framebuffer driver called as linux (a link to linux-source-2.6.24 directory)

and

the other original 2.6.24 kernel sources (straight from www.kernel.org website) that you just downloaded called as linux-pristine.

Step 2: Clean both the kernel directories
We need to make sure that we remove any object or executable files in both the kernel directories. Give the following commands:

debian:/usr/src# cd linux
debian:/usr/src/linux# make mrproper
debian:/usr/src/linux# make clean
debian:/usr/src/linux# cd ../linux-pristine/
debian:/usr/src/linux-pristine# make mrproper
debian:/usr/src/linux-pristine# make clean
debian:/usr/src/linux-pristine#

Step 3: Generate patch
Now give the following commands to create the patch:
debian:# cd /usr/src
debian:/usr/src# diff -Nur linux-pristine/drivers/video/ linux/drivers/video/ > patch-2.6.24-viafb

The format of the diff command is:
diff -options oldfile newfile > patch
where -options = -Nur
oldfile = linux-pristine/drivers/video/
newfile = linux/drivers/video/
patch = patch-2.6.24-viafb

Your patch is now ready and you can view your patch by using your favorite editor like gedit, less, nano, etc.
debian:/usr/src# less patch-2.6.24-viafb

Step 4: Test your patch
We need to make sure that the patch we just generated is correct or now. There is a simple way to do it.

Make a temporary copy of your linux-pristine directory as follow:

debian:/usr/src# cp -r linux-pristine/ /tmp/linux-test-patch

Now patch this kernel source by the patch that you just created in Step 3 above.
debian: cd /tmp/linux-test-patch/
debian:/tmp/linux-test-patch# patch -p1 < /usr/src/patch-2.6.24-viafb

You should be seeing some output as follow:

patching file drivers/video/Kconfig
patching file drivers/video/Makefile
patching file drivers/video/via/accel.c
… … … … … … … … … … … … … … … …
patching file drivers/video/via/via_utility.h
patching file drivers/video/via/vt1622a.c
patching file drivers/video/via/vt1622.c
patching file drivers/video/via/vt1625.c
patching file drivers/video/via/vt1636.c
patching file drivers/video/via/vt1636.h
debian:/tmp/linux-test-patch#

Finally give the following command to test your patch:
debian:/tmp# diff -rq linux-test-patch/drivers/video/ /usr/src/linux/drivers/video/

If your patch was successfully created in Step 3 than you should not see any output after giving the above command. Basically the above command makes sure that there is no difference in any of the files after you patched your kernel.

Note: If you want to see the explanations of the options of the “diff” command, please see the man page:

debian:/ man diff

Step 5: Tell your users how to use your patch
Just ask your users to patch their 2.6.24 kernel by giving the following command:

debian:/usr/src/linux-source-2.6.24# patch -p1 < /usr/src/patch-2.6.24-viafb

Congratulations! You have successfully created a patch that you can distribute to other users who do not want to make changes manually to the kernel sources as you did initially. Treat yourself with something you like to eat or just sit back and relax and enjoy seeing people using your patch.

As usual, please leave a comment/feedback, if you have any.

Be Sociable, Share!