歡迎您光臨本站 註冊首頁

Linux內核升級全過程

←手機掃碼閱讀     火星人 @ 2014-03-24 , reply:0

一、準備工作


啟動Linux系統,並用根用戶登錄,進入終端模式下。


1、查看Linux內核版本

# uname -a

如果屏幕顯示的是2.6.x,說明你的已經是2.6的內核,也用不著看下文了,該幹什麼幹什麼去吧!~~~如果顯示的是2.4.x,那恭喜你,闖關通過,趕快進行下一步。


2、下載2.6內核源碼

下載地址:http://www.kernel.org/pub/linux/kernel/v2.6/
紅帽下載地址: http://downloads.lustre.org/public/kernels/rhel4/


3、下載內核升級工具

(1)下載module-init-tools-3.2.tar.bz2

http://www.kernel.org/pub/linux/utils/kernel/module-init-tools/module-init-tools-3.2.tar.bz2

(2)下載mkinitrd-4.1.18-2.i386.rpm

http://ayo.freshrpms.net/fedora/linux/3/i386/RPMS.core/mkinitrd-4.1.18-2.i386.rpm

(3)下載lvm2-2.00.25-1.01.i386.rpm

http://ayo.freshrpms.net/fedora/linux/3/i386/RPMS.core/lvm2-2.00.25-1.01.i386.rpm

(4)下載device-mapper-1.00.19-2.i386.rpm

http://ayo.freshrpms.net/fedora/linux/3/i386/RPMS.core/device-mapper-1.00.19-2.i386.rpm



二、配置工作


好啦,2.6內核和4個升級工具都下載完了(少一個也不行,如果沒有下載齊全,請不要嘗試下面的步驟,升級是不會成功的),下面回到Linux系統中開始配置工作吧。


4、將下載好的內核和4個升級工具都拷貝到/usr/src文件夾下。怎麼拷貝就不用我教了吧~~~~不會拷貝的去撞牆吧!~~呵呵!


5、拷貝完畢,開始解壓新內核,具體操作請依次執行以下命令:

# cd /usr/src (進入到/usr/src目錄下,如果已經在/usr/src目錄下,可不執行該命令)

# rm ?rf linux (刪除linux文件夾。值得一提的是,如果內核先前從未編譯過,則沒有這個文件夾,此命令行可略過)

# tar jvxf linux-2.6.18.tar.bz2 (解壓新內核)

# ln -s linux-2.6.18 linux (重新生成linux文件夾)


6、安裝module-init-tools工具包

在/usr/src目錄下,依次執行下列命令:

# tar jvxf module-init-tools-3.2.tar.bz2 (解壓module-init-tools)

# cd module-init-tools-3.2 (由/usr/src目錄進入module-init-tools目錄下)

#./configure --prefix=/

# make(make moveold,有網友這樣用,我沒有用,因為我的執行不成功)

# make all install

#./generate-modprobe.conf /etc/modprobe.conf

注:
安裝module-init-tools工具時,命令"make moveold"將把系統原來的modutils工具程序改名為"*.old"(比如,lsmod.old等等). NOTE! 這是非常重要的一步, 千萬不要省略. 這將使得你可以繼續使用原有的linux-2.4.x系統, 因為在2.4.x系統下, 新的module-init- tools工具包實際上是倚賴原來"*.old"程序來載入內核模塊. 如果忘記了這一步也不要緊張, 可以先下載並安裝原來的modutils程序包, 然後按照上面的步驟重來一遍就可以了.


7、安裝另外三個升級工具

回到/usr/src目錄下,依次執行下列3個命令來安裝另外三個升級工具:

# rpm -ivh --nodeps mkinitrd-4.1.18-2.i386.rpm (注意,這裡一定要加入--nodeps參數,在安裝升級工具時,如果加了--nodeps參數仍未能安裝成功,請嘗試再加入參數—force,下同)

# rpm -ivh --nodeps lvm2-2.00.25-1.01.i386.rpm

# rpm -ivh --nodeps device-mapper-1.00.19-2.i386.rpm


如果不更新以上幾個升級包,在後面編譯內核時會提示以下錯誤:

mkinitrd failed

make[1]: *** [install] Error 1

make: *** [install] Error 2


8、配置內核選項。有點繁瑣,~~希望一次成功哦~~。

# cd linux-2.6.18 (進入到/usr/src/linux-2.6.18目錄下)

# make mrproper (該命令可確保源代碼目錄下沒有不正確的.o文件以及文件的互相依賴)

# make menuconfig (配置內核各選項)


此時會出現一個圖形界面,列出了所有的內核配置選項,有的選項下還有子選項,你可以用方向鍵來選擇,用Y鍵來確定。經過我多次試驗,大多數選項默認就行,以下幾個選項必須選擇(請認真核對下面每一個選項,否則編譯很有可能前功盡棄):


