- 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
- unzio & cp the src folder to anywhere
tar xjf linux-5.4.0.tar.bz2
cp -r linux-source-5.4.0 ~
- 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
- 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 .
- copy the config file
make oldconfig
or
cp /boot/config-xxxxx .config
make menuconfig
- modify your kernel
vim arch/x86/kvm/vmx.c
- build the module
make prepare
make modules_prepare
make M=arch/x86/kvm/
- cp the module to /lib/modules
sudo cp arch/x86/kvm/kvm.ko /lib/modules/5.4.0-42-generic/kernel/arch/x86/kvm/
- make a magic
sudo depmod
- 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.