Some problems with Linux patching

Source: Internet
Author: User

Linuxpatchlinux Kernel Document CommandheaderOne interesting feature of Unix-like operating systems is the source-level patch pack. On Windows we patched all to run an executable program, and then we can finish the patch, which is very convenient for the end-user, but for those of us who are curious about the Linux fans a little bit of fun, because we do not know how to do it. And the Linux patch is much more interesting, we first get the program source code and corresponding patch files, and then to the source codes to patch, generate new source files. And then compile this new source code file, you get a patch of the new program. If you don't know how to patch now, it doesn't matter, let's try it, because when I started writing this article, I wouldn't patch my program.

To be exact, this article was not written by me, but I was translated and modified according to an English document written by Daniel P. Bovet, Marco Cesati, and Cosimo Comella. I do not want to be suspected of plagiarism.
Anyway


Interpreting patch files
We can use the diff command to add parameter-run to compare two files and generate a patch file. This patch file will list the differences between the two different versions of the file. We will use a specific example to explain the patch file generated by the diff command.

Assume: We are interested in checking the differences between the two different versions of linux-2.2.13 and linux-2.2.14.
In the first step, we use the following command:

Make Distclean

This allows you to delete all non-text files in two source code directories.
Then we go on to the second step:
*****************************************************************************


Diff-run linux-2.2.13 linux-2.2.14 >/tmp/patch-2.2.14

*****************************************************************************
COMMAND Execution:
-R is a recursive option that sets this option, and diff will compare all of the corresponding files in the two different versions of the source code directory, as you might have expected, and the comparison includes files from subdirectories.
The-n option indicates that if a file exists in a directory, it must be considered to be in this directory, even if the file is an empty file in the corresponding directory. (for example, if there is such a file in the old version, but in the new version of the file is removed, then the diff will still record it, we finished the patch, in the new version of the code, the old version of the file name will still exist, but it is an empty file)
The-u option indicates that a uniform output format is being used.

Let's take a look at the patch file/tmp/patch-2.2.14 generated after the redirect, and here's some of the patch information we extracted from the file:
*****************************************************************************

Diff-run LINUX-2.2.13/ARCH/I386/KERNEL/SIGNAL.C
Linux-2.2.14/arch/i386/kernel/signal.c

Here the first version of the name, linux-2.2.13 is the reference version (that is, the old version), all the problems found in the linux-2.2.14 (new version) are related to the first release.
*****************************************************************************
DIFF HEADER:
The diff command records the first creation time of these two files in a patch file, as follows:
*****************************************************************************
LINUX-2.2.13/ARCH/I386/KERNEL/SIGNAL.C Tue June 8 01:14:20 1999
---linux-2.2.14/arch/i386/kernel/signal.c Sun Jan 23 17:29:25 2000
*****************************************************************************
DIFF BODY:
The diff command found 3 types of differences between the two files.

A) Add (addition):
This line was not in the previous version of the file, but was added to the new version of the file.
b) Replacement (replacement):
The successive lines in the old version of the file were replaced with successive lines in the new version file.
c) Delete (deletion):
A line in the old version of the file no longer appears in the new version file.

In each case, the line number that changed will be prompted.
Let's explain the symbols that diff uses to indicate these three situations:
*****************************************************************************
Add (addition):
Look at the following line in the patch file: (relative to the old and new two versions of the arch/i386/kernel/signal.c file)

419,431 * * *
---419,437----
? Current->exec_domain->signal_invmap[sig]
: SIG),
&FRAME->SIG);
+ if (ERR)
+ goto GIVE_SIGSEGV;

Using the + sign indicated in "&frame->sig"); Add two new lines to the line after this one. These two new lines are the two lines that begin with the + symbol.
419,431 * * * * indicate to the reader that these changes can be viewed from lines 419 to 431 of the old document, and similarly,---419,437----indicate to the reader that these changes can be viewed from 419 to 437 lines from the new document. In this way, the old and the new compare, you can know which places have changed.
However, in the new version of diff, it does not seem to use this method to indicate the new and old file corresponding to the line number, and the use of the symbol @, corresponding to the above example:
419,431 * * *
---419,437----
The new logo that we see may be:
@@-419,431 +419,437 @@
For this kind of expression method, I still do not understand very much, if have any friend to understand more, very welcome you to add this part of content in.
However, there is a point to explain that this line number is not absolutely necessary, in fact, this line number in the source code to patch the time is not used, the hint here is mainly for developers to use when comparing the analysis.

Displacement (replacement):
Look at the following line in the patch file: (relative to the old and new two versions of the arch/i386/kernel/signal.c file)

***************
367,377 * * *
PRINTK ("I/O APIC #%d Version%d at 0x%lx./n",
M->mpc_apicid,m->mpc_apicver,
M->MPC_APICADDR);
! /*
! * We use the first one only currently
! */
! if (ioapics = = 1)
! MP_IOAPIC_ADDR = m->mpc_apicaddr;
}
Mpt+=sizeof (*M);
Count+=sizeof (*M);

