centos7单独编译nbd内核模块

前言

centos7默认内核没有带nbd的模块,可以通过下载跟当前版本匹配的内核源码,编译源码指定的模块,然后加载到系统

步骤

判断版本

1
2
3
4
5
6
7
8
[root@lab201 linux-3.10.0-957.el7]# uname -a
Linux lab201 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@lab201 linux-3.10.0-957.el7]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core

没更新过内核的话,就是centos7.6的3.10.0-957版本的内核

获取源码

1
wget https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.6.1810/os/Source/SPackages/kernel-3.10.0-957.el7.src.rpm

清华的源支持vault的库,比centos自带的vault要快,用这个下载

安装匹配版本的devel包

这个编译模块的时候需要

1
2
wget https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.6.1810/os/x86_64/Packages/kernel-devel-3.10.0-957.el7.x86_64.rpm
rpm -ivh kernel-devel-3.10.0-957.el7.x86_64.rpm

安装完了检查下,下面的目录应该不为空

1
ls /lib/modules/3.10.0-957.el7.x86_64/build

解压源码

1
2
3
rpm2cpio kernel-3.10.0-957.el7.src.rpm |cpio -div
xz -d linux-3.10.0-957.el7.tar.xz
cd linux-3.10.0-957.el7/drivers/block/

默认编译不会通过
报错如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@lab201 block]# make CONFIG_BLK_DEV_NBD=m -C /lib/modules/3.10.0-957.el7.x86_64/build M=`pwd` modules
make: Entering directory `/usr/src/kernels/3.10.0-957.el7.x86_64'
CC [M] /root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/floppy.o
CC [M] /root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/brd.o
CC [M] /root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/loop.o
CC [M] /root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/pktcdvd.o
CC [M] /root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/osdblk.o
CC [M] /root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/nbd.o
/root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/nbd.c: In function ‘__nbd_ioctl’:
/root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/nbd.c:619:19: error: ‘REQ_TYPE_SPECIAL’ undeclared (first use in this function)
sreq.cmd_type = REQ_TYPE_SPECIAL;
^
/root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/nbd.c:619:19: note: each undeclared identifier is reported only once for each function it appears in
make[1]: *** [/root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/nbd.o] Error 1
make: *** [_module_/root/rbd/kernel/linux-3.10.0-957.el7/drivers/block] Error 2
make: Leaving directory `/usr/src/kernels/3.10.0-957.el7.x86_64'

修改代码
增加下面内容
这个是从../../include/linux/blkdev.h里面提取的

1
2
3
4
5
6
7
8
9
                mutex_lock(&nbd->tx_lock);
blk_rq_init(NULL, &sreq);
#ifdef __GENKSYMS__
REQ_TYPE_SPECIAL, /* driver defined type */
#else
REQ_TYPE_DRV_PRIV, /* driver defined type */
#endif
sreq.cmd_type = REQ_TYPE_SPECIAL;
nbd_cmd(&sreq) = NBD_CMD_DISC;

修改makefile

1
2
3
4
5
6
7
8
[root@lab201 block]# cat Makefile
#
# Makefile for the kernel block device drivers.
#
# 12 June 2000, Christoph Hellwig <[email protected]>
# Rewritten to use lists instead of if-statements.
#
obj-$(CONFIG_BLK_DEV_NBD) += nbd.o

只保留一个需要的

再次编译

1
2
3
4
5
6
7
[root@lab201 block]# make CONFIG_BLK_DEV_NBD=m -C /lib/modules/3.10.0-957.el7.x86_64/build M=`pwd` modules
make: Entering directory `/usr/src/kernels/3.10.0-957.el7.x86_64'
Building modules, stage 2.
MODPOST 1 modules
CC /root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/nbd.mod.o
LD [M] /root/rbd/kernel/linux-3.10.0-957.el7/drivers/block/nbd.ko
make: Leaving directory `/usr/src/kernels/3.10.0-957.el7.x86_64'

拷贝模块到当前的内核

1
2
3
4
5
6
7
8
[root@lab201 block]# cp -ra nbd.ko /lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/block/
[root@lab201 block]# depmod -a
[root@lab201 block]# modprobe nbd
[root@lab201 block]# modinfo nbd
filename: /lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/block/nbd.ko
···
[root@lab201 block]# lsmod |grep nbd
nbd 17554 0

完成模块的加载了