如何提交你的第一个 Linux 内核补丁

学习如何做出你的首个 Linux 内核贡献,以及在开始之前你应该知道什么。

Linux 内核是最大且变动最快的开源项目之一,它由大约 53,600 个文件和近 2,000 万行代码组成。在全世界范围内超过 15,600 位程序员为它贡献代码,Linux 内核项目的维护者使用了如下的协作模型。

如何提交你的第一个 Linux 内核补丁

本文中,为了便于在 Linux 内核中提交你的第一个贡献,我将为你提供一个必需的快速检查列表,以告诉你在提交补丁时,应该去查看和了解的内容。对于你贡献的第一个补丁的提交流程方面的更多内容,请阅读 KernelNewbies 的第一个内核补丁教程

为内核作贡献

第 1 步:准备你的系统。

本文开始之前,假设你的系统已经具备了如下的工具:

  • 文本编辑器
  • Email 客户端
  • 版本控制系统(例如:git)

第 2 步:下载 Linux 内核代码仓库。

  1. <span class="kwd">git</span><span class="kwd">clone</span><span class="pun">-</span><span class="pln">b staging</span><span class="pun">-</span><span class="pln">testing</span>
  2. <span class="kwd">git</span><span class="pun">:</span><span class="com">//git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git</span>

复制你的当前配置:

  1. <span class="kwd">cp</span><span class="pun">/</span><span class="pln">boot</span><span class="pun">/</span><span class="pln">config</span><span class="pun">-</span><span class="str">`uname -r`</span><span class="pun">*</span><span class="pun">.</span><span class="pln">config</span>

第 3 步:构建/安装你的内核。

  1. <span class="kwd">make</span><span class="pun">-</span><span class="pln">jX</span>
  2. <span class="kwd">sudo</span><span class="kwd">make</span><span class="pln"> modules_install install</span>

第 4 步:创建一个分支并切换到该分支。

  1. <span class="kwd">git</span><span class="pln"> checkout </span><span class="pun">-</span><span class="pln">b first</span><span class="pun">-</span><span class="kwd">patch</span>

第 5 步:更新你的内核并指向到最新的代码。

  1. <span class="kwd">git</span><span class="pln"> fetch origin</span>
  2. <span class="kwd">git</span><span class="pln"> rebase origin</span><span class="pun">/</span><span class="pln">staging</span><span class="pun">-</span><span class="pln">testing</span>

第 6 步:在最新的代码库上产生一个变更。

使用 make 命令重新编译,确保你的变更没有错误。

第 7 步:提交你的变更并创建一个补丁。

  1. <span class="kwd">git</span><span class="pln"> add </span><span class="pun"><</span><span class="kwd">file</span><span class="pun">></span>
  2. <span class="kwd">git</span><span class="pln"> commit </span><span class="pun">-</span><span class="pln">s </span><span class="pun">-</span><span class="pln">v</span>
  3. <span class="kwd">git</span><span class="pln"> format</span><span class="pun">-</span><span class="kwd">patch</span><span class="pun">-</span><span class="pln">o </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln"> HEAD</span><span class="pun">^</span>

如何提交你的第一个 Linux 内核补丁

主题是由冒号分隔的文件名组成,跟着是使用祈使语态来描述补丁做了什么。空行之后是强制的 signed off 标记,最后是你的补丁的 diff 信息。

下面是另外一个简单补丁的示例:

如何提交你的第一个 Linux 内核补丁

接下来,从命令行使用邮件(在本例子中使用的是 Mutt)发送这个补丁:

  1. <span class="pln">mutt </span><span class="pun">-</span><span class="pln">H </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="lit">0001</span><span class="pun">-<</span><span class="pln">whatever your filename </span><span class="kwd">is</span><span class="pun">></span>

使用 get_maintainer.pl 脚本,去了解你的补丁应该发送给哪位维护者的列表。

