核心(Kernel)顧名思義就是作業系統的心臟,進程管理、記憶體管理、裝置驅動、檔案系統與網路協定都握在它手裡,系統的效能與穩定性幾乎由它決定。本文以實驗環境 Red Hat Enterprise Linux 5.5 為例,走一遍「看版本 → 備環境 → 抓原始碼 → 設定 → 編譯」的完整流程。
[root@localhost yum.repos.d]# lsb_release -a LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch Distributor ID: RedHatEnterpriseServer Description: Red Hat Enterprise Linux Server release 5.5 (Tikanga) Release: 5.5 Codename: Tikanga
RHEL 5.5 的核心檔存放於 /boot,檔名形如 vmlinuz-2.6.18-194.el5:
[root@localhost boot]# pwd /boot [root@localhost boot]# ls config-2.6.18-194.el5 symvers-2.6.18-194.el5.gz grub System.map-2.6.18-194.el5 initrd-2.6.18-194.el5.img vmlinuz-2.6.18-194.el5 lost+found [root@localhost boot]# ll -h vmlinuz-2.6.18-194.el5 -rw-r--r-- 1 root root 1.8M 3?17 2010 vmlinuz-2.6.18-194.el5
查詢目前運作的核心版本:
[root@localhost boot]# uname -r 2.6.18-194.el5
把 2.6.18-194.el5 拆開來看:2 是主版本號,6 是次版本號,18 是修正號;-194 是 Red Hat 針對官方核心所做的修補編號;el5 則是發行版本標記,代表 Enterprise Linux 5。
自訂核心的理由通常就兩種:修補漏洞,或加入新功能。開始之前務必確認 gcc 工具鏈與 ncurses 開發套件已就位(后者是 menuconfig 的圖形介面所需):
[root@localhost ~]# rpm -qa | grep gcc libgcc-4.1.2-48.el5 gcc-4.1.2-48.el5 compat-libgcc-296-2.96-138 gcc-gfortran-4.1.2-48.el5 gcc-c++-4.1.2-48.el5
缺件的話可以一次補齊:
yum install -y *gcc* ncurses-devel
第一步,取得核心原始碼並解開。
[root@localhost ~]# wget https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.4.tar.bz2 --no-check-certificate [root@localhost ~]# tar xf linux-2.6.39.4.tar.bz2 [root@localhost ~]# cd linux-2.6.39.4 [root@localhost linux-2.6.39.4]# ls arch fs MAINTAINERS security block include Makefile sound COPYING init mm tools CREDITS ipc net usr crypto Kbuild README virt Documentation Kconfig REPORTING-BUGS drivers kernel samples firmware lib scripts
第二步,清理環境:make mrproper。它會刪除先前編譯產生的檔案與核心組態檔。如果是剛下載的全新原始碼,這一步可以略過。
第三步,設定核心:make menuconfig。
[root@localhost ~]# make menuconfig
執行後會跳出桌面式的設定介面,因此不確定在純文字的 SSH 遠端環境下能否直接使用;若遠端操作,可改用 make config 或先載入舊組態再微調。設定完成後依序執行 make、make modules_install、make install,最後更新 bootloader 並重啟即可。
Linux Kernel 的詳細介紹:請點這裡
Linux Kernel 的下載地址:請點這裡