How to make a LINUX patch file and how to patch it

This article is reproduced in https://blog.csdn.net/zhenwenxian/article/details/8393731
Thanks to the original blogger for sharing

How to make a LINUX patch file and how to patch it

After modifying a certain version of the Linux kernel, if you want to release it for others to use, you need to make a patch file for this version.
A patch file, also called a patch, is a text document that contains changes between two different versions of the source code tree.
Patches are created by the diff application.
In order to apply a patch correctly, you need to know which base version the patch was created from and
what new version the patch will change the current source tree to. This information will either be present in the metadata of the patch file, or it may be inferred from the filename.

1, How to make a patch file

For example, based on the kernel kernel, the LCD code is modified, the content before modification is placed in the folder kernel, and the modified content is placed in the folder kernel_new, then the command to make the patch file is zhenwx-desktop:~/sourcecode
/ $ diff -Naur kernel/ kernel_new/ > zwx-lcd.patch

The current directory is /home/zhenwx/sourcecode/, which contains the original kernel directory before modification and the modified directory kernel_new. Execute the
command as diff -Naur kernel/ kernel_new/ > zwx-lcd.patch
The diff command will compare the kernel and The files in the kernel_new directory generate the patch file zwx-lcd.patch,
and then you can publish zwx-lcd.patch to the Internet.
This patch file describes the path information of the file and deletes the content of the first few lines, adds the content on the next few lines, deletes with a minus sign, and adds with a plus sign

2. How to apply a patch and uninstall a patch.
You can use the patch program to apply a patch. The patch program reads a diff (or patch) file and applies
the changes described in the file to the code tree.
Patches in the Linux kernel are generated relative to the parent directory of the directory holding the kernel source code.
This means: the file path in the patch file contains the name of the kernel source directory it is based on (or
other names like "a/" and "b/").
Since this probably doesn't match the name of the kernel source directory on your local machine.
You should switch to your kernel source code directory and remove the first component of the file name path in patch when patching (the -p1 parameter of the patch command can accomplish this task).
To uninstall a previously applied patch, use the -R parameter to apply the patch.
So, if you apply the patch with the following command:
patch -p1

那么你可以像下面这样来卸载掉这个补丁:
 patch -R -p1


For example, after other people download the patch file provided above, they only need to execute zhenwx-desktop in the standard kernel directory :~/sourcecode/kernel/$

patch -p1<…/zwx-lcd.patch

Here it is assumed that zwx-lcd.patch and the kernel folder are in the same directory, and the above command is executed in the kernel folder. If the command zhenwx-desktop:~/sourcecode/$
is executed under the /sourcecode directory

patch -p0<…/zwx-lcd.patch

Detailed explanation of the patch command under linux

Linux patch command

Function Description: Patch files.

Syntax: patch [-bceEflnNRstTuvZ][-B <backup prefix string>][-d <working directory>][-D <symbol>][-F <number of columns>][-g <control value >][-i <patch file>][-o <output file>][-p <stripping level>][-r <reject file>][-V <backup method>][-Y <backup prefix string>][-z <backup suffix string>][–backup-if -mismatch][–binary][–help][–nobackup-if-mismatch][–verbose][original file <patch file>] or path [-p <stripping level>] < [patch file]

Supplementary note: the patch command allows users to modify and update the original file by setting the patch file. If you only modify one file at a time, you can directly issue commands in the command line to execute sequentially. If you cooperate with the method of patching files, you can patch a large number of files at one time, which is also one of the upgrade methods for the core of the Linux system.

