手把手教你如何向 Linux 核心提交程式碼

GitChat技術雜談發表於2017-11-23

640.gif?wxfrom=5&wx_lazy=1

本文來自作者 劉盼 在 GitChat 上分享「手把手教你如何向 Linux 核心提交程式碼」,閱讀原文」檢視交流實錄

文末高能

編輯 | 施瓦

開源的力量

說到開源大家都會想到黑客和極客,開源的概念最早也是在極客們推出和推崇的。開源的提倡旨在開放原始碼使之更方便自由的使用和再創作。

隨著這一思想的發展,衍生出諸多的開源協議,比如有GPL,BSD,MIT等。

關於開源的一些故事推薦杜玉傑的 chat 文章《開源縱橫談:谷歌與開源那些事兒》。

豪不誇張的說開源的傳播已經在顛覆傳統軟體的開發模式,推動整個 IT 的進步,圍繞著開源的社群文化也在這個新的時代發光發熱。

值得關注的開源專案實在太多,相信每個軟體行業每個軟體模組都有自己的開源專案。

今天不對開源話題進行交流探討,我們選擇 linux 的核心來手把手教你如何在核心社群提交自己的 patch,讓你輕鬆邁入開源社群的第一步,找到高薪工作,從此走向人生巔峰,迎娶……。

在開源社群提交的每一筆程式碼都有可能對別人影響深遠,所以要細心,細心,再細心。在提交程式碼之前端正態度很重要:

  1. 細心

  2. 聲譽

  3. 謙虛

你提交的每一筆程式碼都有可能給別人帶來巨大的貢獻或者潛在的風險,所以要細心對待你的每一筆 patch。

開源社群圈子其實是比較小的,開源給你帶來的價值不僅僅是程式碼本身,相信在工作中也會潛移默化的給你帶去機會,所以你在開源社群的聲譽就變的尤為重要,你的每一筆程式碼就像做事一樣都體現著做人的本質。

另外社群裡的大牛很多,在你向 maintainer 提出問題的時候,要向學生請教老師問題一樣抱著謙虛的心態,一般來講社群裡的  maintainer 態度都很好,人都很 nice。

以上三點是我在開源社群的經驗,希望對初學者有所幫助。

準備工作

工欲善其事,必先利其器。進入正題之前先準備下需要安裝和配置的環境和工具。首先要安裝 linux 作業系統,有很多發行版。

比如 ubuntu,centos,看個人興趣去選擇,本人比較喜歡 ubuntu,這裡主要以 ubuntu 作業系統為例,給大家演示準備工作。

1. 安裝和配置 msmtp

點選左邊欄的“軟體中心”,在搜尋框中輸入“msmtp”,選擇安裝即可。然後按照如下步驟配置 msmtp:

root@-VirtualBox:~# cd ~/ root@-VirtualBox:~# vi .msmtp.log   //建立log檔案,然後直接退出 root@-VirtualBox:~# vi .msmtprc      //鍵入如下內容 defaults logfile ~/.msmtp.log # gitchat account gitchat protocol smtp host smtp.gitchat.com from peter.liu@gitchat.com user peter.liu@gitchat.com password xxx  //由於single密碼3個月一換,注意同步更新此處 port 25 auth plain tls off #tls_starttls on #tls_trust_file /etc/ssl/certs/ca-certificates.crt #syslog LOG_MAIL # Set a default account account default : gitchat

2. 安裝和配置git 環境

預設的 linux 系統一般都已經安裝好 git。如果沒有,隨便找一本git的書都可以,這裡不詳述。比較好的git資料有:http://git.oschina.net/progit/

在配置使用者名稱的時候,請注意社群朋友習慣用英語溝通,也就是名在前,姓在後。這一點會影響社群郵件討論,因此需要留意。

在配置郵箱時,也要注意。社群會將國內某些著名的郵件伺服器遮蔽。因此建議你申請一個gmail郵箱。舉例配置如下:

git config --global user.name 'Peter Liu' $git config --global user.email ' peter.liu@gitchat.com' $git config --global sendemail.chainreplyto false $git config --global sendemail.smtpserver /usr/bin/msmtp 檢視配置結果的話如下: cat ~/.gitconfig [user]    name = Changbing Xiong    email = peter.liu@gitchat.com [color]    ui = auto [core]    editor = vim [sendemail]    chainreplyto = false    smtpserver = /usr/bin/msmtp

