我们在升级Linux 内核的时候,难免会接触到补丁的知识。下面对如何生成补丁和如何打补丁作讲解。

生成补丁:

制作 hello.c 和 hello_new.c 两个文件如如下所示。

➜  diff ls
hello.c hello_new.c hello_test.c hi.patch
➜ diff cat hello.c
#include "stdio.h"
int main(int argc ,char **argv)
{
printf("Hello World");
}
➜ diff cat hello_new.c
#include "stdio.h"
int main(int argc ,char **argv)
{
printf("Hello World\n");
return ;
}

使用 diff  -uN 命令 进行生成patch

➜  diff diff -uN hello_new.c hello.c > hi.patch
➜ diff cat hi.patch
--- hello_new.c -- ::23.679704122 +
+++ hello.c -- ::59.190677641 +
@@ -, +, @@
#include "stdio.h"
int main(int argc ,char **argv)
{
- printf("Hello World\n");
- return ;
+ printf("Hello World");
}

至此,patch 已经创建完毕。

之后,我们进行使用 patch 命令 对 hello.c 文件进行打补丁。

➜  diff patch -p0 <hi.patch
patching file hello.c
Reversed (or previously applied) patch detected! Assume -R? [n] y
➜ diff cat hello.c
#include "stdio.h"
int main(int argc ,char **argv)
{
printf("Hello World\n");
return ;
}
➜ diff ls
hello.c hello.c.orig hello_new.c hello_test.c hi.patch
➜ diff cat hello.c.orig
#include "stdio.h"
int main(int argc ,char **argv)
{
printf("Hello World");
}
➜ diff cat hello.c
#include "stdio.h"
int main(int argc ,char **argv)
{
printf("Hello World\n");
return ;
}
➜ diff

可见,补丁已经成功应用,并且生成了 .orig 源文件。 --backup-if-mismatch  选项,可以不进行生成orig 文件。

➜  diff patch -p0 --no-backup-if-mismatch < hi.patch
patching file hello.c
Reversed (or previously applied) patch detected! Assume -R? [n] y
➜ diff ls
hello.c hello_new.c hello_test.c hi.patch
➜ diff cat hello.c
#include "stdio.h"
int main(int argc ,char **argv)
{
printf("Hello World\n");
return ;
}

diff 和 patch 命令介绍:

1、diff
--------------------
NAME
       diff - find differences between two files
SYNOPSIS
       diff [options] from-file to-file
--------------------
简单的说,diff的功能就是用来比较两个文件的不同,然后记录下来,也就是所谓的diff补丁。语法格式:diff 【选项】 源文件(夹) 目的文件(夹),就是要给源文件(夹)打个补丁,使之变成目的文件(夹),术语也就是“升级”。下面介绍三个最为常用选项:
-r 是一个递归选项,设置了这个选项,diff会将两个不同版本源代码目录中的所有对应文件全部都进行一次比较,包括子目录文件。
-N 选项确保补丁文件将正确地处理已经创建或删除文件的情况。
-u 选项以统一格式创建补丁文件,这种格式比缺省格式更紧凑些。
2、patch
------------------
NAME
       patch - apply a diff file to an original
SYNOPSIS
       patch [options] [originalfile [patchfile]]
       but usually just
       patch -pnum <patchfile>
------------------
简单的说,patch就是利用diff制作的补丁来实现源文件(夹)和目的文件(夹)的转换。这样说就意味着你可以有源文件(夹)――>目的文件(夹),也可以目的文件(夹)――>源文件(夹)。下面介绍几个最常用选项:

-pnum or --strip=num
Strip the smallest prefix containing num leading slashes from each file name found in the patch file. A sequence of one or more adjacent slashes is counted as a
single slash. This controls how file names found in the patch file are treated, in case you keep your files in a different directory than the person who sent out
the patch. For example, supposing the file name in the patch file was

/u/howard/src/blurfl/blurfl.c

setting -p0 gives the entire file name unmodified, -p1 gives

u/howard/src/blurfl/blurfl.c

without the leading slash, -p4 gives

