內核,是一個操做系統的核心。它負責管理系統的進程、內存、設備驅動程序、文件和網絡系統,決定着系統的性能和穩定性。Linux做爲一個自由軟件,在廣大愛好者的支持下,內核版本不斷更新。新的內核修訂了舊內核的bug,並增長了許多新的特性。若是用戶想要使用這些新特性,或想根據本身的系統度身定製一個更高效,更穩定的內核,就須要從新編譯內核。html
本文將以kernel 4.7.2版本爲實驗,操做平臺爲RedHat 7.2,將經過如下三個方面來講明內核及模塊的編譯。 linux
源碼編譯Linux內核
使用Linux內核模塊
實戰:編譯一個NTFS內核模塊,實現Linux掛載NTFS文件系統並實現讀寫功能c++
1. redhat7或者以上版本,本文以vm12+redhat7.2爲例。shell
2. 內核版本下載地址: 到官網:https://cdn.kernel.org vim
查看最新穩定版內核: https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.2.tar.xzapi
也能夠經過命令行下載: wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.2.tar.xz緩存
硬盤可用空間大於8G.不然編譯時,會由於空間不夠,提示你安裝不成功。網絡
虛擬機內存要調到2.5G以上.最好是4G以上,這裏是8G。ide
1. 新添加一塊20G的硬盤及修改內存:工具
2.檢查當前的內核版本: uname -r
3.到官網:https://cdn.kernel.org 查看最新穩定版內核並下載
若是虛擬機不能上網(如何讓虛擬機上網,參考本人相關博文),那也沒有關係,直接從外網下載好後,用xshell工具上傳至虛擬機。如圖:
在xshell的終端輸入rz,打開下面的上傳界面:
上傳便可。
或者點擊下面按鈕也能夠:
若是虛擬機能夠聯網:不妨從虛擬機直接下載。
[root@xiaolyu ~]# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.2.tar.xz
第二步: 使用硬盤:分區、格式化、掛載:
[root@xiaolyu ~]# fdisk /dev/sdb //對磁盤/dev/sdb進行格式化。
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x63b985bb.
Command (m for help): m //查看幫助信息。
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x63b985bb
Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux
Command (m for help):
Command (m for help): w
對磁盤進行格式化: mkfs -t xfs /dev/sdb1
[root@xiaolyu ~]# ls /dev/sdb1
/dev/sdb1
[root@xiaolyu ~]# mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1 isize=256 agcount=4, agsize=1310656 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=5242624, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@xiaolyu ~]#
建立掛載點並進行掛載:
[root@xiaolyu ~]# mkdir /sdb1 //建立掛載點。
[root@xiaolyu ~]# mount /dev/sdb1 /sdb1 //掛載硬盤。
[root@xiaolyu ~]# df -h | tail -1 //驗證是否掛載成功。
/dev/sdb1 20G 33M 20G 1% /sdb1
[root@xiaolyu ~]#
第三步、編譯、安裝linux新內核及模塊。
1. 將源碼包移動到/sdb1中。
2. 檢查系統是否安裝make、gcc、gcc-c++ 、ncurses-devel和庫工具等等
使用rpm -qa 檢測上述工具及庫是否存在。
[root@xiaolyu ~]# rpm -qa | grep make
automake-1.13.4-3.el7.noarch
make-3.82-21.el7.x86_64
[root@xiaolyu ~]# rpm -qa | grep gcc
gcc-4.8.5-4.el7.x86_64
gcc-gfortran-4.8.5-4.el7.x86_64
libgcc-4.8.5-4.el7.x86_64
gcc-c++-4.8.5-4.el7.x86_64
[root@xiaolyu ~]# rpm -qa |grep gcc-c++
gcc-c++-4.8.5-4.el7.x86_64
[root@xiaolyu ~]# rpm -qa | grep ncurses-devel
[root@xiaolyu ~]# yum -y install ncurses-devel #yum 安裝 ncurses-devel動態庫。
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel7-yum | 4.1 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package ncurses-devel.x86_64 0:5.9-13.20130511.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=================================================
Package Arch Version Repository Size
=================================================
Installing:
ncurses-devel x86_64 5.9-13.20130511.el7 rhel7-yum 713 k
Transaction Summary
Total download size: 713 k
Installed size: 2.1 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ncurses-devel-5.9-13.20130511.el7.x86_64 1/1
Verifying : ncurses-devel-5.9-13.20130511.el7.x86_64 1/1
Installed:
ncurses-devel.x86_64 0:5.9-13.20130511.el7
Complete!
[root@xiaolyu ~]#
3.解壓內核源碼包
xz -d 解壓 .xz的壓縮包
tar xf 解壓.tar的壓縮包
[root@xiaolyu sdb1]# ls
linux-4.7.2.tar.xz
[root@xiaolyu sdb1]# xz -d linux-4.7.2.tar.xz
[root@xiaolyu sdb1]# ls
linux-4.7.2.tar
[root@xiaolyu sdb1]# tar xf linux-4.7.2.tar
[root@xiaolyu sdb1]# ls
linux-4.7.2 linux-4.7.2.tar
[root@xiaolyu sdb1]#
[root@xiaolyu sdb1]# ls
linux-4.7.2 linux-4.7.2.tar
[root@xiaolyu sdb1]# cd linux-4.7.2
[root@xiaolyu linux-4.7.2]# ls
arch CREDITS firmware ipc lib net scripts usr
block crypto fs Kbuild MAINTAINERS README security virt
certs Documentation include Kconfig Makefile REPORTING-BUGS sound
COPYING drivers init kernel mm samples tools
[root@xiaolyu linux-4.7.2]# more README
#說明:這個地方能夠查看README文件,每一個源碼包都有,裏面給出了詳細的安裝編譯配置信息。
4. 清理系統緩存。
儘量給內核編譯留出最大的內存空間。
查看系統緩存 free -m :
[root@xiaolyu linux-4.7.2]# free -m
total used free shared buff/cache available
Mem: 7969 611 5341 10 2015 7040
Swap: 2047 0 2047
[root@xiaolyu linux-4.7.2]#
查看默認緩存設置:cat /proc/sys/vm/drop_caches
[root@xiaolyu linux-4.7.2]# free -m
total used free shared buff/cache available
Mem: 7969 611 5341 10 2015 7040
Swap: 2047 0 2047
[root@xiaolyu linux-4.7.2]# cat /proc/sys/vm/drop_caches
0
[root@xiaolyu linux-4.7.2]# sync
[root@xiaolyu linux-4.7.2]# echo 3 > /proc/sys/vm/drop_caches #buff和cache都清空
[root@xiaolyu linux-4.7.2]# free -m
total used free shared buff/cache available
Mem: 7969 571 7214 10 183 7194
Swap: 2047 0 2047
[root@xiaolyu linux-4.7.2]#
#說明:/proc/sys/vm/drop_cashes的值有三個:
0:buff/cache都不要清理。
1:只清理buff。
2:只清理cache。
3:buff和cache都清理。
說明:重啓(reboot和init 6)同樣能清空緩存.
5. 經過圖形界面配置內核編譯參數,生成內核參數配置文件。
make menuconfig 生成內核參數配置文件。
[root@xiaolyu linux-4.7.2]# make menuconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/mconf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/zconf.lex.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTLD scripts/kconfig/mconf
scripts/kconfig/mconf Kconfig
#
# using defaults found in /boot/config-3.10.0-327.el7.x86_64
#
Your display is too small to run Menuconfig!
It must be at least 19 lines by 80 columns.
make[1]: *** [menuconfig] Error 1
make: *** [menuconfig] Error 2
說明:直接在虛擬機的終端執行 make menuconfig出現上述錯誤,屏幕過小了,無法運行Menuconfig ,因而果斷在xshell下執行上述命令:
通過反覆研究,我將字體縮小的時候,當字體爲13的時候,在終端執行上述命令,是不會出現由於顯示不下而報錯的。下面是截圖。
生成.config配置文件,查看此配置文件:
[root@xiaolyu linux-4.7.2]# vim .config
選擇「File system」 而後按回車
由上圖能夠看出,新內核支持多種文件系統。
按【空格鍵】,進入下圖:
用原內核的配置文件,覆蓋新內核的配置文件。這裏說明一下:爲何要用原內核覆蓋新內核,由於內核的配置,比較複雜,能夠參考:http://blog.csdn.net/star_xiong/article/details/17357821
http://blog.csdn.net/xuyuefei1988/article/details/8635539
http://www.linuxidc.com/Linux/2012-06/63092.htm
新舊內核的差異在於ntfs文件系統的支持,因此用老的來覆蓋一下。
若是出現是否覆蓋 n不覆蓋 y 覆蓋,這裏選y覆蓋。
[root@xiaolyu linux-4.7.2]# cp /boot/config-3.10.0-327.el7.x86_64 /sdb1/linux-4.7.2/.config
cp: overwrite ‘/sdb1/linux-4.7.2/.config’? y
[root@xiaolyu linux-4.7.2]#
比較原內核的配置文件和備份的新生成的配置文件的差別:
[root@xiaolyu linux-4.7.2]# diff .config .config_bak
3c3
< # Linux/x86_64 3.10.0-327.el7.x86_64 Kernel Configuration
---
> # Linux/x86 4.7.2 Kernel Configuration
13d12
< CONFIG_HAVE_LATENCYTOP_SUPPORT=y
14a14,17
> CONFIG_ARCH_MMAP_RND_BITS_MIN=28
由於差別實在太大了,想了解具體的差別的朋友,能夠看我另外一篇博文:
<linux內核更新先後配置文件的比較> http://www.cnblogs.com/jasmine-Jobs/p/5808949.html
差別仍是蠻大的。由於太長了,這裏僅僅給出一個局部的截圖:
這個地方爲了快速完成新內核的安裝,採用了修改原配置文件的方法。
修改配置文件,使其支持ntfs讀寫。
[root@xiaolyu linux-4.7.2]# vim .config
五、編譯內核
先檢查openssl-devel 這個包安裝沒有 ,若是沒有,提早安裝這個包openssl-devel
rpm -qa | grep openssl-devel
yum -y install openssl-devel
[root@xiaolyu linux-4.7.2]# make bzImage //生成內核。這個過程很是很是的慢。
中間屢次問你y/n,所有選y,就能夠了。這個過程比較慢。
說明,上述的問題,我已經徹底解決了,問題出在,我是先執行make menuconfig ,而後cp /boot/config-3.10.0-327.el7.x86_64 /sdb1/linux-4.7.2/.config
這樣的結果使得,新生成的內核被徹底覆蓋掉,毫無用處,4.7.2的內核比3.1的內核多的東西都沒有作任何配置。
正確的作法是:先執行cp /boot/config-3.10.0-327.el7.x86_64 /sdb1/linux-4.4/.config 而後再make menuconfig 。
以下圖:
說明在編譯內核: make bzImage 以前,要先安裝一下這個包:openssl-devel,即:
yum -y install openssl-devel
不然會報以下錯誤:
即:
從新: make bzImage:
這裏也是須要一段時間
出現此界面OK!
6、下面 生成新內核的驅動模塊:
[root@xiaolyu linux-4.7.2]# make modules -j 4
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
CHK include/generated/timeconst.h
CHK include/generated/bounds.h
CHK include/generated/asm-offsets.h
CALL scripts/checksyscalls.sh
CC [M] arch/x86/crypto/glue_helper.o
由於這個模塊編譯的過程很是漫長,因此當編譯完成的時候,要echo $? 判斷一下是否成功:
安裝模塊:make modules install
[root@xiaolyu linux-4.7.2]# make modules_install
出現下面的界面說明模塊安裝成功:
2)安裝新編譯的系統內核 : make install
[root@xiaolyu linux-4.7.2]# make install
從新啓動系統,測試新內核的工做狀況
注意,在啓動的時候,須要本身進來一下選擇,不然默認仍是之前的內核啓動哦。除非你在上一步把默認啓動項給改了。
若是你將默認啓動項給修改成4.7.2,那麼會變成以下界面:
使用新內核啓動系統後,查看內核版本:
[root@xiaolyu Desktop ~]# unmae -r