Modify Kernel module In Ubuntu

  1. get kernel source & header
apt-get source linux-source  
apt install linux-header-$(uname -r)

then the source code should lay in /usr/src as well as the header files

  1. unzio & cp the src folder to anywhere
tar xjf linux-5.4.0.tar.bz2
cp -r linux-source-5.4.0 ~
  1. modify Makefile to make sure the version magic is same as the host kernel
cd /lib/modules/$(uname -a)
modinfo anymod.ko // check the **vermagic** value 

vermagic: 5.4.0-42-generic SMP mod_unload

cd ~/linux-source-5.4.0
vim Makefile

4 SUBLEVEL = 0
5 EXTRAVERSION = -42-generic

  1. copy the kernel "symbol" file into the kernel source directory as well. This file records what function names exist in the running kernel, so that our new module can link to them.
cp /usr/src/linux-headers-5.4.0-42-generic/Module.symvers .
  1. copy the config file
make oldconfig 
or
cp /boot/config-xxxxx .config
make menuconfig
  1. modify your kernel
vim arch/x86/kvm/vmx.c
  1. build the module
make prepare
make modules_prepare
make M=arch/x86/kvm/
  1. cp the module to /lib/modules
sudo cp arch/x86/kvm/kvm.ko /lib/modules/5.4.0-42-generic/kernel/arch/x86/kvm/
  1. make a magic
sudo depmod
  1. replace the old module with new one
rmmod kvm-intel.ko
rmmod kvm.ko
modprobe kvm
modprobe kvm-intel

If those commands run successfully, congulations!
then use dmesg and you may see

module verification failed: signature and/or required key missing - tainting kernel

But the module is actually working now.
Why this warnning appear, to be continue.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容