Kbuild 編譯 Linux 內核系列(二)

Author :Wei Yang
作者公眾號:楊小偉的世界
第一次總是讓人激動,但是也常常伴隨著恐懼和害怕。我相信有不少小白希望學習內核,常常第一步沒有開始,就直接栽倒在了出發的地方。
這篇文章獻給那些嚮往成為內核大牛的小白們,由我來幫你們破除進入內核世界的第一道障礙。
準備環境
編譯內核之前有一些基本的條件。
- 一台聯網的機器(或者虛擬機)。
- 安裝了 Linux 系統。
- 基本的命令操作技能。
除此之外, Linux 系統還要安裝一些軟體包(可能不全,在編譯過程中遇到提示可以使用 google 搜索是缺了哪個包)。
- Git (分散式版本控制工具,用它來獲得內核源碼)
- GCC (編譯器)
- Make (構建系統)
- Libncurse-dev (終端圖形庫)
- OpenSSL-dev (加密庫)
- BC (計算器)
嗯,差不多了,開始動手吧。
獲取內核
感謝 Linus 開創了 Git,自從有了 Git ,獲取內核代碼變得異常的方便,而且時刻都可以是最新的。
在終端輸入以下命令即可:
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
沒有安裝 Git 的請重複上一個步驟。
基本配置
和大部分開源軟體類似, Linux Kernel 也是要配置之後才能夠編譯的。配置方法有好幾種,我比較偏愛的是 make menuconfig 。
$ cd linux
$ make menuconfig
執行完,你可以看到如下的配置界面。
知道為啥我喜歡用這個了吧,因為這個有圖形界面。雖然有好多看不懂,但是畢竟你能猜出個大概意思。
這次只是用來編譯第一個內核,所以不需要什麼配置,直接按右箭頭,走到 Exit 退出保存即可。
注意
在執行 make menuconfig 的時候,可能會遇到一些提示說某些包沒有安裝導致執行失敗。大家不要慌,原因是配置的過程中,內核先編譯了一個用戶態的配置工具,這個過程就需要依賴的軟體包: Make 、 GCC 、 LD 、圖形庫Libncurse-dev 等。不用緊張,按照提示,缺什麼就安裝什麼,再編譯就好了。
開始編譯
配置完了之後就可以編譯了。
很簡單,運行如下命令
$ make -j`grep -c processor /proc/cpuinfo`
如果編譯成功,你就會看到目錄下有一個文件叫 vmlinux 的文件。
恭喜~
安裝內核
安裝內核也很簡單,只需要這樣。
$ sudo make modules_install
$ sudo make install
另外需要注意的是,安裝後有些版本可能要調整引導程序的配置。比如在 ubuntu 上,配置文件在 /boot/grub/grub.cfg 。否則有時候下一次重啟還是使用舊的內核。
重啟機器
執行如下命令。
$ sudo reboot
好了,等下次機器起來,那就是一個嶄新的世界了。
本文是 LinuxStory 柴米油鹽計劃的投稿文章,由 LinuxStory 整理髮布。
原作者署名為:Wei Yang
轉載請註明出處,否則必究相關責任。
本文鏈接:https://linuxstory.org/kbuild-compile-linux-2/
Focxus on Fun
Finally, remember that virtual slots are meant to be fun. While making money is nice,
it』s not guaranteed. Savor the themes, animations, and
features with Thepokies106. When yyou emphaskze
the experience instead of jhst thee payout, the experience becomes far more rewarding. https://it.trustpilot.com/review/premiosabrinasganga.it
It’s an remarkable post in favor oof all the internet people; they will
get advantage from it I am sure. http://boyarka-inform.com
Happy to join conversations, share experiences, and gain fresh perspectives throughout the journey.
I like learning from different perspectives and contributing whenever I can. Always open to new ideas and meeting like-minded people.
Here’s my site:https://automisto24.com.ua/
Here to join conversations, share thoughts, and learn something new throughout the journey.
I’m interested in understanding different opinions and sharing my input when it’s helpful. Always open to different experiences and meeting like-minded people.
There is my web-site-https://automisto24.com.ua/
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.