Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS Linux 內核升級全流程詳解:從 2.4 到 2.6 的實戰指南

編輯:Linux 教學

前言

本文記錄在 CentOS(兼容 RHEL)系統上將 Linux 內核從 2.4 系列升級至 2.6.18 的完整實戰過程。雖然當前主流發行版已內建新內核,但理解手動編譯、配置、安裝全流程,對於嵌入式開發、內核定制、故障排查仍具極高參考價值。操作有風險,生產環境請先快照/備份,並準備救援啟動盤。

一、前置準備

1. 確認當前內核版本

uname -a
# 輸出含 2.4.x 才需繼續;若已是 2.6+ 可跳過

2. 下載源碼包與工具包

組件版本下載地址(示例)
Linux 內核源碼2.6.18https://cdn.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.tar.bz2
module-init-tools3.2https://cdn.kernel.org/pub/linux/utils/kernel/module-init-tools/module-init-tools-3.2.tar.bz2
mkinitrd4.1.18-2Fedora Legacy / Vault
lvm22.00.25-1.01Fedora Legacy / Vault
device-mapper1.00.19-2Fedora Legacy / Vault

注意:舊版鏡像可能失效,建議從 vault.centos.org 或 archive.kernel.org 獲取對應版本。

3. 解壓並部署到 /usr/src

cd /usr/src
rm -rf linux                 # 清理舊符號鏈接/目錄
tar jxvf linux-2.6.18.tar.bz2
ln -s linux-2.6.18 linux

二、工具鏈升級(關鍵步驟)

2.6 內核要求更新的模塊管理工具,否則 make install 會報 mkinitrd failed。

1. module-init-tools

tar jxvf module-init-tools-3.2.tar.bz2
cd module-init-tools-3.2
./configure --prefix=/
make moveold   # 備份舊版模塊工具
make all install
./generate-modprobe.conf /etc/modprobe.conf

2. 安裝其餘三個 RPM 包

cd /usr/src
rpm -ivh --nodeps mkinitrd-4.1.18-2.i386.rpm
rpm -ivh --nodeps lvm2-2.00.25-1.01.i386.rpm
rpm -ivh --nodeps device-mapper-1.00.19-2.i386.rpm

--nodeps 強制忽略依賴,因環境可能不完全滿足。

三、內核配置

cd /usr/src/linux-2.6.18
make mrproper                # 徹底清理,確保無殘留 .o/.config

啟動圖形配置介面:

make menuconfig

必選項(漏選將導致啟動失敗):

  1. Loadable Module Support
    • Module unloading [ * ]
    • Automatic kernel module loading [ * ]
  2. Device Drivers → Block Devices
    • Loopback device support [ * ]
  3. Device Drivers → Multi-device support (RAID and LVM)
    • Device mapper support [ * ]
  4. Device Drivers → Graphics support
    • Support for frame buffer devices [ * ]
  5. Device Drivers → USB support
    • USB Mass Storage support [ * ]
  6. Device Drivers → Network device support → Ethernet (10/100Mbit)
    • AMD PCnet32 PCI support [ * ](常見虛擬機網卡)
  7. File systems → ext2/ext3 相關 9 項全部選 [*]
    • Second extended fs support
    • Ext2 extended attributes / POSIX ACL / Security Labels
    • Ext3 journalling file system support
    • Ext3 extended attributes / POSIX ACL / Security Labels
    • JBD (ext3) debugging support

    關鍵:ext2/ext3 必須編譯為模塊 (M) 或確保 initrd 包含對應模塊,不可直接編入內核,否則啟動時會報 VFS: Cannot open root device 或 kernel panic: no init found。

  8. File Systems → DOS/FAT/NT Filesystems
    • NTFS file system support [ * ]
  9. File Systems → Miscellaneous filesystems
    • Compressed ROM file system support (cramfs) [ * ]
  10. VMware / SCSI 虛擬磁盤環境額外必選
    • Device Drivers → SCSI device support → SCSI disk support [ * ]
    • SCSI low-level drivers → BusLogic SCSI support [ * ](建議選 M)

