Build nbd Kernel Module On CentOS7

TurnToJPG -->

Get Source Code

First check your kernel version via:

$ uname -r

Then find the corresponding kernel source rpm under, download its rpm and install it.

$ wget
# useradd builder
# groupadd builder
$ rpm -ivh kernel-3.10.0-229.7.2.el7.src.rpm

Build Preparation

As a normal user, do following:

$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros
$ cd ~/rpmbuild/SPECS
$ rpmbuild -bp --target=$(uname -m) kernel.spec
$ cd ~/rpmbuild/BUILD/kernel-3.10.0-229.7.2.el7/linux-3.10.0-229.7.2.el7.centos.x86_64/
$ ls
arch     COPYING  Documentation  fs       ipc      kernel       Makefile  README          scripts   tools
block    CREDITS  drivers        include  Kbuild   lib          mm        REPORTING-BUGS  security  usr
configs  crypto   firmware       init     Kconfig  MAINTAINERS  net       samples         sound     virt

Now the source code tree is available.


In the kernel source directory, type make menuconfig for configurating the kernel configuration, select :

Device Driver -> Block devices -> Set “M” On “Network block device support”

Save the configuration and exit, now begin to make via:

$ make prepare && make modules_prepare && make

Now makeout the kernel module and copy it to modules directory:

$ make M=drivers/block -j8
$ modinfo drivers/block/nbd.ko
$ sudo cp drivers/block/nbd.ko /lib/modules/3.10.0-229.7.2.el7.x86_64/extra/
$ sudo depmod -a && sudo modprobe nbd

Finally met some problems of nbd, don’t know why, later will debug on.