Linux device drivers to load and unload module

Front description

Examples described herein relate to two modules and hello.ko world.ko, wherein hello world export symbols for use;

insmod

The command code and data modules into the kernel, then the kernel symbol table module to continue any unresolved symbols. insmod module does not modify the disk file, but only modify the copy in memory. insmod can accept some command line options, and can re-link module to module integer and string variables prior to assignment to the kernel.

The order of loading, by;

1 [root@localhost export]# insmod hello.ko
2 [root@localhost export]# insmod world.ko

First load world.ko, not through, because the world can not find a symbol from the reference;

1 [root@localhost export]# insmod world.ko 
2 insmod: ERROR: could not insert module world.ko: Unknown symbol in module
modprobe

And insmod type, also be used to modprobe module into the kernel, except that, modprobe will consider whether to load the module symbolic references to some of the kernel does not exist currently, if there is such a reference, modprobe will search path in the current module find other modules defined these symbols, if these dependencies module is found, it will at the same time these modules loaded into the kernel. Use insmod fails in this case, and record "unresolved symbols" messages in the system log file;

In use modprobe, clean lines and rows modules_install added in the configuration file;

 1 ifneq ($(KERNELRELEASE),)
 2         obj-m :=hello.o world.o
 3 #       module-objs := file1.o file2.o
 4 else
 5         KERNELDIR ?=/lib/modules/$(shell uname -r)/build
 6         PWD :=$(shell pwd)
 7 default:
 8         $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
 9         $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
10 clean:
11         rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers
12         $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
13 endif

Load Module world, success;

1 [root@localhost export]# modprobe world

lsmod See, also visible hello dependent module is loaded into the kernel;

1 [root@localhost export]# lsmod
2 Module                  Size  Used by
3 world                  16384  0 
4 hello                  16384  1 world
rmmod

rmmod to remove the module from the kernel; if the kernel module is still in use, or kernel module is configured to prohibit the removal, you can not remove the module; and support in the kernel configuration module still busy time module is removed possible; however, reboot the system is a more appropriate approach;

First remove world.ko, then remove hello.ko, success;

[root@localhost export]# rmmod world.ko 
[root@localhost export]# rmmod hello.ko

Hello.ko first removable module, generate an error because hello world being used;

1 [root@localhost export]# rmmod hello.ko
2 rmmod: ERROR: Module hello is in use by: world
lsmod

lsmod to list all the currently loaded kernel modules, including some additional information, which acquires information by reading / proc / modules;

1 [root@localhost export]# lsmod
2 Module                  Size  Used by
3 world                  16384  0 
4 hello                  16384  1 world

 

Guess you like

Origin www.cnblogs.com/wanpengcoder/p/11755850.html