(1)Loadable Module support選項中,一定要選上「Module unloading」和「Automatic kernel module loading」這兩項;

(2)Device Drivers--->Block Devices中一定要選上Loopback device support;

Device Drivers--->Multi-device support(RAID and LVM)」處要選上「device mapper support」;

Device Drivers--->Graphics support」, 一定要選上」 Support for frame buffer devices」;

Device Drivers --->;USB support --->選上」USB Mass Storage support」(如果是在實環境中,想要更多USB支持,就全選吧。我的是在虛擬機中,用不著了)

Device Drivers ---> Fusion MPT device support ---> Fusion MPT (base + ScsiHost) drivers和 Fusion MPT misc device (ioctl) driver(這個要不要, 不確定 , 注意:這個只能選模塊方式,直接編譯進內核,就出錯,不知道為什麼,這個浪費了我好多時間才找到,編譯了幾十次的成果。出錯信息如下:No module mptbase found for kernel vmware。。。。。。。)


(3) File system--->(以下9個選項是關於ext2和ext3文件系統配置,全部選上)

Second extended fs support

Ext2 extended attributes

Ext2 POSIX Access Control Lists

Ext2 Security Labels

Ext3 journalling file system support

Ext3 extended attributes

Ext3 POSIX Access Control Lists

Ext3 Security Labels

JBB (ext3) debugging support

File system--->DOS/FAT/NT Filesystems --->選上「 NTFS file system support」;

File Systems --> Miscellaneous filesystems ---><*> Compressed ROM file system support (cramfs)


注意:

ext2和ext3文件系統配置很重要,也是必需的,對Ext3、Ext2文件的支持必須編譯進內核,而不能編譯為模塊,否則在你reboot時機器就會當掉,出現如下錯誤信息:

kernel panic : no init found ,try passing init = option to kernel.....

或者是:

VFS:Cannot open root device "hdxy" or unknow-block(0,0)

Please append a correct "root=" boot option

kernel panic:VFS:Unable to mount root fs on unknown-block(0,0)

或者是:

mount: error 19 mounting ext3

pivotroot: pivot_root(/sysroot,/sysroot/initrd) failed: 2

umount /initrd/proc fail: 2

Freeing unused kernel memory: 244k freed

Kernel panic ? not syncing: No init found. Try passing init = option to kernel

(我的機器就是在重啟之後出現第三種錯誤,進不去系統,鬱悶死,只好重裝了~~~)


(4)由於在vmware中使用模擬的SCSI硬碟和AMD PCnet32網卡,因此需要「SCSI disk support」、「BusLogin SCSI support」、「AMD PCnet32 PCI support」的支持,最好編譯進內核,而不是編譯為內核模塊。:


Device Drivers --->SCSI device support ---><*> SCSI disk support
Device Drivers --->SCSI device support --->SCSI low-level drivers ---> <*> BusLogic SCSI support
Device Drivers ---> Networking support --->Ethernet (10 or 100Mbit) ---> <*> AMD PCnet32 PCI support
如果編譯為模塊,initrd則是必須的,否則啟動過程中會出現「VFS:unable to mounting root fs」.程序包mkinitrd依賴於程序包device-mapper,而程序包device-mapper又依賴於lvm2,因此需要同時下載mkinitrd、device-mapper、lvm2程序包。

三、編譯工作


OK,繁雜的配置工作完成了,至此,編譯前的準備工作都做好了!

9、開始編譯啦……:

在/usr/src/linux-2.6.18目錄下,執行以下命令即可編譯。編譯需要一段時間,給自己倒杯茶耐心等候吧!

# make dep (建立編譯時所需的從屬文件。注意:如果內核從未編譯過,此步可跳過)

# make clean (清除內核編譯的目標文件)

# make bzImage (注意大小寫。這一步才真正編譯內核)

內核編譯成功后,會在/usr/src/linux/arch/i386/boot目錄中生成一個新內核的映像文件bzImage。如果用make zImage編譯,內核很大的話,系統會提示你使用make bzImage命令來編譯,所以我直接用make bzImage來編譯。

# make modules (編譯可載入模塊)

# make modules_install (安裝可載入模塊)

安裝成功后,系統會在/lib/modules目錄下生成一個2.6.18子目錄,裡面存放著新內核的所有可載入模塊。

# make install (安裝新內核)


注意:make install的時候可能會出現如下錯誤信息:

No module BusLogic found for kernel 2.4.12

mkinitrd failed

