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 https://download.openvz.org/virtuozzo/factory/source/SRPMS/v/vzkernel-3.10.0-327.3.1.vz7.10.10.src.rpm
# 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\*
/lib/modules/3.10.0-327.3.1.vz7.10.10/extra/via-rhine.ko

# 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 https://download.openvz.org/virtuozzo/releases/openvz-7.0.1-554/source/SRPMS/v/vzkernel-3.10.0-327.36.1.vz7.18.7.src.rpm but we were lucky and needed only one file : 3c59x.c from https://src.openvz.org/projects/OVZ/repos/vzkernel/browse/drivers/net/ethernet/3com (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 :

cpp-4.8.5-11.vl7.x86_64
dkms-2.3-2.20161202gitde1dca9.vl7.noarch
gcc-4.8.5-11.vl7.x86_64
U glibc-2.17-157.vl7.2.x86_64
U glibc-common-2.17-157.vl7.2.x86_64
glibc-devel-2.17-157.vl7.2.x86_64
glibc-headers-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
libmpc-1.0.1-3.vl7.x86_64
mpfr-3.1.1-4.vl7.x86_64
vzkernel-devel-3.10.0-327.36.1.vz7.18.7.x86_64
vzkernel-headers-3.10.0-327.36.1.vz7.18.7.x86_64

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

The rest of this doc is heavily inspired from https://wiki.centos.org/HowTos/BuildingKernelModules

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 :

PACKAGE_NAME="3c59x"
PACKAGE_VERSION="0.1"
BUILT_MODULE_NAME[0]="3c59x"
DEST_MODULE_LOCATION[0]="/kernel/drivers/net/ethernet/3com/"
AUTOINSTALL="yes"

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)