当前位置:   article > 正文

Linux DKMS

Linux DKMS

DKMS(Dynamic Kernel Module Support)是一个框架,用于构建和安装内核模块。它允许第三方内核模块在系统内核升级时自动重新构建和安装,从而确保这些模块与新内核版本兼容。

DKMS的工作原理是将内核模块的源代码和安装脚本存储在一个特定的位置,并在内核升级时重新构建和安装这些模块。这样,即使系统内核更新,这些第三方模块也可以保持有效,而无需手动重新编译和安装。

DKMS的主要优点包括:

1. 自动化:DKMS可以自动处理内核模块的构建和安装过程,简化了管理第三方模块的流程。

2. 兼容性:通过DKMS,第三方模块可以与新内核版本保持兼容,无需手动干预。

3. 灵活性:DKMS支持各种不同类型的内核模块,使其适用于各种不同的需求和场景。

安装依赖

  1. sudo apt install linux-headers-$(uname -r)
  2. sudo apt install gcc make

安装DKMS

  1. root@velinux:~# apt install dkms
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. Suggested packages:
  6. menu
  7. The following NEW packages will be installed:
  8. dkms
  9. 0 upgraded, 1 newly installed, 0 to remove and 457 not upgraded.
  10. Need to get 51.5 kB of archives.
  11. After this operation, 200 kB of additional disk space will be used.
  12. Get:1 http://deb.debian.org/debian sid/main amd64 dkms all 3.0.12-4 [51.5 kB]
  13. Fetched 51.5 kB in 1s (48.4 kB/s)
  14. Selecting previously unselected package dkms.
  15. (Reading database ... 72640 files and directories currently installed.)
  16. Preparing to unpack .../archives/dkms_3.0.12-4_all.deb ...
  17. Unpacking dkms (3.0.12-4) ...
  18. Setting up dkms (3.0.12-4) ...
  19. Processing triggers for man-db (2.8.5-2) ...
  20. root@velinux:~#
  21. root@velinux:~#
  22. root@velinux:~# dkms
  23. Error! Unknown action specified: ""
  24. Usage: /usr/sbin/dkms [action] [options]
  25. [action] = { add | remove | build | install | uninstall | match | autoinstall |
  26. mktarball | ldtarball | status }
  27. [options] = [-m module] [-v module-version] [-k kernel-version] [-a arch]
  28. [-c dkms.conf-location] [-q] [--force] [--force-version-override] [--all]
  29. [--templatekernel=kernel] [--directive='cli-directive=cli-value']
  30. [--config=kernel-.config-location] [--archive=tarball-location]
  31. [--kernelsourcedir=source-location]
  32. [--binaries-only] [--source-only] [--verbose]
  33. [--no-depmod] [--modprobe-on-install] [-j number] [--version]

制作dkms目录结构和路径/usr/src/hello-1.0.0

  1. bsp-server@bsp:/usr/src/hello-1.0.0$ tree .
  2. .
  3. ├── dkms.conf
  4. ├── hello.c
  5. └── Makefile
  6. 0 directories, 3 files

dkms配置文件

  1. bsp-server@bsp:~$ cat /usr/src/hello-1.0.0/dkms.conf
  2. PACKAGE_NAME="hello"
  3. PACKAGE_VERSION="1.0.0"
  4. CLEAN="make clean"
  5. MAKE[0]="make all"
  6. BUILT_MODULE_NAME[0]="hello"
  7. DEST_MODULE_LOCATION[0]="/updates"
  8. 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驱动代码

  1. bsp-server@bsp:~$ cat /usr/src/hello-1.0.0/hello.c
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. static int __init hello_init(void)
  5. {
  6. printk(KERN_INFO "Hello world!\n");
  7. return 0;
  8. }
  9. static void __exit hello_exit(void)
  10. {
  11. printk(KERN_INFO "Goodbye world!\n");
  12. }
  13. module_init(hello_init);
  14. module_exit(hello_exit);
  15. MODULE_LICENSE("GPL");
  16. MODULE_AUTHOR("Your Name");
  17. MODULE_DESCRIPTION("A simple hello world driver");

Makefile代码

  1. bsp-server@bsp:~$ cat /usr/src/hello-1.0.0/Makefile
  2. obj-m += hello.o
  3. all:
  4. make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
  5. clean:
  6. make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  7. install:
  8. make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install
  9. depmod -a
  10. uninstall:
  11. rmmod hello
  12. rm -f /lib/modules/$(shell uname -r)/extra/hello.ko
  13. depmod -a
  14. .PHONY: all clean install uninstall

添加

  1. bsp-server@bsp:~$ sudo dkms add -m hello -v 1.0.0
  2. Creating symlink /var/lib/dkms/hello/1.0.0/source -> /usr/src/hello-1.0.0

报错提示

  1. 1.拷贝到/usr/src/hello-1.0.0
  2. bsp-server@bsp:~/dkms_test$ sudo dkms add -m hello -v 1.0.0
  3. Error! Could not find module source directory.
  4. Directory: /usr/src/hello-1.0.0 does not exist.
  5. 2.创建'/kernel', '/updates', or '/extra'其中一个目录填在/usr/src/hello-1.0.0/dkms.conf中的DEST_MODULE_LOCATION[0]
  6. bsp-server@bsp:~$ sudo dkms add -m hello -v 1.0.0
  7. dkms.conf: Error! Directive 'DEST_MODULE_LOCATION' does not begin with
  8. '/kernel', '/updates', or '/extra' in record #0.
  9. Error! Bad conf file.
  10. File: /usr/src/hello-1.0.0/dkms.conf does not represent a valid dkms.conf file.