blurfl/blurfl.c

and not specifying -p at all just gives you blurfl.c. Whatever you end up with is looked for either in the current directory, or the directory specified by the -d
option.

具体的使用说明,可以使用 man diff 和 man patch 命令来进行查看。

保持更新,转载请注明出处。

Linux 补丁生成与使用的更多相关文章

  1. Linux下生成patch和打patch

    转自:http://blog.csdn.net/dl0914791011/article/details/17299103 通过diff工具生成补丁, patch工具打上补丁. 在使用diff之前, ...

  2. linux下生成core dump文件方法及设置

    linux下生成core dump文件方法及设置    from:http://www.cppblog.com/kongque/archive/2011/03/07/141262.html core ...

  3. random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串

    openssl rand -hex n (n is number of characters) LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head ...

  4. linux c生成唯一文件名称

    linux c生成唯一文件名称可用mktemp()或mkstemp()函数

  5. 【linux】在linux上生成SSH-key 简单原理介绍+生成步骤

    1.首先什么是SSH Secure Shell (SSH) 是一个允许两台电脑之间通过安全的连接进行数据交换的网络协议.通过加密保证了数据的保密性和完整性.SSH采用公钥加密技术来验证远程主机,以及( ...

  6. linux 模拟生成 CAN 设备

    /************************************************************************************** * linux 模拟生成 ...

  7. 1.Linux下生成密钥

    1.Linux下生成密钥 ssh-keygen的命令手册,通过”man ssh-keygen“命令: 通过命令”ssh-keygen -t rsa“ 生成之后会在用户的根目录生成一个 “.ssh”的文 ...

  8. Linux中生成Core Dump系统异常信息记录文件的教程

    Linux中生成Core Dump系统异常信息记录文件的教程 http://www.jb51.net/LINUXjishu/473351.html

  9. linux c 生成uuid

    /********方法一**********/#include <stdio.h> #include <stdlib.h> #include <string.h> ...

随机推荐

  1. Selenium3自动化问题二:各chrome版本对应的chromedriver版本

    一:问题说明 最近用到selenium3在火狐浏览器中执行自动化脚本,遇到了一些问题,最后解决方案中占比最多的就是浏览器和驱动版本不一致导致的,故这里给出chrome.firefox驱动的不同版本对应 ...

  2. Tomcat学习总结(4)——基于Tomcat7、Java、WebSocket的服务器推送聊天室

    前言           HTML5 WebSocket实现了服务器与浏览器的双向通讯,双向通讯使服务器消息推送开发更加简单,最常见的就是即时通讯和对信息实时性要求比较高的应用.以前的服务器消息推送大 ...

  3. linux-openvpn

    1.安装openvpn 1)安装需要的依赖,需要用到epel源 #yum -y install  epel-release 修改epel.repo文件enabled=1 #yum install ea ...

  4. tcp/ip通信中tcp头部结构tcphdrp->check校验计算

    通过raw socket修改通信数据后,可通过函数 set_tcp_checksum1(iph); 重新校验计算iph->check值 在http://www.cnblogs.com/dpf-1 ...

  5. UVa 10562 Undraw the Trees(递归遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...

  6. Dockerfile构建镜像

    Dockerfile构建镜像的步骤: 从基础镜像运行一个容器 执行一条指令,对容器做出修改 执行类似docker commit的操作,提交一个新的镜像层 再基于刚提交的镜像运行一个新的容器 执行Doc ...

  7. 在net中json序列化与反序列化

    准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则: 数据在键值对中 数据由逗号分隔 花括 ...

  8. [日常] PHP与Mysql测试kill慢查询并检验PDO的错误模式

    <?php try{ //1. pdo的错误模式,抛出异常,不记录到php的error日志,不影响代码继续运行, $opts=array( PDO::ATTR_ERRMODE => PDO ...

  9. 【Java基础】5、java中的匿名内部类

    匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 实例1:使用匿名内部类来实现抽象方 ...

  10. Charlie's Change(完全背包+路径记忆)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3176   Accepted: 913 D ...