内核,是一个操作系统的核心。它负责管理系统的进程、内存、设备驱动程序、文件和网络系统,决定着系统的性能和稳定性。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 =versio