Team LiB
Previous Section Next Section

Generating Patches

All changes to the Linux kernel are distributed in the form of patches, which are the output of the GNU diff(1) program in a form that is readable by the patch(1) program. The simplest way to generate a patch is to have two source trees, one that is the vanilla stock kernel and another that is the stock tree with your modifications. A common scheme is to name the stock tree linux-x.y.z (which is what the source tarball extracts to, initially) and to name your modified tree simply linux. Then, to generate a patch of the two trees, issue the command

diff -urN linux-x.y.z/ linux/ > my-patch

from one directory below your trees. This is typically done somewhere in your home, and not /usr/src/linux, so that you do not need to be root. The -u flag specifies that the unified diff format should be used. Without this, the patch is ugly and not very readable by humans. The -r flag specifies to recursively diff all directories, and the -N flag specifies that new files in the modified tree should be included in the diff. Alternatively, if you need to diff only a single file, you can do

diff -u linux-x.y.z/some/file linux/some/file > my-patch

Note that it is important to always diff the trees from one directory below your source trees. This creates a patch that is very usable by others, even if their directory names differ. To apply a patch made in this format, do

patch -p1 < ../my-patch

from the root of your source tree. In this example, the patch is named my_patch and is created one directory below the current. The -p1 flag specifies that the first directory name is to be stripped from the patch. This enables you to apply a patch regardless of the directory-naming convention used by the patch maker.

A useful utility is diffstat, which generates a histogram of a patch's changes (line additions and removals). To generate the output on one of your patches, do

diffstat -p1 my-patch

It is often useful to include this output when you post a patch to lkml. Because the patch(1) program ignores all lines until a diff is detected, you can even include a short description at the top of the patch itself.

    Team LiB
    Previous Section Next Section