Parameters:
 -b or -backup backs up each original file.
 -B<backup prefix string> or --prefix=<backup prefix string> When setting the file backup, the prefix string appended in front of the file name, the string can be a path name.
 -c or --context Interpret patched data into associative differences.
 -d<working directory> or --directory=<working directory> Set the working directory.
 -D<marker> or –ifdef=<marker> Use the specified symbol to mark the changed place.
 -e or --ed Interpret patch data into a description file available to the ed command.
 -E or --remove-empty-files If the content of the output file after patching is blank, remove the file.
 -f or --force The effect of this parameter is similar to specifying the -t parameter, but it will assume that the version of the patched data is a new version.
 -F<Number of columns> or –fuzz<Number of columns> Set the maximum number of columns.
 -g<control value> or --get=<control value> Set to control the patch job with RSC or SCCS.
 -i <patch file> or --input=<patch file> Read the specified patch file for you.
 -l or --ignore-whitespace Ignore tabs and space characters between patched data and input data.
 -n or --normal Interpret patch data as normal differences.
 -N or --forward Ignore patched data that is older than the original file version, or that version of the patched data has already been used.
 -o <output file> or –output=<output file>
 -p<stripping level> or --strip=<stripping level> Set the path names to be stripped.
 -f<rejection file> or --reject-file=<rejection file> Set the name of the file that saves the information related to the rejection of patching. The default file name is .rej.
 -R or --reverse assumes that the patch data is generated by exchanging the old and new files.
 -s or --quiet or --silent Do not display the instruction execution process unless an error occurs.
 -t or --batch automatically skip errors without asking any questions.
 -T or –set-time The effect of this parameter is similar to specifying the -Z parameter, but the local time is the main one.
 -u or --unified Interpret patched data as unified differences.
 -v or –version Display version information.
 -V<backup method> or --version-control=<backup method> After backing up the target file with the -b parameter, a backup character string will be added to the end of the backup file. When the -V parameter is used to specify different backup methods, backup strings with different suffixes will also be generated.
 -Y<backup prefix string> or –basename-prefix=–<backup prefix string> When setting the file backup, the prefix string appended at the beginning of the file base name.
 -z<backup suffix string> or --suffix=<backup suffix string> The effect of this parameter is similar to specifying the -B parameter, the difference is that if the path and file name used by the patch job are src/linux/fs/super .c, after adding the backup/ string, the file super.c will be backed up in the /src/linux/fs/backup directory.
 -Z or --set-utc Change the patched file, and set the access time to UTC.
 --backup-if-mismatch Only back up files when the patched data does not exactly match, and no files are deliberately specified to be backed up.
 --binary Read and write data in binary mode, not through the standard output device.
 --help Online help.
 --nobackup-if-mismatch Do not back up files if the patched data does not exactly match and no files are specifically specified to be backed up.
 --verbose verbosely display the execution of the command.

patch, it is a patching command, there are many usages, see help #man patch
-p parameter determines whether to use the prefix directory information of the read source file name, if no -p parameter is provided, all directory information is ignored,
patch -p0 ("p" refers to the path, and the number after it indicates which part of the path to remove. 0 means not to remove, it is the full path)
patch -p1 (the number 1 after "p" means to remove the first path before, The directory before the first "/", and so on. For
file names such as /usr/src/linux-2.6.11/Makefile, when the -p3 parameter is provided, linux-2.6.11/Makefile will be used as the desired patch document.

For the example of the Linux kernel source code LCD upgrade package just mentioned, assuming that the source code directory is located in /home/zhenwx/sourcecode/kernel, then
use "patch -p0<.../zwx- lcd.patch" can work, and
when the current directory is /home/zhenwx/sourcecode/kernel, using "patch -p1<.../zwx-lcd.patch" can also work normally.

If the name of the first-level directory described in the patch file is different from your own first-level directory, this method will come in handy if you want to remove the first-level directory.

For example, if the directory described in the patch you get is different from yours, the directory information described in the first line of the patch

diff --git a/arch/arm/mach-msm/acpuclock-8064.c b/arch/arm/mach-msm/acpuclock-8064.c

But your directory is neither a nor b, then you have to remove the first layer of the directory, and patch it under the path of your kernel

Use -p1 parameter
—————————————————
Copyright statement: This article is the original article of CSDN blogger "zhenwenxian", following the CC 4.0 BY-SA copyright agreement, please attach the original source for reprinting link and this statement.
Original link: https://blog.csdn.net/zhenwenxian/article/details/8393731

Guess you like

Origin blog.csdn.net/weixin_37778713/article/details/105989031