提交你的第一个补丁之前,你应该知道的事情

  • Greg Kroah-Hartmanstaging tree 是提交你的 第一个补丁 的最好的地方,因为他更容易接受新贡献者的补丁。在你熟悉了补丁发送流程以后,你就可以去发送复杂度更高的子系统专用的补丁。
  • 你也可以从纠正代码中的编码风格开始。想学习更多关于这方面的内容,请阅读 Linux 内核编码风格文档
  • checkpatch.pl 脚本可以帮你检测编码风格方面的错误。例如,运行如下的命令:perl scripts/checkpatch.pl -f drivers/staging/Android/* | less
  • 你可以去补全开发者留下的 TODO 注释中未完成的内容:find drivers/staging -name TODO
  • Coccinelle 是一个模式匹配的有用工具。
  • 阅读 归档的内核邮件
  • 为找到灵感,你可以去遍历 linux.git 日志去查看以前的作者的提交内容。
  • 注意:不要与你的补丁的审核者在邮件顶部交流!下面就是一个这样的例子:

    错误的方式:

    1. <span class="typ">Chris</span><span class="pun">,</span>
    2. <span class="typ">Yes</span><span class="pln"> let</span><span class="pun">’</span><span class="pln">s schedule the meeting tomorrow</span><span class="pun">,</span><span class="pln"> on the second floor</span><span class="pun">.</span>
    3. <span class="pun">></span><span class="typ">On</span><span class="typ">Fri</span><span class="pun">,</span><span class="typ">Apr</span><span class="lit">26</span><span class="pun">,</span><span class="lit">2013</span><span class="pln"> at </span><span class="lit">9</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> AM</span><span class="pun">,</span><span class="typ">Chris</span><span class="pln"> wrote</span><span class="pun">:</span>
    4. <span class="pun">></span><span class="typ">Hey</span><span class="typ">John</span><span class="pun">,</span><span class="pln"> I had some questions</span><span class="pun">:</span>
    5. <span class="pun">></span><span class="lit">1.</span><span class="typ">Do</span><span class="pln"> you want to schedule the meeting tomorrow</span><span class="pun">?</span>
    6. <span class="pun">></span><span class="lit">2.</span><span class="typ">On</span><span class="pln"> which floor </span><span class="kwd">in</span><span class="pln"> the office</span><span class="pun">?</span>
    7. <span class="pun">></span><span class="lit">3.</span><span class="typ">What</span><span class="kwd">time</span><span class="kwd">is</span><span class="pln"> suitable to you</span><span class="pun">?</span>

    (注意那最后一个问题,在回复中无意中落下了。)

    正确的方式:

    1. <span class="typ">Chris</span><span class="pun">,</span>
    2. <span class="typ">See</span><span class="kwd">my</span><span class="pln"> answers below</span><span class="pun">...</span>
    3. <span class="pun">></span><span class="typ">On</span><span class="typ">Fri</span><span class="pun">,</span><span class="typ">Apr</span><span class="lit">26</span><span class="pun">,</span><span class="lit">2013</span><span class="pln"> at </span><span class="lit">9</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> AM</span><span class="pun">,</span><span class="typ">Chris</span><span class="pln"> wrote</span><span class="pun">:</span>
    4. <span class="pun">></span><span class="typ">Hey</span><span class="typ">John</span><span class="pun">,</span><span class="pln"> I had some questions</span><span class="pun">:</span>
    5. <span class="pun">></span><span class="lit">1.</span><span class="typ">Do</span><span class="pln"> you want to schedule the meeting tomorrow</span><span class="pun">?</span>
    6. <span class="typ">Yes</span><span class="pln"> tomorrow </span><span class="kwd">is</span><span class="pln"> fine</span><span class="pun">.</span>
    7. <span class="pun">></span><span class="lit">2.</span><span class="typ">On</span><span class="pln"> which floor </span><span class="kwd">in</span><span class="pln"> the office</span><span class="pun">?</span>
    8. <span class="typ">Let</span><span class="str">'s keep it on the second floor.</span>
    9. <span class="str">> 3. What time is suitable to you?</span>
    10. <span class="str">09:00 am would be alright.</span>

    (所有问题全部回复,并且这种方式还保存了阅读的时间。)

  • Eudyptula challenge 是学习内核基础知识的非常好的方式。

想学习更多内容,阅读 KernelNewbies 的第一个内核补丁教程。之后如果你还有任何问题,可以在 kernelnewbies 邮件列表 或者 #kernelnewbies IRC channel 中提问。

相关推荐