3. 訂閱mailinglist

Linux 開源分支要求開發者上傳 patch 或者 driver 時,需要將郵件抄送給 mailinglist、maintainer 和其他人,首先需要訂閱相關子系統的 mailinglist。

訂閱郵件列表 mailing list://改成自己所要提交程式碼所在子系統的 mailing list,詳見 linux 程式碼根目錄下的 MAINTAINERS 文件

To: majordomo@vger.kernel.org 郵件內容: subscribe linux-media 取消訂閱郵件列表mailing list: To: majordomo@vger.kernel.org 郵件內容: unsubscribe linux-media

注意:訂閱 mailinglist 的郵件不需要標題,請參考如下方式:

# vi subscrible Subject: //冒號後面保留一個空格 //必須空一行,並且該行不要有空格 subscribe linux-media  //在這裡加個回車,然後退出並儲存檔案 #git send-email --from peter.liu@gitchat.com --to      peter.liu@gitchat.com ./subscrible  //驗證一下 #git send-email --from peter.liu@gitchat.com --to    majordomo@vger.kernel.org ./subscrible  

注意:傳送完 subscribe郵件 後,你會收到一封確認郵件,比如我的確認郵件標題為“Confirmationfor subscribe linux-media”,裡面有認證資訊,請按照郵件內容,再發一個認證郵件給 majordomo@vger.kernel.org,如下:

# vi subscrible_auth Subject: auth xxxxxxxx subscribe linux-media peter.liu@gitchat.com # git send-email --from peter.liu@gitchat.com --to majordomo@vger.kernel.org ./subscrible_auth

4. 下載 linux 原始碼

首先開啟以下網頁選取你想要工作的分支:https://www.kernel.org

0?wx_fmt=png

從下載的程式碼裡選取感興趣的模組,你可以在核心原始碼目錄\MAINTAINERS檔案中,找一下相應檔案的維護者,及其git地址。例如,watchdog模組的資訊如下:

WATCHDOGDEVICE DRIVERS   M:      Wim Van Sebroeck <wim@iguana.be>   R:      Guenter Roeck <linux@roeck-us.net>   L:      linux-watchdog@vger.kernel.org   W:      http://www.linux-watchdog.org/   T:      gitgit://www.linux-watchdog.org/linux-watchdog.git   S:      Maintained   F:      Documentation/devicetree/bindings/watchdog/   F:      Documentation/watchdog/   F:      drivers/watchdog/   F:      include/linux/watchdog.h   F:      include/uapi/linux/watchdog.h

其中,git://www.linux-watchdog.org/linux-watchdog.git 是其 git 地址。你可以用如下命令拉取watchdog程式碼到本地:

gitremote add watchdog git://www.linux-watchdog.org/linux-watchdog.git gitfetch --tags watchdog

當然,這裡友情提醒一下,MAINTAINERS 裡面的資訊可能不一定準確,這時候你可能需要藉助 google,或者問一下社群的朋友,或者直接問一下作者本人。

不過,一般情況下,基於linux-next分支工作不會有太大的問題。實在有問題再去打擾作者本人。

5. 閱讀 Documentation/SubmittingPatches,這很重要。

製作補丁

1. 補丁描述

補丁第一行是標題,比較重要。它首先應當是模組名稱。

舉個例子,怎麼找到 drivers/clk/samsung/clk-s3c2412.c 檔案屬於哪個模組?可以試試下面這個命令,看看 drivers/clk/samsung/clk-s3c2412.c 檔案的歷史補丁:

# git log drivers/clk/samsung/clk-s3c2412.c commit 02c952c8f95fd0adf1835704db95215f57cfc8e6 Author:Martin Kaiser <martin@kaiser.cx> Date:   Wed Jan 25 22:42:25 2017 +0100 clk: samsung:mark s3c...._clk_sleep_init() as __init

可以看出模組名稱是 “clk:samsung”。下面是我為這個補丁新增的描述,其中第一行是標題:

clk: samsung: mark symbols static where possible for s3c2410 We get 1 warnings when building kernel withW=1: /dimsum/git/kernel.next/drivers/clk/samsung/clk-s3c2410.c:363:13:warning: no previous prototype for 's3c2410_common_clk_init'[-Wmissing-prototypes] void __init s3c2410_common_clk_init(struct device_node *np, unsigned long xti_f, In fact, this function is only used in thefile in which they are declared and don't need a declaration, butcan be made static. So this patch marks these functions with 'static'.

這段描述是我從其他補丁中拷貝出來的,有幾下幾點需要注意:首先標題中故意新增了“for s3c2410”,以區別於另外兩個補丁。其次“1 warnings”這個單詞中,錯誤的使用了複數,這是因為複製的原因。

接著“/dimsum/git/kernel.next/”這個路徑名與我的本地路徑相關,不應當出現在補丁中。最後警告描述超過了80個字元,但是這是一個特例,這裡允許超過80字元。

這些問題,如果不處理的話,Maintainer 會不高興的!如果 Maintainer 表示了不滿,而你不修正的話,這個補丁就會被忽略。修正後的補丁描述如下:

clk: samsung: mark symbols static wherepossible for s3c2410 We get 1 warning when building kernel withW=1: drivers/clk/samsung/clk-s3c2410.c:363:13:warning: no previous prototype for 's3c2410_common_clk_init'[-Wmissing-prototypes] void__init s3c2410_common_clk_init(struct device_node *np, unsigned long xti_f, In fact, this function is only used in thefile in which they are declared and don't need a declaration, but can be made static. So this patch marks these functions with 'static'.

我們的補丁描述一定要注意用詞,不要出現將“unused”寫為“no used”這樣的錯誤。反覆使用 git add,git commit 將補丁提交到git倉庫。

2. 如何生成補丁

有很多的場景根據不同需求生成補丁,這裡介紹兩種工作中常用遇到的場景:

# git format-patch HEAD^ 0001-au0828-fix-logic-of-tuner-disconnection.patch # cat 0001-au0828-fix-logic-of-tuner-disconnection.patch From cc4f6646ae5eb0d75d56cca62e2d60c1ac8cad66 Mon Sep 17 00:00:00 2001 From: Changbing Xiong <cb.xiong@samsung.com> Date: Tue, 22 Apr 2014 16:10:29 +0800 Subject: [PATCH] au0828: fix logic of tuner disconnection //此處的[PATCH]是工具自動加上的 The driver crashed when the tuner was disconnected while restart stream 。。。。。。。 restart stream operations has been released. Change-Id: Iaa1b93f4d5b08652921069182cdd682aba151dbf //需要通過vim刪除此行 Signed-off-by: peter liu <peter.liu@gitchat.com> --- drivers/media/usb/au0828/au0828-dvb.c |   13 +++++++++++++ 。。。。。。。

上面是將最近一次的修改生成一個 patch,不過注意,如果 patch 中有 Change-Id 行,需要刪除;

3. 檢查補丁

在傳送補丁前,我們需要用指令碼檢查一下補丁:

./scripts/checkpatch.pl 000* --------------------------------------- 0001-clk-samsung-mark-symbols-static-where-possible-for-s.patch --------------------------------------- WARNING: Possible unwrapped commit description (prefer a maximum 75 chars per line) #9: void__init s3c2410_common_clk_init(struct device_node *np, unsigned long xti_f, WARNING: line over 80 characters #29: FILE:drivers/clk/samsung/clk-s3c2410.c:363: +static void __init s3c2410_common_clk_init(struct device_node *np, unsigned long xti_f, total: 0 errors, 2 warnings, 8 lineschecked

請留意輸出警告,其中第一個警告是說我們的描述中,有過長的語句。前面已經提到,這個警告可以忽略。但是第二個警告告訴我們程式碼行超過80個字元了。這是不能忽略的警告,必須處理。

4. 補丁的提交

在第一次commit時使用-s,後面修改下面內容時,用—amend即可。

au0828: fix logic of tuner disconnection                     //標題,帶上模組名稱,如au0828                                                                                             //此處必須空一行 The driver crashed when the tuner was disconnected while restart stream operations are still being performed. Fixed by adding a flag in struct au0828_dvb to indicate whether restart stream operations can be performed. If the stream gets misaligned, the work of restart stream operations are usually scheduled for many times in a row. If tuner is disconnected at this time and some of restart stream operations are still not flushed, then the driver crashed due to accessing the resource which used in restart stream operations has been released.                                                                                             //此處必須空一行 Signed-off-by: Changbing Xiong <cb.xiong@samsung.com>    //通過git commit –s 自動生成 # Please enter the commit message for your changes. Lines starting  //從這往下都是自動生成,勿動 # with '#' will be ignored, and an empty message aborts the commit. # On branch tizen # Your branch is ahead of 'origin/tizen' by 1 commit. # # Changes to be committed: #   (use "git reset HEAD^1 <file>..." to unstage) # # modified:   drivers/media/usb/au0828/au0828-dvb.c # modified:   drivers/media/usb/au0828/au0828.h

5. 傳送補丁

生成正確的補丁後,請再次用checkpatch.pl檢查補丁正確性。確保無誤後,可以準備將它傳送給Maintainer了。但是應該將補丁發給誰?這可以用get_maintainer.pl來檢視:

#./scripts/get_maintainer.pl 000* Kukjin Kim <kgene@kernel.org>(maintainer:ARM/SAMSUNG EXYNOS ARM ARCHITECTURES) Krzysztof Kozlowski <krzk@kernel.org>(maintainer:ARM/SAMSUNG EXYNOS ARM ARCHITECTURES) Sylwester Nawrocki<s.nawrocki@samsung.com> (supporter:SAMSUNG SOC CLOCK DRIVERS) Tomasz Figa <tomasz.figa@gmail.com>(supporter:SAMSUNG SOC CLOCK DRIVERS) Chanwoo Choi <cw00.choi@samsung.com>(supporter:SAMSUNG SOC CLOCK DRIVERS) Michael Turquette<mturquette@baylibre.com> (maintainer:COMMON CLK FRAMEWORK) Stephen Boyd <sboyd@codeaurora.org>(maintainer:COMMON CLK FRAMEWORK) linux-arm-kernel@lists.infradead.org(moderated list:ARM/SAMSUNG EXYNOS ARM ARCHITECTURES) linux-samsung-soc@vger.kernel.org (moderatedlist:ARM/SAMSUNG EXYNOS ARM ARCHITECTURES) linux-clk@vger.kernel.org (open list:COMMONCLK FRAMEWORK) linux-kernel@vger.kernel.org (open list)

接下來,可以用git send-email命令傳送補丁了:

git send-email 000* --tokgene@kernel.org,krzk@kernel.org,s.nawrocki@samsung.com,tomasz.figa@gmail.com,cw00.choi@samsung.com,mturquette@baylibre.com,sboyd@codeaurora.org--cc linux-arm-kernel@lists.infradead.org,linux-samsung-soc@vger.kernel.org,linux-clk@vger.kernel.org,linux-kernel@vger.kernel.org

注意哪些人應當作為郵件接收者,哪些人應當作為抄送者。在本例中,補丁是屬於實驗性質的,可以不抄送給郵件列表帳戶。

提醒:你應當將補丁先發給自己,檢查無誤後再發出去。如果你有朋友在社群有較高的威望,也可以抄送給他,必要的時候,也許他能給你一些幫助。這有助於將補丁順利的合入社群。

重要提醒:本文講述的,主要是實驗性質的補丁,用於開啟社群大門。真正重要的補丁,可能需要經過反覆修改,才能合入社群。

工具介紹

本文重在講述向核心提交程式碼的方法論,主要是實驗性質的補丁,用於開啟社群大門。真正重要的補丁,可能需要經過反覆修改,才能合入社群。最後給大家介紹下常用工具的連結:

  • https://www.kernel.org/pub/software/scm/git/docs/git-send-email.html

  • https://www.kernel.org/pub/software/scm/git/docs/git-format-patch.html


近期熱文

沉迷前端,無法自拔的人,如何規劃職業生涯?

區塊鏈在哪些案例上發揮著重大作用

Java 實現 Web 應用中的定時任務

業務團隊如何高效實施自動化測試

入行 AI,如何選個腳踏實地的崗位


《GitChat 達人課:誰動了我的密碼?

0?wx_fmt=jpeg

「閱讀原文」看交流實錄,你想知道的都在這裡

相關文章