Then followed by the following line:

---368,376----
PRINTK ("I/O APIC #%d Version%d at 0x%lx./n",
M->mpc_apicid,m->mpc_apicver,
M->MPC_APICADDR);
! Mp_apics [mp_apic_entries] = *m;
! if (++mp_apic_entries > Max_io_apics)
! --mp_apic_entries;
}
Mpt+=sizeof (*M);
Count+=sizeof (*M);

This indicates the use of the old version of the file! The 5 lines of the symbol identification are used in the new version of the file! The 3 lines of the identity are replaced.
This shows that symbols are meant to be replaced. But why would there be a replacement, rather than deleting it and adding it, I'm not sure. Still need to please know the friend pointed out.

Delete (deletion):
Look at the following line in the patch file: (relative to the old and new two versions of the drivers/net/config.in file)
***************
93,100 * * *
Fi
If ["$CONFIG _experimental" = "Y"]; Then
TriState ' RealTek 8129/8139 (not 8019/8029!) support ' config_rtl8139
-TriState ' SiS, PCI Fast Ethernet Adapter support ' config_sis900
-TriState ' Packet Engines yellowfin gigabit-nic support ' Config_yellowfin
Fi
BOOL ' Other ISA cards ' Config_net_isa
If ["$CONFIG _net_isa" = "Y"]; Then
---94,99----
***************
Two lines that are identified in the old version of the file with the-symbol will no longer appear in the new version of the file, i.e., in the new version of the file, the two lines are deleted.


Build a patch of your own

Now that you've modified and tested a new version of Linux, call it Linux-2.4.5kh3, which is a bit different from the old version of what you're currently using called LINUX-2.4.5KH2.

Now you want to make a patch that can upgrade linux-2.4.5kh2 to Linux-2.4.5kh3. Incidentally, this patch is much smaller than the source code of the Linux kernel.
This patch file is typically loaded with a single floppy disk, so this is useful for upgrading the old operating system kernel on another computer.

There are only two steps to making a patch in nature, as described below:
A) A patch file is generated on computer A (computer A is the one that has both the source code of the new kernel and the source code of the old kernel). and copy the patch file to a floppy disk.
b) on Computer B, read the floppy disk that holds the patch file and use the patch file to upgrade the old kernel on computer B to the new kernel.

Let's take a detailed description below. Steps 1th through 4th describe how to make a patch file and copy it to a floppy disk. Steps 5th through 6th describe how to use the patch file to upgrade the old operating system kernel to a new version.
*****************************************************************************
1th Step: Clean up the source code files of two cores (Files without *.o or. * files)
*****************************************************************************
We assume that the source code paths for the two cores are:
/usr/src/linux-2.4.5kh2 and/usr/src/linux-2.4.5kh3

Run the following command:
Cd/usr/src/linux-2.4.5kh2
Make Distclean
Cd/usr/src/linux-2.4.5kh3
Make Distclean
*****************************************************************************
2nd step: Create a "context diffs" file between the two kernel source versions (this file indicates all the differences between the two different versions of the source code).
*****************************************************************************
Run the following command (first the old kernel, then the new kernel):
Cd/usr/src
Diff-run linux-2.4.5kh2 linux-2.4.5kh3 > Patch-2.4.5kh3
*****************************************************************************
3rd Step: Check the patch file.
*****************************************************************************
Run the following command to view the patch file to make sure it does not contain any garbage:
Less patch-2.4.5kh3
The rubbish mentioned here is a non-asci code garbled, or control character. If you find that there are no text content in the patch file, then there is garbage. This is the 1th to 3rd step we need to re-operate
*****************************************************************************
4th Step: Copy the patch file to a floppy disk.
*****************************************************************************
Mount/flp
Cp/usr/src/patch-2.4.5kh3/flp
Umount/flp

Since our patch files are generally very small, we do not need to compress it. Now we move the floppy disk with the patch to the front of Computer B.
*****************************************************************************
5th step: Read the patch file from the floppy disk.
*****************************************************************************
Cd/usr/src
Mount/flp
Cp/flp/patch-2.4.5kh3 Patch-2.4.5kh3
Umount/flp
*****************************************************************************
6th step: Use this patch file to upgrade the old kernel's source code to the new kernel version.
*****************************************************************************
A) Execute the Marvelous patch command:
Patch-p0 < Patch-2.4.5kh3
The patch command acts on the input patch file PATCH-2.4.5KH3 and upgrades all the files and subdirectories in the corresponding old version of the kernel source code to the corresponding new version (although the old version here must be the same as the old version when we run the diff command on Computer a). In our example, the old kernel source code version is linux-2.4.5kh2. Here the parameter –p0 is used to ensure that the file name is not changed (neither modified nor deleted).
b) Re-name the kernel source code:
MV Linux-2.4.5kh2 Linux-2.4.5kh3



