????Linux?????????????ж????顣?????????ò??????±??????????????????????????С??????????С?????????£?Linux?豸??????????????????????????????
????????????????? .ko ?? ???λ?? /lib/modules/<kernel_version>/kernel ?????
??????????????”hello world”??????????飬????????????ο?????
??????????????????????????
????· lsmod: ????????????????
[root@centos1 vagrant]# lsmod
Module                  Size  Used by
nfnetlink_queue        18197  0
nfnetlink              14606  1 nfnetlink_queue
iptable_filter         12810  0
xt_NFQUEUE             12697  0
vboxsf                 39725  1
crct10dif_pclmul       14289  0
crc32_pclmul           13113  0
crc32c_intel           22079  0
ghash_clmulni_intel    13259  0
ppdev                  17671  0

????· insmod: ??????????
[root@centos1 hello]# insmod ./hello.ko
[root@centos1 hello]# lsmod |grep hello
hello                  12428  0
????· modinfo: ???????????
[root@centos1 hello]# modinfo hello.ko
filename:       /home/vagrant/hello/hello.ko
description:    A Simple Hello World module
author:         flygoast
license:        GPL
rhelversion:    7.1
srcversion:     2E67B30196A57711CD2CC3E
depends:
vermagic:       3.10.0-229.14.1.el7.x86_64 SMP mod_unload modversions
????· rmmod: ????????飬 ???????????????黹??????????????????????
????[root@centos1 hello]# rmmod hello.ko
????[root@centos1 hello]#
????· modprobe: ??????????????????????????????飬????????ο?man modprobe
??????????????????????????????
?????????????????????????????????????CentOS7?????YUM?????:
????yum install -y kernel-devel
????????????? hello ?? ???????б????????? hello.c ?? ????????:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("flygoast");
MODULE_DESCRIPTION("A Simple Hello World module");
static int __init hello_init(void)
{
printk(KERN_INFO "Hello world! ");
return 0;
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "Cleaning up module. ");
}
module_init(hello_init);
module_exit(hello_cleanup);
???????? Makefile ?????????????:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
??????????????????:
[root@centos1 hello]# make
make -C /lib/modules/3.10.0-229.14.1.el7.x86_64/build M=/home/vagrant/hello modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-229.14.1.el7.x86_64'
CC [M]  /home/vagrant/hello/hello.o
Building modules?? stage 2.
MODPOST 1 modules
CC      /home/vagrant/hello/hello.mod.o
LD [M]  /home/vagrant/hello/hello.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-229.14.1.el7.x86_64’
??????????????????????????????????:
[root@centos1 hello]# insmod hello.ko
[root@centos1 hello]# dmesg | tail -1
[62576.091311] Hello world!
[root@centos1 hello]# rmmod hello.ko
[root@centos1 hello]# dmesg | tail -1
[62611.981529] Cleaning up module.
?????????鱻??????? module_init ??????á???????????У???????? hello_init ?????? /var/log/message ?????????????????????????鱻?????? module_exit ??????????????? hello_cleanup ????????????????????????
?????????????????????????????????д???????????????С??????С???????????????????鱻?????????б???????????????????????