DKMS(Dynamic Kernel Module Support)是一个框架,用于构建和安装内核模块。它允许第三方内核模块在系统内核升级时自动重新构建和安装,从而确保这些模块与新内核版本兼容。
DKMS的工作原理是将内核模块的源代码和安装脚本存储在一个特定的位置,并在内核升级时重新构建和安装这些模块。这样,即使系统内核更新,这些第三方模块也可以保持有效,而无需手动重新编译和安装。
DKMS的主要优点包括:
-
自动化:DKMS可以自动处理内核模块的构建和安装过程,简化了管理第三方模块的流程。
-
兼容性:通过DKMS,第三方模块可以与新内核版本保持兼容,无需手动干预。
-
灵活性:DKMS支持各种不同类型的内核模块,使其适用于各种不同的需求和场景。
安装依赖
sudo apt install linux-headers-$(uname -r)
sudo apt install gcc make
安装DKMS
root@velinux:~# apt install dkms
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
menu
The following NEW packages will be installed:
dkms
0 upgraded, 1 newly installed, 0 to remove and 457 not upgraded.
Need to get 51.5 kB of archives.
After this operation, 200 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian sid/main amd64 dkms all 3.0.12-4 [51.5 kB]
Fetched 51.5 kB in 1s (48.4 kB/s)
Selecting previously unselected package dkms.
(Reading database ... 72640 files and directories currently installed.)
Preparing to unpack .../archives/dkms_3.0.12-4_all.deb ...
Unpacking dkms (3.0.12-4) ...
Setting up dkms (3.0.12-4) ...
Processing triggers for man-db (2.8.5-2) ...
root@velinux:~#
root@velinux:~#
root@velinux:~# dkms
Error! Unknown action specified: ""
Usage: /usr/sbin/dkms [action] [options]
[action] = { add | remove | build | install | uninstall | match | autoinstall |
mktarball | ldtarball | status }
[options] = [-m module] [-v module-version] [-k kernel-version] [-a arch]
[-c dkms.conf-location] [-q] [--force] [--force-version-override] [--all]
[--templatekernel=kernel] [--directive='cli-directive=cli-value']
[--config=kernel-.config-location] [--archive=tarball-location]
[--kernelsourcedir=source-location]
[--binaries-only] [--source-only] [--verbose]
[--no-depmod] [--modprobe-on-install] [-j number] [--version]
制作dkms目录结构和路径/usr/src/hello-1.0.0
bsp-server@bsp:/usr/src/hello-1.0.0$ tree .
.
├── dkms.conf
├── hello.c
└── Makefile
0 directories, 3 files
dkms配置文件
bsp-server@bsp:~$ cat /usr/src/hello-1.0.0/dkms.conf
PACKAGE_NAME="hello"
PACKAGE_VERSION="1.0.0"
CLEAN="make clean"
MAKE[0]="make all"
BUILT_MODULE_NAME[0]="hello"
DEST_MODULE_LOCATION[0]="/updates"
AUTOINSTALL="yes"
PACKAGE_NAME="hello":指定DKMS包的名称为"hello"
PACKAGE_VERSION="1.0.0":指定DKMS包的版本号为"1.0.0"
CLEAN="make clean":指定在构建新模块之前要执行的清理操作。当执行dkms build命令时,会先执行make clean命令来清理之前构建的模块。
-MAKE[0]="make all":指定构建模块时要执行的命令。在这里,执行make al`命令来编译模块源代码。
BUILT_MODULE_NAME[0]="hello":指定要构建的模块的名称为"hello",这与源代码中的模块名称对应。
DEST_MODULE_LOCATION[0]="/updates":指定安装模块的目标位置。在这里,将构建的模块安装到"
/home/bsp-server/dkms_dest"目录中。
AUTOINSTALL="yes":指定是否自动安装模块。设置为"yes"时,DKMS会在构建成功后自动安装模块。
Hello驱动代码
bsp-server@bsp:~$ cat /usr/src/hello-1.0.0/hello.c
#include <linux/init.h>
#include <linux/module.h>
static int __init hello_init(void)
{
printk(KERN_INFO "Hello world!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_INFO "Goodbye world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple hello world driver");
Makefile代码
bsp-server@bsp:~$ cat /usr/src/hello-1.0.0/Makefile
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
install:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install
depmod -a
uninstall:
rmmod hello
rm -f /lib/modules/$(shell uname -r)/extra/hello.ko
depmod -a
.PHONY: all clean install uninstall
添加
bsp-server@bsp:~$ sudo dkms add -m hello -v 1.0.0
Creating symlink /var/lib/dkms/hello/1.0.0/source -> /usr/src/hello-1.0.0
报错提示
1.拷贝到/usr/src/hello-1.0.0
bsp-server@bsp:~/dkms_test$ sudo dkms add -m hello -v 1.0.0
Error! Could not find module source directory.
Directory: /usr/src/hello-1.0.0 does not exist.
2.创建'/kernel', '/updates', or '/extra'其中一个目录填在/usr/src/hello-1.0.0/dkms.conf中的DEST_MODULE_LOCATION[0]
bsp-server@bsp:~$ sudo dkms add -m hello -v 1.0.0
dkms.conf: Error! Directive 'DEST_MODULE_LOCATION' does not begin with
'/kernel', '/updates', or '/extra' in record #0.
Error! Bad conf file.
File: /usr/src/hello-1.0.0/dkms.conf does not represent a valid dkms.conf file.
dkms编译
bsp-server@bsp:~$ dkms build -m hello -v 1.0.0
Error! No write access to create a DKMS tree at /var/lib/dkms
bsp-server@bsp:~$ sudo dkms build -m hello -v 1.0.0
Kernel preparation unnecessary for this kernel. Skipping...
Building module:
cleaning build area...
make -j40 KERNELRELEASE=6.5.0-15-generic all...
Signing module:
Generating a new Secure Boot signing key:
Can't load /var/lib/shim-signed/mok/.rnd into RNG
801B5B1FF57F0000:error:12000079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:106:Filename=/var/lib/shim-signed/mok/.rnd
...+...+..+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+..+...+.......+...+........+.+......+...+...........+...+...+....+......+.....+.........+....+..+.........+................+.........+...+.....+......+......+.+....................+...+.+......+...+........................+..+....+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
............+....+...............+.....+.+........+....+...+..+..........+...+........+......+...+.+......+...+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*....+.+..............+....+.....+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...+...+...+..+.......+...+..+......+..........+..+...+.+.....+................+........+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
- /var/lib/dkms/hello/1.0.0/6.5.0-15-generic/x86_64/module/hello.ko
Secure Boot not enabled on this system.
cleaning build area...
dkms安装
bsp-server@bsp:~$ sudo dkms install -m hello -v 1.0.0
hello.ko:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/6.5.0-15-generic/updates/dkms/
depmod..........
查看dkms状态
bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms status
hello/1.0.0, 6.5.0-15-generic, x86_64: installed
bsp-server@bsp:/usr/src/hello-1.0.0$
dkms的unstall
bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms uninstall -m hello -v 1.0.0
Module hello-1.0.0 for kernel 6.5.0-15-generic (x86_64).
Before uninstall, this module version was ACTIVE on this kernel.
hello.ko:
- Uninstallation
- Deleting from: /lib/modules/6.5.0-15-generic/updates/dkms/
- Original module
- No original module was found for this module on this kernel.
- Use the dkms install command to reinstall any previous module version.
depmod...
bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms status
hello/1.0.0, 6.5.0-15-generic, x86_64: built
dkms的remove
bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms remove -m hello -v 1.0.0
Module hello-1.0.0 for kernel 6.5.0-15-generic (x86_64).
This module version was INACTIVE for this kernel.
depmod...
Deleting module hello-1.0.0 completely from the DKMS tree.
bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms status
bsp-server@bsp:/usr/src/hello-1.0.0$
dkms制作deb包
可以转成deb、rpm使用,还有其他具体dkms -h看看
bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms mkdeb -m hello -v 1.0.0
Using /etc/dkms/template-dkms-mkdeb
copying template...
modifying debian/changelog...
modifying debian/compat...
modifying debian/control...
modifying debian/copyright...
modifying debian/dirs...
modifying debian/postinst...
modifying debian/prerm...
modifying debian/README.Debian...
modifying debian/rules...
copying legacy postinstall template...
Copying source tree...
Building binary package...dpkg-buildpackage: warning: using a gain-root-command while being root
dpkg-source --before-build .
fakeroot debian/rules clean
dh_clean: warning: Compatibility levels before 10 are deprecated (level 7 in use)
debian/rules build
fakeroot debian/rules binary
dh_installdirs: warning: Compatibility levels before 10 are deprecated (level 7 in use)
dh_strip: warning: Compatibility levels before 10 are deprecated (level 7 in use)
dh_compress: warning: Compatibility levels before 10 are deprecated (level 7 in use)
dh_installdeb: warning: Compatibility levels before 10 are deprecated (level 7 in use)
dh_shlibdeps: warning: Compatibility levels before 10 are deprecated (level 7 in use)
dpkg-genbuildinfo --build=binary -O../hello-dkms_1.0.0_amd64.buildinfo
dpkg-genchanges --build=binary -O../hello-dkms_1.0.0_amd64.changes
dpkg-source --after-build .
Moving built files to /var/lib/dkms/hello/1.0.0/deb...
Cleaning up temporary files...
bsp-server@bsp:/usr/src/hello-1.0.0$ ls /var/lib/dkms/hello/1.0.0/deb/
hello-dkms_1.0.0_amd64.deb