dkms编译

  1. bsp-server@bsp:~$ dkms build -m hello -v 1.0.0
  2. Error! No write access to create a DKMS tree at /var/lib/dkms
  3. bsp-server@bsp:~$ sudo dkms build -m hello -v 1.0.0
  4. Kernel preparation unnecessary for this kernel. Skipping...
  5. Building module:
  6. cleaning build area...
  7. make -j40 KERNELRELEASE=6.5.0-15-generic all...
  8. Signing module:
  9. Generating a new Secure Boot signing key:
  10. Can't load /var/lib/shim-signed/mok/.rnd into RNG
  11. 801B5B1FF57F0000:error:12000079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:106:Filename=/var/lib/shim-signed/mok/.rnd
  12. ...+...+..+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+..+...+.......+...+........+.+......+...+...........+...+...+....+......+.....+.........+....+..+.........+................+.........+...+.....+......+......+.+....................+...+.+......+...+........................+..+....+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  13. ............+....+...............+.....+.+........+....+...+..+..........+...+........+......+...+.+......+...+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*....+.+..............+....+.....+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...+...+...+..+.......+...+..+......+..........+..+...+.+.....+................+........+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  14. -----
  15. - /var/lib/dkms/hello/1.0.0/6.5.0-15-generic/x86_64/module/hello.ko
  16. Secure Boot not enabled on this system.
  17. cleaning build area...

dkms安装

  1. bsp-server@bsp:~$ sudo dkms install -m hello -v 1.0.0
  2. hello.ko:
  3. Running module version sanity check.
  4. - Original module
  5. - No original module exists within this kernel
  6. - Installation
  7. - Installing to /lib/modules/6.5.0-15-generic/updates/dkms/
  8. depmod..........

查看dkms状态

  1. bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms status
  2. hello/1.0.0, 6.5.0-15-generic, x86_64: installed
  3. bsp-server@bsp:/usr/src/hello-1.0.0$

dkms的unstall

  1. bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms uninstall -m hello -v 1.0.0
  2. Module hello-1.0.0 for kernel 6.5.0-15-generic (x86_64).
  3. Before uninstall, this module version was ACTIVE on this kernel.
  4. hello.ko:
  5. - Uninstallation
  6. - Deleting from: /lib/modules/6.5.0-15-generic/updates/dkms/
  7. - Original module
  8. - No original module was found for this module on this kernel.
  9. - Use the dkms install command to reinstall any previous module version.
  10. depmod...
  11. bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms status
  12. hello/1.0.0, 6.5.0-15-generic, x86_64: built

dkms的remove

  1. bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms remove -m hello -v 1.0.0
  2. Module hello-1.0.0 for kernel 6.5.0-15-generic (x86_64).
  3. This module version was INACTIVE for this kernel.
  4. depmod...
  5. Deleting module hello-1.0.0 completely from the DKMS tree.
  6. bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms status
  7. bsp-server@bsp:/usr/src/hello-1.0.0$

dkms制作deb包

可以转成deb、rpm使用,还有其他具体dkms -h看看

  1. bsp-server@bsp:/usr/src/hello-1.0.0$ sudo dkms mkdeb -m hello -v 1.0.0
  2. Using /etc/dkms/template-dkms-mkdeb
  3. copying template...
  4. modifying debian/changelog...
  5. modifying debian/compat...
  6. modifying debian/control...
  7. modifying debian/copyright...
  8. modifying debian/dirs...
  9. modifying debian/postinst...
  10. modifying debian/prerm...
  11. modifying debian/README.Debian...
  12. modifying debian/rules...
  13. copying legacy postinstall template...
  14. Copying source tree...
  15. Building binary package...dpkg-buildpackage: warning: using a gain-root-command while being root
  16. dpkg-source --before-build .
  17. fakeroot debian/rules clean
  18. dh_clean: warning: Compatibility levels before 10 are deprecated (level 7 in use)
  19. debian/rules build
  20. fakeroot debian/rules binary
  21. dh_installdirs: warning: Compatibility levels before 10 are deprecated (level 7 in use)
  22. dh_strip: warning: Compatibility levels before 10 are deprecated (level 7 in use)
  23. dh_compress: warning: Compatibility levels before 10 are deprecated (level 7 in use)
  24. dh_installdeb: warning: Compatibility levels before 10 are deprecated (level 7 in use)
  25. dh_shlibdeps: warning: Compatibility levels before 10 are deprecated (level 7 in use)
  26. dpkg-genbuildinfo --build=binary -O../hello-dkms_1.0.0_amd64.buildinfo
  27. dpkg-genchanges --build=binary -O../hello-dkms_1.0.0_amd64.changes
  28. dpkg-source --after-build .
  29. Moving built files to /var/lib/dkms/hello/1.0.0/deb...
  30. Cleaning up temporary files...
  31. bsp-server@bsp:/usr/src/hello-1.0.0$ ls /var/lib/dkms/hello/1.0.0/deb/
  32. hello-dkms_1.0.0_amd64.deb
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号