*****************************************************************************
Undo a patch
*****************************************************************************
If you are not satisfied with the new patch, and you want to revert to the earlier kernel version, then we will start with the 6th step described above to reverse the sequence of operations (why there are many reasons to take the undo patch back to the previous version, we do not say):
A) Restore the version of the kernel source code to its previous name:

MV Linux-2.4.5kh3 LINUX-2.4.5KH2

b) execute that wonderful patch command:

Patch-re-p0 < Patch-2.4.5kh3
Here, the-e option shows that if an empty file is found, it is deleted, and the-r option indicates that the "new" file and the "old" file in the patch file are now being swapped (in effect, to patch the new version into the old version, but it is important to change the directory name of the source code. I'm not sure.

Here is a question, why do you want to rename the code directory?

*****************************************************************************
Avoid make Distclean operations
*****************************************************************************
Run a recursive diff without make Distclean, as described in document/usr/src/linux/documentation/submittingpatches, using the following command:
Diff-run-x dontdiff linux-2.4.5kh2 linux-2.4.5kh3 > Patch.diff
Dontdiff is an executable file that can be downloaded from the following URL:
Http://www.moses.uklinux.net/patches/dontdiff
But, as far as I'm concerned, I still don't know why I'm doing this, or what the purpose of making Distclean is. If any of the friends know, thank you very much for telling me or for adding this part of the text to this document.




Make an official patch file
You have installed a new Linux, we call it Linux-2.4.2. But almost every month, new Linux patches are available that fix some problems or support new hardware or other new features. So, when you install Linux-2.4.2 a few months later, there will be a new stable release, we call it Linux-2.4.5.


The process we will describe explains how you can most easily upgrade your Linux from 2.4.2 to the latest 2.4.5.

The key trick is to use a series of patch files.
*****************************************************************************
No. 0 Step: Understand how the official Linux patch files are made.
*****************************************************************************

Each of the official Linux patch files is a diff file that is generated between all the files in two successive versions of Linux using the Diff tool.

As an example, we assume that this patch file is used to upgrade the Linux-2.4.2 to Linux-2.4.5, which is obtained by the following method:
cd/usr/src/linux-2.4.2
Make Distclean
cd/usr/src/linux-2.4.3
Make Distclean
Ln-s linux-2.4.2 Linux
Diff-run Linux linux-2.4.3 > patch_2.4.3
Gzip patch_2.4.3

These patch files use gzip compression in order to save download time for the Linux distribution site. For example, we generate a patch file--patch_2.4.3.gz, which is a compressed patch file.

The official Linux patch always considers the default source subtree name "Linux". This approach may be a bit confusing, but it allows some (versions) of successive patches to be continuously modified to the source code subtree without modifying the name of the source subtree. (as we can see below)

The names of these patch files are all standard:
patch_2.4.3 records the version differences between linux-2.4.3 and linux-2.4.2.
patch_2.4.4 records the version difference between linux-2.4.4 and linux-2.4.3.
patch_2.4.5 records the version difference between linux-2.4.5 and linux-2.4.4.

In our case, we just need these 3 patch files whose names are patch_2.4.3.gz, patch_2.4.4.gz, and patch_2.4.5.gz to upgrade Linux-2.4.2 to Linux-2.4.5.

*****************************************************************************
1th Step: Upgrade the name of the Linux source code directory and the Linux symbolic connection.
*****************************************************************************

Assume that the path to the kernel source code is:/usr/src/linux-2.4.2/
And all the required compression patches have been stored in the directory/USR/SRC.
Execute the following command:
Cd/usr/src
MV linux-2.4.2 linux-2.4.5
RM Linux
Ln-s linux-2.4.5 Linux

*****************************************************************************
2nd step: Clean up the source code directory (delete intermediate target files and configuration files).
*****************************************************************************
Execute the following command:
CD Linux
Make Distclean
*****************************************************************************
3rd Step: Unzip the patch file.
*****************************************************************************
In our example, execute:

Cd/usr/src
Gunzip patch-2.4.3.gz
Gunzip patch-2.4.4.gz
Gunzip patch-2.4.5.gz
*****************************************************************************
4th Step: Execute the patch command repeatedly.
*****************************************************************************
Execute the following command:
For I in 3 4 5; Do
Patch-p0 < patch_2-4. $i
Done
which
Patch-p0 < Patchfile

The command acts on the input patch file and upgrades all files and subdirectories in the corresponding old version of the kernel source code to the corresponding new version (in our case, the old version of the kernel source code directory is/usr/src/linux). In our example, the old kernel source code version is linux-2.4.5kh2. Here the parameter –p0 is used to ensure that the file name is not changed (neither modified nor deleted).
The target directory is Linux, that is, the old version of the kernel source code after the patch, it was upgraded to a new version of the kernel source code.

Some problems with Linux patching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.