此問題一般只出現在SCSI硬碟+VMWARE+REDHAT架構中,因為BusLogic被編譯進了內核而不是一個module的形式(2.4內核的Buslogic模塊即使靜態編譯進內核也不行)。解決方式是直接將可以BusLogic.o文件複製過去:

# cp /usr/src/linux-2.6.18/drivers/scsi/BusLogic.o /lib/modules/2.6.18/kernel/drivers/scsi

不過別忘記,複製過後再執行一下make install。

(如果make menuconfig時,把BusLogic模塊選成M--Module模式--就不會出現這個提示。)

編譯錯誤解決匯總
①錯誤1
OBJCOPY arch/i386/boot/compressed/vmlinux.bin
BFD: Warning: Writing section `.bss' to huge (ie negative) file offset 0xc0277000.
objcopy: arch/i386/boot/compressed/vmlinux.bin: File truncated
make[2]: *** [arch/i386/boot/compressed/vmlinux.bin] Error 1
make[1]: *** [arch/i386/boot/compressed/vmlinux] Error 2
make: *** [bzImage] Error 2
解決方法:

在arch/i386/Makefile裡面做一些修改:

將OBJCOPYFLAGS改成如下的:

OBJCOPYFLAGS :=-O binary --change-section-lma
.bss-0xc0000000 ?R
.note -R .comment
?S

然後重新make bzImage.




編譯成功提示如下:

OBJCOPY arch/i386/boot/compressed/vmlinux.bin

GZIP
arch/i386/boot/compressed/vmlinux.bin.gz

LD
arch/i386/boot/compressed/piggy.o

LD
arch/i386/boot/compressed/vmlinux

OBJCOPY arch/i386/boot/vmlinux.bin

HOSTCC
arch/i386/boot/tools/build

BUILD
arch/i386/boot/bzImage

Root device is (8, 1)

Boot sector 512 bytes.

Setup is 7498 bytes.

System is 1191 kB

Kernel: arch/i386/boot/bzImage is ready
(#3)
②錯誤2
ERROR: "crypto_free_tfm" [net/rxrpc/af-rxrpc.ko] undefined!
ERROR: "crypto_alloc_base" [net/rxrpc/af-rxrpc.ko] undefined!
make[1]: *** [__modpost] Error 1
make: *** [modules] Error 2
解決方法:
選中Cryptographic options
--->
Cryptographic API


四、啟動新內核

10、將新內核和System.map文件拷貝到/boot目錄下,依次執行以下命令:

# 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 (進入boot目錄)

# rm ?rf System.map (刪除原來的連接)

# ln -s System.map-2.6.18 System.map (重新建立連接)
注:{
(1) 由於linux2.6 內核增加了sysfs文件系統,所以要創建/sys目錄

#mkdir /sys

(2)修改啟動腳本/etc/rc.sysinit

①添linux2.6內核新支持的sysfs系統

1)找到「mount ?f /proc」,在下面添加如下內容:

#Mount /sys for kernel-2.6.x

if [ "$KERNELVER" ="2.6" ]; then

mount -f /sys

fi

2)找到「action$」Mounting proc filesystem:」mount ?n ?t proc /proc /proc」,在下面添加如此內容:

#Mount /sys for kernel-2.6.x

if [ "$KERNELVDER" = "2.6" ]; then

action $"Mounting sysfs filesystem: " mount -n -t sysfs /sys /sys

fi

②為linux2.6內核符號文件添加支持(添加的KERNELVAER變數,來判斷內核版本)

#

#---------------------KERNEL 2.6.x support---------------------------

#This is for compatibility between kernel-2.4.x and kernel-2.6.x

#

UNAME='uname -r'

KERNELVER=${UNAME:0:3}


if [ "$KERNELVER" = "2.6" ]; then

#

#This is for kernel-2.6.x

#

KSYMS=/proc/kallsyms

KEYBDEV_NAME=usbkbd

MOUSEDEV=usbmouse

else

#

#This is for kernel-2.4.x

#

KSYMS=/proc/ksyms

KEYBDEV_NAME=keybdev

MOUSEDEV=mousedev

Fi




③添加USB鍵盤與滑鼠的新支持

1)由於USB鍵盤模塊名稱從keybdev變為usbkbd,USB滑鼠模塊摸名稱從mousedev變為usbmouse,所以必須修改啟動腳本文件。通過定義KEYBDEV_NAME和MOUSEDEV_NAME兩個變數來標識

相應的模塊名,具體代碼見②。

2)在rc.sysinit腳本中所有出現keybdev、mousedev的地方都變成$KEYBDEV_NAME和$MOUSEDEV_NAME。


3)對rc.sysinit以下內容進行修改

needusbstorage=

if [ $usb = "1" ]; then

needusbstorage=`LC_ALL=C grep -e "^I.*Cls=08" /proc/bus/usb/devices 2>/dev/null`

LC_ALL=C grep 'hid' /proc/bus/usb/drivers || action $"Initializing USB HID interface: " modprobe hid 2> /dev/null

action $"Initializing USB keyboard: " modprobe $KEYBDEV_NAME 2> /dev/null

action $"Initializing USB mouse: " modprobe $MOUSEDEV_NAME 2> /dev/null

fi

修改為

needusbstorage=

if [ $usb = "1" ]; then

if [ "$KERNELVER" = "2.6" ]; then

needusbstorage=`LC_ALL=C grep -e "^I.*Cls=08" /sys/bus/usb/devices 2>/dev/null`

LC_ALL=C grep 'hid' /sys/bus/usb/drivers || action $"Initializing USB HID interface: " modprobe hid 2> /dev/null

else

needusbstorage=`LC_ALL=C grep -e "^I.*Cls=08" /proc/bus/usb/devices 2>/dev/null`

LC_ALL=C grep 'hid' /proc/bus/usb/drivers || action $"Initializing USB HID interface: " modprobe hid 2> /dev/null

fi

action $"Initializing USB keyboard: " modprobe $KEYBDEV_NAME 2> /dev/null

action $"Initializing USB mouse: " modprobe $MOUSEDEV_NAME 2> /dev/null

fi

(3)修改/etc/fstab文件系統列表

在/etc/fstab中增加,如下內容:

none /sys sysfs fefaults 0 0

(4)修改/etc/init.d/halt 腳本中的halt_get_remaining函數

找到halt_get_remaining函數,修改

awk '$2 ~ /^\/$|^\/proc|^\/dev/{next}

修改為

awk '$2 ~ /^\/$|^\/proc|^\/sys|^\/dev/{next}

(5)修改/etc/modprobe.conf配置文件

由於在linux2.6內核中USB驅動模塊被改名了,在主機控制方面,usb-ohci被改成ochi-hcd,usb-uhci被改變成uhci-hcd,因此modprobe.conf配置文件中應該進行相應的改變。

alias usb-controller uhci-hcd
}

11、修改Grub啟動管理器

如果沒有錯誤的話, 下面開始修改grub配置文件(不要告訴我你用的lilo)

在/boot目錄下,執行以下命令:

# new-kernel-pkg --mkinitrd --depmod --install 2.6.18 (這時候你的/boot下生成一個initrd-2.4.12.img,並且你的grub.conf也作了相應更改)


# df (查看根目錄在那個分區,下一步要用到。注意,這裡根分區不時boot的那個50M的分區,而一般是你最大的那個分區,也就是」/」,千萬不要搞錯哦。我的為 /dev/hda2)


# vi /grub/grub.conf

進入grub.conf文件,找到如下信息:

default=1

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= LABEL=/

initrd /initrd-2.6.18.img

做兩處修改:

(1) 將default=1改為default=0(不改的話,重啟之後默認進入2.4內核)

(2) 將kernel行的「LABEL=/」換成根目錄所在的分區(上一步查看的就是)

此步很重要,修改錯誤將會可能導致進不去系統,我把我修改後的grub.conf文件列出來,不明之處,可以對照修改:

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


12,# init 6 (重啟機器,以啟動新內核)

大功告成!看看升級后的2.6內核吧!



新內核啟動期間問題

init[1]: segfault at 00000021 eip 00000021 esp bfbb7bb8 error 4

或者

init[1]: segfault at 21 ip 00000021 sp bf9e5878 error 4 in init[804800+6000]

Kernel panic - not syncing: Attempted to kill init!

1)有網友說:出現這個錯誤,而且系統應該算是崩潰了,一直不停刷屏。這個是在編譯2.6.23和.2.6.24時出現的錯誤。尚未解決。網上好多朋友也都說碰到這個問題,好多也沒有解決。有人解答說虛擬機下不能升級到2.6.20以上的版本。可能這樣說還是有些問題,我曾經試過一下內核都是可以的,2.6.19,2.6.20,2.6.21,到2.6.22時沒有出現上面的刷屏現象,但2.6.24刷屏前的代碼和它相似,估計是從.26.22版本改了一些東西導致的。具體原因不解。


2)也有網友說:在vmware里只能升內核到2.6.18,2.6.23以上內核可以編譯通過reboot后不能進入系統。如果要用2.6.24,在vmware中就用Fedora系統吧。
3)反正這個問題就是關於內核版本差異的問題,要想解決此問題,需要內核的可移植性支持,拭目以待高手出現吧。

[火星人 ] Linux內核升級全過程已經有598次圍觀

http://coctec.com/docs/linux/show-post-145943.html