內核,是一個操作系統的核心。它負責管理系統的進程、內存、設備驅動程序、文件和網絡系統,決定着系統的性能和穩定性。Linux作為一個自由軟件,在廣大愛好者的支持下,內核版本不斷更新。新的內核修訂了舊內核的bug,並增加了許多新的特性。如果用戶想要使用這些新特性,或想根據自己的系統度身定制一個更高效,更穩定的內核,就需要重新編譯內核。
本文將以kernel 4.7.2版本為實驗,操作平台為RedHat 7.2,將通過以下三個方面來說明內核及模塊的編譯。
源碼編譯Linux內核
使用Linux內核模塊
實戰:編譯一個NTFS內核模塊,實現Linux掛載NTFS文件系統並實現讀寫功能
一、 源碼編譯linux內核
准備工作:
1. redhat7或者以上版本,本文以vm12+redhat7.2為例。
2. 內核版本下載地址: 到官網:https://cdn.kernel.org
查看最新穩定版內核: https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.2.tar.xz
也可以通過命令行下載: wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.2.tar.xz
虛擬機硬件的要求:
硬盤可用空間大於8G.否則編譯時,會因為空間不夠,提示你安裝不成功。
虛擬機內存要調到2.5G以上.最好是4G以上,這里是8G。
第一步: 對硬件進行設置,使其滿足要求並下載內核:
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
5、編譯內核
先檢查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