內核,是一個操作系統(tǒng)的核心。它負責管理系統(tǒng)的進程、內存、設備驅動程序、文件和網(wǎng)絡系統(tǒng),決定著系統(tǒng)的性能和穩(wěn)定性。linux作為一個自由軟件,在廣大愛好者的支持下,內核版本不斷更新。新的內核修訂了舊內核的bug,并增加了許多新的特性。如果用戶想要使用這些新特性,或想根據(jù)自己的系統(tǒng)度身定制一個更高效,更穩(wěn)定的內核,就需要重新編譯內核。
本文將以kernel 4.7.2版本為實驗,操作平臺為redhat 7.2,將通過以下三個方面來說明內核及模塊的編譯。
源碼編譯linux內核
使用linux內核模塊
實戰(zhàn):編譯一個ntfs內核模塊,實現(xiàn)linux掛載ntfs文件系統(tǒng)并實現(xiàn)讀寫功能
一、 源碼編譯linux內核準備工作:
1. redhat7或者以上版本,本文以vm12+redhat7.2為例。
2. 內核版本下載地址:到官網(wǎng):https://cdn.kernel.org
查看最新穩(wěn)定版內核: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.到官網(wǎng):https://cdn.kernel.org 查看最新穩(wěn)定版內核并下載
如果虛擬機不能上網(wǎng)(如何讓虛擬機上網(wǎng),參考本人相關博文),那也沒有關系,直接從外網(wǎng)下載好后,用xshell工具上傳至虛擬機。如圖:
在xshell的終端輸入rz,打開下面的上傳界面:
上傳即可。
或者點擊下面按鈕也可以:
如果虛擬機可以聯(lián)網(wǎng):不妨從虛擬機直接下載。
[root@xiaolyu ~]# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.2.tar.xz
第二步:使用硬盤:分區(qū)、格式化、掛載:
[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 ~]#
創(chuàng)建掛載點并進行掛載:
[root@xiaolyu ~]# mkdir /sdb1 //創(chuàng)建掛載點。
[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.檢查系統(tǒng)是否安裝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動態(tài)庫。
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. 清理系統(tǒng)緩存。
盡可能給內核編譯留出最大的內存空間。
查看系統(tǒng)緩存 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. 通過圖形界面配置內核編譯參數(shù),生成內核參數(shù)配置文件。
make menuconfig 生成內核參數(shù)配置文件。
[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
說明:直接在虛擬機的終端執(zhí)行 make menuconfig出現(xiàn)上述錯誤,屏幕太小了,沒法運行menuconfig ,于是果斷在xshell下執(zhí)行上述命令:
經(jīng)過反復研究,我將字體縮小的時候,當字體為13的時候,在終端執(zhí)行上述命令,是不會出現(xiàn)因為顯示不下而報錯的。下面是截圖。
生成.config配置文件,查看此配置文件:
[root@xiaolyu linux-4.7.2]# vim .config
選擇“file system” 然后按回車
由上圖可以看出,新內核支持多種文件系統(tǒng)。
按【空格鍵】,進入下圖:
用原內核的配置文件,覆蓋新內核的配置文件。這里說明一下:為什么要用原內核覆蓋新內核,因為內核的配置,比較復雜,可以參考:
http://www.linuxidc.com/linux/2012-06/63092.htm
新舊內核的差別在于ntfs文件系統(tǒng)的支持,所以用老的來覆蓋一下。
如果出現(xiàn)是否覆蓋 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,就可以了。這個過程比較慢。
說明,上述的問題,我已經(jīng)完全解決了,問題出在,我是先執(zhí)行make menuconfig ,然后cp /boot/config-3.10.0-327.el7.x86_64 /sdb1/linux-4.7.2/.config
這樣的結果使得,新生成的內核被完全覆蓋掉,毫無用處,4.7.2的內核比3.1的內核多的東西都沒有做任何配置。
正確的做法是:先執(zhí)行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:
這里也是需要一段時間
出現(xiàn)此界面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
出現(xiàn)下面的界面說明模塊安裝成功:
2)安裝新編譯的系統(tǒng)內核 :make install
[root@xiaolyu linux-4.7.2]# make install
重新啟動系統(tǒng),測試新內核的工作情況
注意,在啟動的時候,需要自己進來一下選擇,否則默認還是以前的內核啟動哦。除非你在上一步把默認啟動項給改了。
如果你將默認啟動項給修改為4.7.2,那么會變成如下界面:
使用新內核啟動系統(tǒng)后,查看內核版本:
[root@xiaolyudesktop ~]# unmae -r
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。