先升级一下kernel及安装kernel-devel,保持他们俩的统一
yum install kernel kernel-devel
参看当前内核版本
[root@localhost kernels]# uname -r
3.10.0-1127.13.1.el7.x86_64
然后找对应的kernel源码包。一般yum都是升级到最新的,因此到 http://vault.centos.org/ 的整个7系列的最新版本目录下去找。我这个内核对应的源码包是
http://vault.centos.org/7.8.2003/updates/Source/SPackages/kernel-3.10.0-1127.13.1.el7.src.rpm
然后安装解压这个源码
rpm -ivh kernel-3.10.0-1127.13.1.el7.src.rpm
cd ~/rpmbuild/SOURCES/
tar Jxf linux-3.10.0-1127.13.1.el7.tar.xz -C /usr/src/kernels/
[root@localhost SOURCES]# cd /usr/src/kernels/
[root@localhost kernels]# ll
total 8
drwxr-xr-x. 22 root root 4096 Jul 18 20:00 3.10.0-1127.13.1.el7.x86_64
drwxrwxr-x. 23 root root 4096 Jun 13 02:09 linux-3.10.0-1127.13.1.el7
可以看到有2个目录了,3.10.0-1127.13.1.el7.x86_64是kernel-devel安装的,另一个是我们源码解压缩的
接着就是按部就班的编译过程
mv $(uname -r) $(uname -r)-old
mv linux-3.10.0-1127.13.1.el7/ $(uname -r)
cd $(uname -r)
make mrproper
cp ../$(uname -r)-old/Module.symvers ./
cp /boot/config-$(uname -r) ./.config
make oldconfig
make prepare
make scripts
vi drivers/block/nbd.c
sreq.cmd_type = REQ_TYPE_SPECIAL;
改为
sreq.cmd_type = 7;
make CONFIG_BLK_DEV_NBD=m M=drivers/block CONFIG_STACK_VALIDATION=
cp drivers/block/nbd.ko /lib/modules/$(uname -r)/kernel/drivers/block/
depmod
PS:
网上说的步骤 make CONFIG_BLK_DEV_NBD=m M=drivers/block 会报错
make[1]: *** No rule to make target tools/objtool/objtool', needed by
drivers/block/floppy.o'. Stop.
加入CONFIG_STACK_VALIDATION可破