配置完成保存 .config 退出。

四、編譯與安裝

cd /usr/src/linux-2.6.18
make dep          # 2.6 內核實際不再需要,但保留兼容
make clean
make bzImage      # 編譯壓縮內核映像(大內核必須用 bzImage)
make modules      # 編譯模塊
make modules_install  # 安裝模塊至 /lib/modules/2.6.18/
make install      # 安裝內核、生成 initrd、更新 bootloader

常見錯誤及對策:

  • No module BusLogic found for kernel 2.4.12:因 BusLogic 被編入內核而非模塊。解決:cp drivers/scsi/BusLogic.o /lib/modules/2.6.18/kernel/drivers/scsi/ 後重跑 make install,或在 menuconfig 中將 BusLogic 設為 M。
  • mkinitrd failed:檢查 module-init-tools、lvm2、device-mapper 是否正確安裝。

五、啟動新內核

1. 複製內核映像與 System.map

cp /usr/src/linux-2.6.18/arch/i386/boot/bzImage /boot/vmlinuz-2.6.18
cp /usr/src/linux-2.6.18/System.map /boot/System.map-2.6.18
cd /boot
rm -f System.map
ln -s System.map-2.6.18 System.map

2. 生成 initrd 並更新 GRUB

new-kernel-pkg --mkinitrd --depmod --install 2.6.18

此命令會在 /boot 生成 initrd-2.6.18.img 並自動修改 /boot/grub/grub.conf。

3. 手動核對 /boot/grub/grub.conf

# 查看根分區所在設備
df /
# 假設為 /dev/hda2

vi /boot/grub/grub.conf

需修改兩處:

  1. default=1 → default=0(使新內核為預設啟動項)。
  2. kernel /vmlinuz-2.6.18 ro root=LABEL=/ → kernel /vmlinuz-2.6.18 ro root=/dev/hda2(指定真實根分區設備,避免 LABEL 解析失敗)。

修改後示例:

default=0
timeout=10
splashimage=(hd0,0)/grub/splash.xpm.gz
title Red Hat Linux (2.6.18)
root (hd0,0)
kernel /vmlinuz-2.6.18 ro root=/dev/hda2
initrd /initrd-2.6.18.img
title Red Hat Linux (2.4.20-8)
root (hd0,0)
kernel /vmlinuz-2.4.20-8 ro root=LABEL=/
initrd /initrd-2.4.20-8.img

六、驗證與善後

reboot
uname -r    # 應顯示 2.6.18

若啟動失敗,在 GRUB 選單選擇舊內核 (2.4) 進入系統,檢查 /var/log/messages、dmesg、initrd 內容(lsinitrd),核對模塊、根分區參數、文件系統支持。

七、現代視角的補充建議

  • 優先使用發行版內核:yum update kernel / dnf upgrade kernel 自動處理依賴、簽名、bootloader、initrd,極大降低風險。
  • 若需定制:安裝 kernel-devel、kernel-headers,使用 make olddefconfig 基於現有配置微調,再 make rpm-pkg 打包為 RPM 安裝,納入包管理。
  • UEFI + Secure Boot:需對內核、模塊、bootloader 簽名,否則無法啟動。
  • 內核版本對應:本文以 2.6.18 為例(RHEL 5 / CentOS 5 時代),現代內核 (5.x/6.x) 配置選項差異極大,請參考對應版本 Documentation/ 與 make help。

結語

手動升級內核是對 Linux 啟動流程、模塊機制、initrd、bootloader 協作關係的一次全鏈路演練。雖然生產環境極少手動編譯,但掌握此流程能讓你在內核恐慌、模塊缺失、啟動掛起等疑難雜症面前游刃有餘。

Copyright © Windows教程網 All Rights Reserved