編譯自己的Ubuntu內核


很多時候我們在使用Ubuntu的時候,想修改一下內核配置,然后編譯,安裝到Ubuntu中。這也是進行Ubuntu內核開發的前提。

獲取當前Ubuntu對應代碼

有很多方法可以獲得Ubuntu內核代碼,但是下面兩種非常方便。

uname -r顯示當前內核的版本號,apt-get source則用來獲取package的source code:

apt-get source linux-image-$(uname -r)

image

另一種方法是使用git,每個發行版的內核庫都會在kernel.ubuntu.com找到。如果想下載,只需要git clone一個庫到本地即可:

git clone git://kernel.ubuntu.com/ubuntu/ubuntu-<release codename>.git

其中release codename通過lsb_release -a獲取,比如Ubuntu 16.04的Codename為xenial。

Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial

PS:從實際使用來看,apt-get source的方式更方便,也不容易出錯,獲取的內核代碼和當前使用的是同一個版本。

編譯環境配置

如果需要編譯內核,還需要安裝一個package。可以使用如下命令:

sudo apt-get build-dep linux-image-$(uname -r)

修改內核配置

整個編譯過程使用的配置文件分布在若干子配置文件中,使用如下命令可以將這些配置文件集中,調用menuconfig進行修改。

使用chmod解決部分腳本可執行權限問題,

chmod a+x debian/rules
chmod a+x debian/scripts/*
chmod a+x debian/scripts/misc/*
fakeroot debian/rules clean
fakeroot debian/rules editconfigs # you need to go through each (Y, Exit, Y, Exit..) or get a complaint about config later

編譯內核

進入內核代碼的根目錄,執行如下命令:

fakeroot debian/rules clean
# quicker build:
fakeroot debian/rules binary-headers binary-generic binary-perarch
# if you need linux-tools or lowlatency kernel, run instead:
fakeroot debian/rules binary

如果編譯成功,在根目錄下會生成若干.deb包。

如下是4.4.0-57-generic內核在AMD64系統上生成的.deb包:

image

安裝內核鏡像

使用dpkg -i安裝若干.deb文件到系統中,然后重啟:

sudo dpkg -i linux*4.4.0-57*.deb
sudo reboot

Debug符號表

有時進行Debug需要符號表,這就需要在編譯的時候進行配置。首先需要安裝pkg-config-dgbsym,然后在執行編譯binary-*的時候增加’skildbg=false’:

sudo apt-get install pkg-create-dbgsym
fakeroot debian/rules clean
fakeroot debian/rules binary-headers binary-generic binary-perarch skipdbg=false

刪除Ubuntu內核

如果部分內核不需要,可以選擇刪除。需要先查看當前使用的內核版本:

uname -a可以獲取當前內核版本號。

在Ubuntu內核鏡像包含了以下的包。

  • linux-image-: 內核鏡像
  • linux-image-extra-: 額外的內核模塊
  • linux-headers-: 內核頭文件

首先檢查系統中安裝的內核鏡像:

dpkg --list|grep linux-image

dpkg --list|grep linux-headers

輸出如下結果:

al@al:~/data/ubuntu-kernel$ dpkg --list|grep linux-image
ii  linux-image-3.13.0-105-generic                       3.13.0-105.152                                amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
rc  linux-image-3.13.0-32-generic                        3.13.0-32.57                                  amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-4.4.0-57-generic                         4.4.0-57.78                                   amd64        Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-105-generic                 3.13.0-105.152                                amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
rc  linux-image-extra-3.13.0-32-generic                  3.13.0-32.57                                  amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-4.4.0-57-generic                   4.4.0-57.78                                   amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-generic                                  4.4.0.57.60                                   amd64        Generic Linux kernel image
al@al:~/data/ubuntu-kernel$ dpkg --list|grep linux-headers
ii  linux-headers-4.4.0-57                               4.4.0-57.78                                   all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-57-generic                       4.4.0-57.78                                   amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-generic                                4.4.0.57.60                                   amd64        Generic Linux kernel headers

 

在列出的內核鏡像中,你可以移除一個特定的版本(比如3.13.0-105和3.13.0-32)。

sudo apt-get purge linux-image-3.19.0-15

這就會將3.19.0-15相關的內核模塊刪除。通過下面dpkg --list再驗證如下:

dpkg --list|grep linux-headers

還可以通過sudo update-grub2來更新grub列表。

參考資料:

BuildYourOwnKernelhttps://wiki.ubuntu.com/Kernel/BuildYourOwnKernel

KernelGitGuidehttps://wiki.ubuntu.com/Kernel/Dev/KernelGitGuide


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM