歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> 編譯Linux內核(kernel)

編譯Linux內核(kernel)

日期:2017/2/28 16:14:12   编辑:Linux內核

學習Linux就從內核編譯開始,現在就讓我們邁出第一步,下面記錄編譯過程(以下操作均在root下完成)。

1、准備工作

我安裝的是Ubuntu10.04,為完成內核編譯,還需要安裝一下包:

# apt-get install gcc
# apt-get install make

# apt-get install bzip2
# apt-get install libc6-dev
# apt-get install ncurse-dev
# apt-get install initrd-tools ####為了使用mkinitrd命令生成initrd.img-XXX

2、下載源碼

可以在官網www.kernel.org下載響應的源碼(下載完整版F),也可以wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.tar.bz2命令獲取。

#wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.tar.bz2

#tar xjf linux-2.6.39.tar.bz2 -C /usr/src

3、配置文件

根據自己機器的硬件情況,選擇內核編譯配置選項,在這裡我使用的是默認配置(make menuconfig後直接保存退出,生成.config文件);具體配置可以參考金步國先生翻譯的資料:點擊這裡下載Linux 2.6.19.x 內核編譯配置選項。

#cd /usr/src/linux-2.6.39

#make menuconfig

4、編譯內核

#make clean ####第一次編譯可以省略

#make mrproper ####第一次編譯可以省略

#make

5、編譯和安裝模塊

#make modules

# make modules_install

6、生成內核鏡像文件

#makinitramfs -o /boot/initrd-2.6.39.img /lib/modules/2.6.39 ##啟動、加載模塊
# cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.39 ##可引導的、壓縮的內核
# cp System.map /boot/System.map-2.6.39 ##kernel 索引
# cp .config /boot/config-2.6.39 #內核配置文件


7、設置啟動項
啟動項是/boot/grub/grub.cfg文件,我們需要修改該文件,具體如下:

首先,復制下面內容。
menuentry 'Ubuntu, with Linux 2.6.32-32-generic' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,4)'
search --no-floppy --fs-uuid --set 8450af8f-090b-4eae-a123-6f1e3604fa9e
linux /boot/vmlinuz-2.6.32-32-generic root=UUID=8450af8f-090b-4eae-a123-6f1e3604fa9e ro quiet splash
initrd /boot/initrd.img-2.6.32-32-generic
}

然後,對上面內容進行響應的修改,紅色部分。
menuentry 'Ubuntu_Test, with Linux 2.6.39' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,4)'
search --no-floppy --fs-uuid --set 8450af8f-090b-4eae-a123-6f1e3604fa9e
linux /boot/vmlinuz-2.6.39 root=UUID=8450af8f-090b-4eae-a123-6f1e3604fa9e ro quiet splash
initrd /boot/initrd.img-2.6.39
}

8、測試
重啟,選擇Ubuntu_Test, with Linux 2.6.39進入ubuntu系統,最後通過命令uname -a查看系統的內核信息。

Copyright © Linux教程網 All Rights Reserved