总述:编写.c文件和Makefile 文件->make->生成点.ko文件->insmode->lsmode->rmmode.
一、编写Hello.c 与 Makefile
ubuntu@ubuntu-VirtualBox:/$ cd ~/Desktop/
ubuntu@ubuntu-VirtualBox:~/Desktop$ cd Mooc/
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ vim helloworld.c
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ vim Makefile
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ ls
helloworld.c Makefile
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ cat helloworld.c
/*************************************************************************
> File Name: helloworld.c
> Author: Allen
> Mail: [email protected]
> Created Time: Wed 09 Aug 2023 11:39:21 AM
************************************************************************/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
// 编写入口函数,函数名可以自己指定
static int __init lkm_init(void)
{
printk("Hello World Allen\n");
return 0;
}
// 编写出口函数,函数名可以自己指定
static void __exit lkm_exit(void)
{
printk("GoodBye Allen\n");
}
// 指定入口点和出口点
module_init(lkm_init);
module_exit(lkm_exit);
// 声明许可证
MODULE_LICENSE("GPL");
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ cat Makefile
// 编写Makefile 文件,Makefile首字母大写
//由于刚才建立的文件是helloworld.c 因此 生成的是helloworld.o
obj-m:=helloworld.o
// 为了使makefile 有较好的移植性,定义两个路径和一个内核版本
CURRENT_PATH:=$(shell pwd)
//保存当前内核版本
LINUX_KERNEL:=$(shell uname -r)
//指定内核的路径
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)
// 下面来编译文件
all:
//make 前面是tab 而非space键
//编译内核模块 放到 当前目录
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
//清空内核模块 路径为当前路径 当前目录
clean:
make -C $(LINUX_KERNEL_PATH) M=$(CURRNT_PATH) clean
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$
二、编译make
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ make
make -C /usr/src/linux-headers-5.4.0-42-generic M=/home/ubuntu/Desktop/Mooc modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-42-generic'
CC [M] /home/ubuntu/Desktop/Mooc/helloworld.o
Building modules, stage 2.
MODPOST 1 modules
CC [M] /home/ubuntu/Desktop/Mooc/helloworld.mod.o
LD [M] /home/ubuntu/Desktop/Mooc/helloworld.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-42-generic'
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ ls
helloworld.c helloworld.mod.c Makefile
helloworld.ko helloworld.mod.o modules.order
helloworld.mod helloworld.o Module.symvers
三、查看内核函数 .h 文件的路径
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ cd /usr/src/
ubuntu@ubuntu-VirtualBox:/usr/src$ ls
linux-headers-5.4.0-42-generic linux-hwe-5.4-headers-5.4.0-42
ubuntu@ubuntu-VirtualBox:/usr/src$ uname -r
5.4.0-42-generic
ubuntu@ubuntu-VirtualBox:/usr/src/linux-headers-5.4.0-42-generic$ ls -l
total 1600
drwxr-xr-x 3 root root 4096 Aug 7 2020 arch
lrwxrwxrwx 1 root root 39 Jan 17 2023 block -> ../linux-hwe-5.4-headers-5.4.0-42/block
lrwxrwxrwx 1 root root 39 Jan 17 2023 certs -> ../linux-hwe-5.4-headers-5.4.0-42/certs
lrwxrwxrwx 1 root root 40 Jan 17 2023 crypto -> ../linux-hwe-5.4-headers-5.4.0-42/crypto
lrwxrwxrwx 1 root root 47 Jan 17 2023 Documentation -> ../linux-hwe-5.4-headers-5.4.0-42/Documentation
lrwxrwxrwx 1 root root 41 Jan 17 2023 drivers -> ../linux-hwe-5.4-headers-5.4.0-42/drivers
lrwxrwxrwx 1 root root 36 Jan 17 2023 fs -> ../linux-hwe-5.4-headers-5.4.0-42/fs
drwxr-xr-x 4 root root 4096 Aug 7 2020 include
lrwxrwxrwx 1 root root 38 Jan 17 2023 init -> ../linux-hwe-5.4-headers-5.4.0-42/init
lrwxrwxrwx 1 root root 37 Jan 17 2023 ipc -> ../linux-hwe-5.4-headers-5.4.0-42/ipc
lrwxrwxrwx 1 root root 40 Jan 17 2023 Kbuild -> ../linux-hwe-5.4-headers-5.4.0-42/Kbuild
lrwxrwxrwx 1 root root 41 Jan 17 2023 Kconfig -> ../linux-hwe-5.4-headers-5.4.0-42/Kconfig
drwxr-xr-x 2 root root 4096 Aug 7 2020 kernel
lrwxrwxrwx 1 root root 37 Jan 17 2023 lib -> ../linux-hwe-5.4-headers-5.4.0-42/lib
lrwxrwxrwx 1 root root 42 Jan 17 2023 Makefile -> ../linux-hwe-5.4-headers-5.4.0-42/Makefile
lrwxrwxrwx 1 root root 36 Jan 17 2023 mm -> ../linux-hwe-5.4-headers-5.4.0-42/mm
-rw-r--r-- 1 root root 1616509 Jul 10 2020 Module.symvers
lrwxrwxrwx 1 root root 37 Jan 17 2023 net -> ../linux-hwe-5.4-headers-5.4.0-42/net
lrwxrwxrwx 1 root root 41 Jan 17 2023 samples -> ../linux-hwe-5.4-headers-5.4.0-42/samples
drwxr-xr-x 7 root root 4096 Aug 7 2020 scripts
lrwxrwxrwx 1 root root 42 Jan 17 2023 security -> ../linux-hwe-5.4-headers-5.4.0-42/security
lrwxrwxrwx 1 root root 39 Jan 17 2023 sound -> ../linux-hwe-5.4-headers-5.4.0-42/sound
drwxr-xr-x 3 root root 4096 Aug 7 2020 tools
lrwxrwxrwx 1 root root 40 Jan 17 2023 ubuntu -> ../linux-hwe-5.4-headers-5.4.0-42/ubuntu
lrwxrwxrwx 1 root root 37 Jan 17 2023 usr -> ../linux-hwe-5.4-headers-5.4.0-42/usr
lrwxrwxrwx 1 root root 38 Jan 17 2023 virt -> ../linux-hwe-5.4-headers-5.4.0-42/virt
ubuntu@ubuntu-VirtualBox:/usr/src/linux-headers-5.4.0-42-generic$ cd include
ubuntu@ubuntu-VirtualBox:/usr/src/linux-headers-5.4.0-42-generic/include$ ls
acpi config dt-bindings kvm media pcmcia scsi target vdso
asm-generic crypto generated linux misc ras soc trace video
clocksource drm keys math-emu net rdma sound uapi xen
ubuntu@ubuntu-VirtualBox:/usr/src/linux-headers-5.4.0-42-generic/include$ cd linux
ubuntu@ubuntu-VirtualBox:/usr/src/linux-headers-5.4.0-42-generic/include/linux$ ls kernel.h
kernel.h
ubuntu@ubuntu-VirtualBox:/usr/src/linux-headers-5.4.0-42-generic/include/linux$ ls module.h
module.h
四、我们是在/proc目录下打印的输出,因此我们去proc下寻找刚才.c 文件中的输出日志信息
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ ls /proc/
1 13 1463 1572 1859 2024 2181 257 336 796 dma mtrr
10 130 1467 1588 1860 2030 22 26 338 797 driver net
1010 131 147 159 1872 2031 2201 2643 339 798 execdomains pagetypeinfo
1031 1325 1471 16 1878 2033 2204 2644 341 8 fb partitions
1035 1327 148 163 1883 2035 2209 27 343 801 filesystems pressure
1042 1329 1484 1664 1886 2037 221 28 344 802 fs sched_debug
1058 1346 1485 1665 1913 2040 2238 285 345 805 interrupts schedstat
1059 135 1489 1689 1923 2043 2242 29 346 807 iomem scsi
11 136 1490 17 1928 2051 2253 2904 349 808 ioports self
1100 1362 1496 1701 1939 2058 2268 3 351 809 irq slabinfo
1111 139 15 1705 1951 2061 2280 30 353 814 kallsyms softirqs
1114 14 1500 1706 1955 2062 2293 305 355 875 kcore stat
12 140 1503 1719 1959 2064 23 307 356 9 keys swaps
123 141 1504 1723 1962 2065 2316 310 4 966 key-users sys
124 142 1505 1725 1965 2067 2326 311 439 acpi kmsg sysrq-trigger
125 1425 1509 1733 1975 2068 24 314 440 asound kpagecgroup sysvipc
126 143 1515 1736 1981 2070 241 316 473 buddyinfo kpagecount thread-self
127 1432 1516 179 1990 2077 243 318 474 bus kpageflags timer_list
1272 1437 1522 18 1994 21 2434 319 479 cgroups loadavg tty
1274 1439 1523 1835 1999 2102 2436 322 6 cmdline locks uptime
128 144 1530 1836 2 2143 244 324 767 consoles mdstat version
129 1442 1538 1847 20 2144 2463 326 769 cpuinfo meminfo version_signature
12920 1444 1542 1848 2003 2152 2464 327 770 crypto misc vmallocinfo
12926 1457 1545 1854 2008 217 25 3291 784 devices modules vmstat
12929 1460 1547 1855 2014 218 2569 332 792 diskstats mounts zoneinfo
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ dmesg
[ 0.000000] Linux version 5.4.0-42-generic (buildd@lgw01-amd64-023) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 (Ubuntu 5.4.0-42.46~18.04.1-generic 5.4.44)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-42-generic root=UUID=3fc194eb-a39e-422d-b0a1-2e44a550ea77 ro quiet splash
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
[ 0.000000] AMD AuthenticAMD
[ 0.000000] Hygon HygonGenuine
[ 0.000000] Centaur CentaurHauls
[ 0.000000] zhaoxin Shanghai
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
[ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
[ 0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format.
[ 30.645137] vboxsf: Successfully loaded version 6.1.38 r153438 on 5.4.0-42-generic SMP mod_unload (LINUX_VERSION_CODE=0x5042c)
[ 30.646678] 03:25:22.566837 automount vbsvcAutomounterMountIt: Successfully mounted 'share' on '/media/sf_share'
[ 605.720046] rfkill: input handler disabled
[ 608.802831] ISO 9660 Extensions: Microsoft Joliet Level 3
[ 608.973310] ISO 9660 Extensions: RRIP_1991A
[ 2226.019032] Hello World Allen
//插入模块
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ sudo insmod helloworld.ko
//打印模块信息
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ lsmod
Module Size Used by
helloworld 16384 0
nls_utf8 16384 1
isofs 49152 1
vboxsf 81920 1
vboxvideo 36864 0
dsm_driver 241664 1
binfmt_misc 24576 1
intel_rapl_msr 20480 0
intel_rapl_common 24576 1 intel_rapl_msr
crct10dif_pclmul 16384 1
crc32_pclmul 16384 0
ghash_clmulni_intel 16384 0
//删除模块
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ sudo rmmod helloworld
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ dmesg
[ 0.000000] Linux version 5.4.0-42-generic (buildd@lgw01-amd64-023) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 (Ubuntu 5.4.0-42.46~18.04.1-generic 5.4.44)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-42-generic root=UUID=3fc194eb-a39e-422d-b0a1-2e44a550ea77 ro quiet splash
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
[ 0.000000] AMD AuthenticAMD
[ 0.000000] Hygon HygonGenuine
[ 0.000000] Centaur CentaurHauls
[ 0.000000] zhaoxin Shanghai
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
[ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
[ 0.000000] x86/fpu: Enabled xstate feat
[ 30.645094] *** VALIDATE vboxsf ***
[ 30.645098] vboxsf: Successfully loaded version 6.1.38 r153438
[ 30.645137] vboxsf: Successfully loaded version 6.1.38 r153438 on 5.4.0-42-generic SMP mod_unload (LINUX_VERSION_CODE=0x5042c)
[ 30.646678] 03:25:22.566837 automount vbsvcAutomounterMountIt: Successfully mounted 'share' on '/media/sf_share'
[ 605.720046] rfkill: input handler disabled
[ 608.802831] ISO 9660 Extensions: Microsoft Joliet Level 3
[ 608.973310] ISO 9660 Extensions: RRIP_1991A
[ 2226.019032] Hello World Allen
buntu@ubuntu-VirtualBox:~/Desktop/Mooc$ sudo rmmod helloworld
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ dmesg
[ 0.000000] Linux version 5.4.0-42-generic (buildd@lgw01-amd64-023) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 (Ubuntu 5.4.0-42.46~18.04.1-generic 5.4.44)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.4.0-42-generic root=UUID=3fc194eb-a39e-422d-b0a1-2e44a550ea77 ro quiet splash
[ 30.645094] *** VALIDATE vboxsf ***
[ 30.645098] vboxsf: Successfully loaded version 6.1.38 r153438
[ 30.645137] vboxsf: Successfully loaded version 6.1.38 r153438 on 5.4.0-42-generic SMP mod_unload (LINUX_VERSION_CODE=0x5042c)
[ 30.646678] 03:25:22.566837 automount vbsvcAutomounterMountIt: Successfully mounted 'share' on '/media/sf_share'
[ 605.720046] rfkill: input handler disabled
[ 608.802831] ISO 9660 Extensions: Microsoft Joliet Level 3
[ 608.973310] ISO 9660 Extensions: RRIP_1991A
[ 2226.019032] Hello World Allen
[ 2276.922629] GoodBye Allen
//查看模块 就没有helloworld 模块了
ubuntu@ubuntu-VirtualBox:~/Desktop/Mooc$ lsmod
Module Size Used by
nls_utf8 16384 1
isofs 49152 1
vboxsf 81920 1
vboxvideo 36864 0
dsm_driver 241664 1
binfmt_misc 24576 1
intel_rapl_msr 20480 0
标签:linux,5.4,Linux,ubuntu,42,Helloworld,headers,Ubuntu,root From: https://www.cnblogs.com/wang-xiao-shuai/p/17616678.html