hl10502的博客

CentOS7.2编译nbd内核模块

CentOS7.2默认没有nbd的内核模块,需要手动编译nbd内核模块。

环境

CentOS7.2

1
2
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

内核版本

1
2
[root@localhost ~]# uname -r
3.10.0-327.el7.x86_64

安装rpmbuild和编译的相关依赖

1
2
[root@localhost ~]# yum install rpm-build -y
[root@localhost ~]# yum install m4 net-tools bc xmlto asciidoc hmaccalc newt-devel perl pesign elfutils-devel binutils-devel bison audit-libs-devel numactl-devel pciutils-devel ncurses-devel libtiff perl-ExtUtils-Embed -y

安装内核rpm

http://vault.centos.org 下载CentOS7.2的kernel src rpm包并使用builder用户安装。

添加builder用户和组

1
2
[root@localhost opt]# useradd builder
[root@localhost opt]# groupadd builder

下载CentOS7.2最新的kernel rpm

1
2
3
[root@localhost ~]# cd /opt/
[root@localhost opt]# wget http://vault.centos.org/7.2.1511/updates/Source/SPackages/kernel-3.10.0-327.36.3.el7.src.rpm
[root@localhost opt]# cp kernel-3.10.0-327.36.3.el7.src.rpm /home/builder

安装kernel-3.10.0-327.36.3.el7.src.rpm

1
2
3
[root@localhost opt]# su builder
[builder@localhost opt]$ cd ~
[builder@localhost ~]$ rpm -ivh kernel-3.10.0-327.36.3.el7.src.rpm

准备编译

创建rpmbuild目录。kernel-3.10.0-327.36.3.el7.src.rpm安装后的源码和kernel.spec文件分别自动生成到~/rpmbuild/SOURCES/目录和~/rpmbuild/SPECS/目录

1
2
3
4
5
6
7
8
9
[builder@localhost ~]$ mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
[builder@localhost ~]$ ls ~/rpmbuild/SOURCES/
centos.cer cpupower.service kernel-3.10.0-ppc64.config kernel-3.10.0-s390x-debug.config linux-3.10.0-327.36.3.el7.tar.xz Module.kabi_s390x
centos-kpatch.x509 debrand-rh-i686-cpu.patch kernel-3.10.0-ppc64-debug.config kernel-3.10.0-s390x-kdump.config linux-kernel-test.patch Module.kabi_x86_64
centos-ldup.x509 debrand-rh_taint.patch kernel-3.10.0-ppc64le.config kernel-3.10.0-x86_64.config Makefile.common secureboot.cer
check-kabi debrand-single-cpu.patch kernel-3.10.0-ppc64le-debug.config kernel-3.10.0-x86_64-debug.config Module.kabi_ppc64 sign-modules
cpupower.config extra_certificates kernel-3.10.0-s390x.config kernel-abi-whitelists-327.tar.bz2 Module.kabi_ppc64le x509.genkey
[builder@localhost ~]$ ls ~/rpmbuild/SPECS/
kernel.spec

解包源代码

1
2
3
[builder@localhost ~]$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros
[builder@localhost ~]$ cd ~/rpmbuild/SPECS
[builder@localhost ~]$ rpmbuild -bp --target=$(uname -m) kernel.spec

编译

执行make menuconfig配置内核配置信息,在弹出的窗口中选择Device Driver -> Block devices -> Network block device support,并在“Network block device support”中输入“M”,保存退出

1
2
[builder@localhost ~]$ cd ~/rpmbuild/BUILD/kernel-3.10.0-327.36.3.el7/linux-3.10.0-327.36.3.el7.x86_64
[builder@localhost linux-3.10.0-327.36.3.el7.x86_64]$ make menuconfig

编译

1
2
[builder@localhost linux-3.10.0-327.36.3.el7.x86_64]$ make prepare && make modules_prepare && make
[builder@localhost linux-3.10.0-327.36.3.el7.x86_64]$ make M=drivers/block -j8

查看nbd模块,并复制到内核目录,加载nbd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost linux-3.10.0-327.36.3.el7.x86_64]# su builder
[builder@localhost linux-3.10.0-327.36.3.el7.x86_64]$ modinfo drivers/block/nbd.ko
filename: /home/builder/rpmbuild/BUILD/kernel-3.10.0-327.36.3.el7/linux-3.10.0-327.36.3.el7.x86_64/drivers/block/nbd.ko
license: GPL
description: Network Block Device
rhelversion: 7.2
srcversion: D8C4A5FDDF180E1B9264E35
depends:
vermagic: 3.10.0 SMP mod_unload modversions
parm: nbds_max:number of network block devices to initialize (default: 16) (int)
parm: max_part:number of partitions per device (default: 0) (int)
parm: debugflags:flags for controlling debug output (int)
[builder@localhost linux-3.10.0-327.36.3.el7.x86_64]$ su root
[root@localhost linux-3.10.0-327.36.3.el7.x86_64]# cp drivers/block/nbd.ko /lib/modules/3.10.0-327.el7.x86_64/extra/
[root@localhost linux-3.10.0-327.36.3.el7.x86_64]# depmod -a && modprobe nbd

nbd.ko共享

复制nbd.ko 到其他CentOS7.2主机上的 /lib/modules/3.10.0-327.el7.x86_64/extra/目录。
比如在节点node232上,加载nbd内核,查看内核

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@node232 ~]# depmod -a && modprobe nbd
[root@node232 ~]# modinfo nbd
filename: /lib/modules/3.10.0-327.el7.x86_64/extra/nbd.ko
license: GPL
description: Network Block Device
rhelversion: 7.2
srcversion: D8C4A5FDDF180E1B9264E35
depends:
vermagic: 3.10.0 SMP mod_unload modversions
parm: nbds_max:number of network block devices to initialize (default: 16) (int)
parm: max_part:number of partitions per device (default: 0) (int)
parm: debugflags:flags for controlling debug output (int)
[root@node232 ~]# lsmod | grep nbd
nbd 17603 0

参考

http://purplepalmdash.github.io/2015/08/13/build-nbd-kernel-module-on-centos7