首页 > 系统相关 >Linux Ubuntu Helloworld 模块 的编译 插入 删除

Linux Ubuntu Helloworld 模块 的编译 插入 删除

时间:2023-08-09 13:55:45浏览次数:345  
标签:linux 5.4 Linux ubuntu 42 Helloworld headers Ubuntu root

总述:编写.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

相关文章

  • linux配置 崩溃生成core 转储
    ulimit-c查看core文件大小限制0表达不生成ulimit-cunlimited设置为不限制vim/proc/sys/kernel/core_pattern编辑生成规则|/usr/share/apport/apport%p%s%c%d%P%E%p:进程ID(PID)%s:进程名称(通常是可执行文件的名称)%c:命令行参数%d:应用程序的工作目录%P:包含应......
  • linux修改时区为上海,linux修改时区 东八区
    linux修改时区为上海,linux修改时区东八区原文链接:https://www.jingjia.net/article/yingxiao741067.htmllinux系统修改系统时间与时区的方法有哪些修改系统时间。linux系统时钟有两个,一个是硬件时钟,即BIOS时间,就是我们进行CMOS设置时看到的时间,另一个是系统时钟,是linux......
  • Linux终端命令行提示符PS1格式修改
    zsh先确定你是用的shell类型,运行echo$SHELL如果你使用的是zsh,那么修改/etc/zshrc其中有一行设置PS1的值的,修改为PS1="%n@%m%1~%#"其它的zsh中转义变量%T系统时间(时:分)%*系统时间(时:分:秒)%D系统日期(年-月-日)%n你的用户名%B-%b开始到结束使用粗体打印%......
  • Linux:如何在脚本中判断某个指令是否存在
    学习自:检查linux命令是否存在的正确方式指令:which用法:which命令结果:一个字符串存在:输出命令对应的可执行文件地址不存在:输出信息/usr/bin/which:nowgetin(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin),但是如果用字符串变量去承接时,会发现字符串......
  • Linux 网络管理
    Linux网络管理目录Linux网络管理一.什么是网络?1.1网络的定义1.2网络传输介质1.3网络设备1.交换机2.路由器3.网关路由4.区别二.Linux处理数据包过程4.1详解4.2开启内核转发三.OSI7层网络模型3.1定义3.2每层解释四.TCP/IP(四层模型)4.1TCP和UDP区别4.2TCP3......
  • Linux命令(60)之time
    linux命令之time1.time介绍linux命令time是用来显示特定指令执行时所需消耗的CPU时间2.time用法time[参数]command[选项]常用参数无。3.实例3.1.显示date的执行时间命令:timedate[root@rhel77~]#timedateMonJul1709:09:39CST2023real 0m0.001suser 0m0.000ssys 0m0......
  • ubuntu22.04问题:Method https has died unexpectedly!
    问题当我们执行update的时候,有时候会遇到下面这种情况apt-getupdate命中:1http://mirrors.tuna.tsinghua.edu.cn/ubuntujammyInRelease命中:2http://mirrors.tuna.tsinghua.edu.cn/ubuntujammy-updatesInRelease命中:3http://mirrors.tuna.tsinghua.edu.cn/ubuntuj......
  • rocky linux:安装ffmpeg(ffmpeg 5.1.3/rocky linux 9.2)
    一,ffmpeg官网:网址:http://ffmpeg.org/如图:说明:最新版本出到了6.0,我们从dnf通道安装,版本可能略低二,安装rpmfusion库[root@img~]#dnfinstall--nogpgcheckhttps://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-9.noarch.rpm[root@img~]#dnfinstall......
  • rocky linux:安装pytorch(pytorch 2.0.1 / Python 3.9.16)
    一,pytorch官网:https://pytorch.org/如图:根据自己的需求选择版本、平台、语言环境等信息,然后运行命令二,运行pip安装命令:[root@imgbin]#pip3installtorchtorchvisiontorchaudio--index-urlhttps://download.pytorch.org/whl/cpuLookinginindexes:https://......
  • rocky linux:编译安装python3.11.4(rocky linux 9.2)
    一,查看现有的版本:1,本地版本[root@img~]#python--versionPython3.9.162,现在的最新版本:访问官网:https://www.python.org/如图:可以看到线上的最新版本是3.11.4 二,编译/安装:1,下载:先复制下载地址2,从服务器用wget命令下载:[root@imgpython]#wgethttp......