Linux內核編譯:
編譯安裝內核的詳細說明:
編譯安裝內核概要步驟:
一、安裝開發包組
二、下載內核源碼文件
三、準備.config文本配置文件
四、經過make menuconfig配置內核選項
五、make[-j #] 指定編譯時使用CPU核心數
六、make modules_install:安裝模塊
七、make install :安裝內核相關文件,安裝bzImage爲/boot/vmlinuz-VERSION-RELEASE、生成initramfs文件、編輯grub配置文件linux
配置內核選項方式:
支持「更新」模式進行配置:在已有的.config文件的基礎之上進行修改配置:
make config:基於命令行以遍歷的方式去配置內核中可配置的每一個選項;
make menuconfig:基於cureses的文本配置窗口;
make gconfig:基於GTK開發環境的窗口界面
make xconfig;基於QT開發環境窗口界面;
支持「全新配置」模式進行配置,不須要.config文件:
make defconfig:基於內核爲目標平臺提供的「默認」配置爲模板進行配置;
make allnoconfig:全部選項均回答爲「no」git
編譯參數指定:
make [-j #] 多線程編譯
編譯內核中的一部分代碼:
只編譯某子目錄中的相關代碼:
cd /usr/src/linux
make path/to/dir/ 只能在內核源碼所在的目錄中才能編譯centos
只編譯一個特定的模塊:
cd /usr/src/linux
make path/to/dir/file.ko
例如:只爲e1000編譯驅動:
make drivers/net/ethernet/intel/e1000/e1000.ko多線程
如何交叉編譯:目標平臺與當前編譯操做所在的平臺不一樣;
make ARCH=arch_name
獲取特定目標平臺的使用幫助:
make ARCH=arch_name help編輯器
編譯清理:
如何在執行過編譯操做的內核源碼樹上作從新編譯,需先清理:
make clean:清理編譯生成的絕大多數文件,但會保留.config及編譯外部模塊所須要的文件;
make mrproper:清理編譯生成的全部文件,包括配置生成的.config文件及某些備份文件;
make distclean:至關於mrproper,額外清理各類patches以及編輯器備份文件;ide
卸載內核:
刪除/lib/modules/目錄下不須要的內核庫文件
刪除/usr/src/linux/目錄下不須要的內核源碼
刪除/boot目錄下啓動的內核和內核映像文件
更改grub的配置文件,刪除不須要的內核啓動列表工具
內核編譯演示:
一、獲取源碼包(www.kernel.org),選擇要編譯的版本,爲了保障可以成功編譯,建議不要使用跨版本太大的源碼包開發工具
二、準備開發環境(相關開發工具包),以及查看本身的主機硬件相關信息是否能夠符合ui
三、解壓到 /usr/src 目錄下tar xf linux-3.10.67.tar.xz -C /usr/src
[root@centos7 ~]# ls
bin Documents f1 httpd-2.2.29 linux-3.18.41.tar.xz # 獲取的源碼包 Music Public Templates
anaconda-ks.cfg Desktop Downloads hanshu3.sh httpd-2.2.29.tar.bz2 mbox Pictures select.sh Videos
[root@centos7 ~]# tar xvf linux-3.18.41.tar.xz -C /usr/src # 解壓到/usr/src目錄下
[root@centos7 ~]# ls /usr/src
debug kernels linux-3.18.41 # 解壓的源碼包centos7
四、進到/usr/src目錄中,並建立一個軟連接;
[root@centos7 ~]# cd /usr/src
[root@centos7 src]# ls
debug kernels linux-3.18.41
[root@centos7 src]# ln -s linux-3.18.41/ linux # 建立軟連接
[root@centos7 src]# ls
debug kernels linux linux-3.18.41
[root@centos7 src]# cd linux # 進到目錄中去
[root@centos7 linux]# ls
arch COPYING crypto drivers fs init Kbuild kernel MAINTAINERS mm README samples security tools virt
block CREDITS Documentation firmware include ipc Kconfig lib Makefile net REPORTING-BUGS scripts sound usr
[root@centos7 linux]# du -sh . # 查看大小
631M .
五、複製/boot下的以config開頭的文件做爲模板文件到/usr/src/linux/.config
# cp /boot/config-3.10.0-327.el7.x86_64 /usr/src/linux/.config
[root@centos7 linux]# cp /boot/config-3.10.0-327.el7.x86_64 /usr/src/linux/.config #複製模板文件
[root@centos7 linux]# ls -a
. arch .config(# 複製的模板文件) CREDITS Documentation firmware .gitignore init Kbuild kernel .mailmap Makefile net REPORTING-BUGS scripts sound usr
.. block COPYING crypto drivers fs include ipc Kconfig lib MAINTAINERS mm README samples security tools virt
六、在/usr/src/linux 中使用 # make menuconfig 配置內核選項,選好以後保存退出;
七、打開screen界面,執行 # make [-j 4] 編譯內核,可以使用-j指定編譯線程數量;
這時可使用 # htop 查看一下cpu的使用狀況,以下所示:cpu佔用率急劇升高,負載也變得好大;
八、編譯完成以後,接下來須要執行 # make modules_install 安裝內核模塊
[root@centos7 src]# du -sh linux-3.18.41/
7.1G linux-3.18.41/ # 能夠看到編譯完成以後,變成了7.1G
[root@centos7 linux]# ls /lib/modules
3.10.0-327.el7.x86_64 # 能夠看到這時模塊文件只有一個
[root@centos7 linux]# make modules_install # 安裝內核模塊
[root@centos7 linux]# ls /lib/modules
3.10.0-327.el7.x86_64 3.18.41-1.0-Taolinux # 能夠發現多了一個內核模塊
[root@centos7 linux]# ls /lib/modules/3.18.41-1.0-Taolinux/ # 編譯完成,而已看到生成的內核模塊文件
build modules.builtin.bin modules.softdep
kernel modules.dep modules.symbols
modules.alias modules.dep.bin modules.symbols.bin
modules.alias.bin modules.devname source
modules.builtin modules.order
九、執行 # make install 生成內核文件
[root@centos7 linux]# make install
sh ./arch/x86/boot/install.sh 3.18.41-1.0-Taolinux arch/86/boot/bzlmage \
System.map "/boot"
十、reboot重啓系統,選擇使用新內核
啓動以後,查看內核版本以下:
[root@centos7 ~]# uname -r
3.18.41-1.0-Taolinux # 說明新版內核已經啓動,編譯成功