📜 ⬆️ ⬇️

Automatically building kernel modules with DKMS

In some cases, manually assembling kernel modules is impractical because it is more convenient to use Dynamic Kernel Module Support (DKMS)
The DKMS technology allows, among other things, the automatic assembly of system modules when updating the kernel.

This article discusses automatic control using DKMS using CAN bus modules as an example.

DKMS assumes that a package of a module or a set of kernel modules is located in the / usr / src / {package_name} - {package-version} directory
In addition to the source code of the module / modules, the package directory contains the dkms.conf config, as well as the Makefile according to which it is being built.

Create a module for the DKMS CAN bus module directory /usr/src/can-dkms-4.1.6-200
CAN bus modules are located in two different subdirectories of kernel sources:

Copy these subdirectories (keeping their structure) from the kernel sources into the directory of our can-dkms-4.1.6-200 package
Create a dkms.conf config in the package directory:
PACKAGE_NAME="can-dkms" PACKAGE_VERSION="4.1.6-200" MAKE[0]="make M=$dkms_tree/$PACKAGE_NAME/$PACKAGE_VERSION/build modules" BUILT_MODULE_NAME[0]="can" BUILT_MODULE_LOCATION[0]="net/can" DEST_MODULE_LOCATION[0]="/updates/net/can" BUILT_MODULE_NAME[1]="can-raw" BUILT_MODULE_LOCATION[1]="net/can" DEST_MODULE_LOCATION[1]="/updates/net/can" BUILT_MODULE_NAME[2]="can-bcm" BUILT_MODULE_LOCATION[2]="net/can" DEST_MODULE_LOCATION[2]="/updates/net/can" BUILT_MODULE_NAME[3]="can-gw" BUILT_MODULE_LOCATION[3]="net/can" DEST_MODULE_LOCATION[3]="/updates/net/can" BUILT_MODULE_NAME[4]="vcan" BUILT_MODULE_LOCATION[4]="drivers/net/can" DEST_MODULE_LOCATION[4]="/updates/drivers/net/can" BUILT_MODULE_NAME[5]="can-dev" BUILT_MODULE_LOCATION[5]="drivers/net/can" DEST_MODULE_LOCATION[5]="/updates/drivers/net/can" BUILT_MODULE_NAME[6]="kvaser_usb" BUILT_MODULE_LOCATION[6]="drivers/net/can/usb" DEST_MODULE_LOCATION[6]="/updates/drivers/net/can/usb" AUTOINSTALL="yes" 
The configuration file contains the name and version of the package, the build command, the list of modules to be assembled, and also includes auto-assembly.
')
In the package directory you also need to create a Makefile:
 # net/can export CONFIG_CAN := m export CONFIG_CAN_RAW := m export CONFIG_CAN_BCM := m export CONFIG_CAN_GW := m export CONFIG_CAN_VCAN := m # drives/net/can export CONFIG_CAN_DEV := m export CONFIG_CAN_KVASER_USB := m SRC_DIRS := net/can drivers/net/can modules: $(SRC_DIRS) $(SRC_DIRS): $(MAKE) -C /lib/modules/`uname -r`/build M="$(M)/$@" subdir-ccflags-y=-DCONFIG_CAN_CALC_BITTIMING .PHONY: $(SRC_DIRS) modules 
The Makefile defines the structure of the subdirectories of CAN bus modules, as well as the list of assembled modules.

Thus, the DKMS package for CAN bus modules is created.
Next, you need to register the package in the DKMS tree:
 sudo dkms add -m can-dkms -v 4.1.6-200 
Then the link is created /var/lib/dkms/can-dkms/4.1.6-200/source

Assembling and installing the package is done by the commands:
 sudo dkms build -m can-dkms -v 4.1.6-200 sudo dkms install -m can-dkms -v 4.1.6-200 

If necessary, the package can be removed from the tree with the command:
 sudo dkms remove -m can-dkms/4.1.6-200 --all 


When creating the article, materials of the aur.archlinux.org/packages/linux-can-dkms project were used .

Source: https://habr.com/ru/post/266399/


All Articles