Building external kernel modules

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search

This article describes how to build a kernel module which is not included into the stock Virtuozzo kernel.
(This article applies to Virtuozzo 7)

Building a kernel module (*.ko)[edit]

Here is an example how to build "via-rhine" kernel module which is in the Virtuozzo kernel source tree, but not enabled in kernel config by default.

// You need to install some dev packages in advance (the list here may be incomplete).
# yum install rpm-build gcc xmlto asciidoc hmaccalc python-devel newt-devel pesign
// If you are going to build a kernel module against some kernel, you need kernel headers for that kernel.
// Assume you want to build a kernel module against currently running kernel.
# yum install vzkernel-devel.x86_64
// Get sources of the module you'd like to build,
// in this particular example the easiest way i believe is just to download the kernel src.rpm.
# cd /tmp
# wget
# rpm -ihv vzkernel-3.10.0-327.3.1.vz7.10.10.src.rpm

// "Prepare" source tree, it's not enough just to take the archive stored in it,
// you need to apply additional patch(es), rpmbuild does this for us.
# rpmbuild -bp /root/rpmbuild/SPECS/kernel.spec --nodeps
// Go to the module source directory.
# cd /root/rpmbuild/BUILD/kernel-3.10.0-327.3.1.el7/linux-3.10.0-327.3.1.vz7.10.10/drivers/net/ethernet/via
// Edit the Makefile so you get the required kernel module compiled.
// In this particular example the via-rhine compiles in-kernel by default, so we need to force it to be built as a module.
# sed -ie 's/$(CONFIG_VIA_RHINE)/m/' Makefile
// Build and install the module.
# make -C /lib/modules/`uname -r`/build M=$PWD
# make -C /lib/modules/`uname -r`/build M=$PWD modules_install
// Check the module has been really copied and load it.
# find /lib/modules -name \*rhine\*

# modprobe via-rhine
# lsmod |grep rhine
via_rhine 32501 0
mii 13934 1 via_rhine

Here you are!

Yellowpin.svg Note: Your case is a bit more complicated? Read Building External Modules

Building a kernel module using Dynamic Kernel Module Support (DKMS)[edit]

(draft : this is a small example of adding back the support of an old network card just after the installation of Virtuozzo 7.0.1 on a bare metal server)

First, you need to figure out which kernel source files are needed to build your modules. Here, what we needed wasn't in the source package but we were lucky and needed only one file : 3c59x.c from (beware of branch and/or tag).

Note : if network is unavailable on the target host, download these packets (lookup /etc/yum.repos.d/ for the EPEL and Virtuozzo URL). Adapt the version numbers to your release :

U glibc-2.17-157.vl7.2.x86_64
U glibc-common-2.17-157.vl7.2.x86_64
U libgcc-4.8.5-11.vl7.x86_64
U libgomp-4.8.5-11.vl7.x86_64

(U = already present but seems to require an update)

The rest of this doc is heavily inspired from

Install dkms and vzkernel-devel.

Create a /usr/src subdirectory named like this modulename-buildversion and place your file(s) there :

mkdir /usr/src/3c59x-0.1
cd /usr/src/3c59x-0.1
cp 3c59x.c /usr/src/3c59x-0.1
echo "obj-m += 3c59x.o" >/usr/src/3c59x-0.1/Makefile

Create the /usr/src/3c59x-0.1/dkms.conf file :


Then add dkms support :

dkms add -m 3c59x -v 0.1
dkms build -m 3c59x -v 0.1
dkms install -m 3c59x -v 0.1

For each kernel update, dkms should rebuild the module for you. There is an option in dkms to build a RPM and maybe allow to do all that work remotely, but not tested, sorry.

Building a kernel module rpm package (kmod)[edit]

TBD, you are welcome to put the description here. :)

--Finist (talk) 05:07, 6 February 2016 (EST)