Loading and Unloading Modules

本文介绍了Linux内核模块的加载过程,包括insmod工具如何将模块加载到内核中,以及内核如何通过sys_init_module系统调用处理模块。同时提到了modprobe工具,它能自动解决模块间的依赖关系。此外,还讨论了rmmod用于移除模块的情况,以及当模块被占用或不允许卸载时可能出现的问题。
摘要由CSDN通过智能技术生成

Loading and Unloading Modules

After the module is built, the next step is loading it into the kernel. As we've already pointed out, insmod does the job for you. The program loads the module code and data into the kernel, which, in turn, performs a function similar to that of ld, in that it links any unresolved symbol in the module to the symbol table of the kernel. Unlike the linker, however, the kernel doesn't modify the module's disk file, but rather an in-memory copy. insmod accepts a number of command-line options (for details, see the manpage), and it can assign values to parameters in your module before linking it to the current kernel. Thus, if a module is correctly designed, it can be configured at load time; load-time configuration gives the user more flexibility than compile-time configuration, which is still used sometimes. Load-time configuration is explained in Section 2.8 later in this chapter. 构建模块后,下一步是将其加载到内核中。 正如我们已经指出的,insmod 为您完成了这项工作。 该程序将模块代码和数据加载到内核中,然后内核执行类似于 ld 的功能,因为它将模块中任何未解析的符号链接到内核的符号表。 然而,与链接器不同的是,内核不会修改模块的磁盘文件,而是修改内存中的副本。 insmod 接受许多命令行选项(有关详细信息,请参阅手册页),它可以在将模块链接到当前内核之前为模块中的参数分配值。 因此,如果一个模块设计正确,它可以在加载时进行配置; 加载时配置为用户提供了比编译时配置更大的灵活性,编译时配置有时仍在使用。 加载时配置将在本章后面的 2.8 节中解释。

Interested readers may want to look at how the kernel supports insmod: it relies on a system call defined in kernel/module.c. The function sys_init_module allocates kernel memory to hold a module (this memory is allocated with vmalloc ; see the Section 8.4 in Chapter 2); it then copies the module text into that memory region, resolves kernel references in the module via the kernel symbol table, and calls the module's initialization function to get everything going. 有兴趣的读者可能想看看内核如何支持 insmod:它依赖于 kernel/module.c 中定义的系统调用。 函数 sys_init_module 分配内核内存来保存一个模块(这个内存是用 vmalloc 分配的;参见第 2 章的 8.4 节); 然后它将模块文本复制到该内存区域,通过内核符号表解析模块中的内核引用,并调用模块的初始化函数以使一切正常进行。

If you actually look in the kernel source, you'll find that the names of the system calls are prefixed with sys_. This is true for all system calls and no other functions; it's useful to keep this in mind when grepping for the system calls in the sources. 如果您实际查看内核源代码,您会发现系统调用的名称以 sys_ 为前缀。 对于所有系统调用都是如此,而没有其他函数; 在获取源中的系统调用时,记住这一点很有用。

The modprobe utility is worth a quick mention. modprobe, like insmod, loads a module into the kernel. It differs in that it will look at the module to be loaded to see whether it references any symbols that are not currently defined in the kernel. If any such references are found, modprobe looks for other modules in the current module search path that define the relevant symbols. When modprobe finds those modules (which are needed by the module being loaded), it loads them into the kernel as well. If you use insmod in this situation instead, the command fails with an "unresolved symbols" message left in the system logfile. modprobe 实用程序值得一提。 modprobe 和 insmod 一样,将模块加载到内核中。 不同之处在于它将查看要加载的模块,以查看它是否引用了当前未在内核中定义的任何符号。 如果找到任何此类引用,modprobe 会在当前模块搜索路径中查找定义相关符号的其他模块。 当 modprobe 找到这些模块(正在加载的模块需要这些模块)时,它也会将它们加载到内核中。 如果您在这种情况下使用 insmod,则该命令将失败,并在系统日志文件中留下“未解析的符号”消息。

As mentioned before, modules may be removed from the kernel with the rmmod utility. Note that module removal fails if the kernel believes that the module is still in use (e.g., a program still has an open file for a device exported by the modules), or if the kernel has been configured to disallow module removal. It is possible to configure the kernel to allow "forced" removal of modules, even when they appear to be busy. If you reach a point where you are considering using this option, however, things are likely to have gone wrong badly enough that a reboot may well be the better course of action. 如前所述,可以使用 rmmod 实用程序从内核中删除模块。 请注意,如果内核认为模块仍在使用中(例如,程序仍有模块导出的设备的打开文件),或者内核已配置为不允许模块删除,则模块删除失败。 可以将内核配置为允许“强制”删除模块,即使它们看起来很忙。 但是,如果您到了考虑使用此选项的地步,那么事情可能已经严重出错,重新启动可能是更好的做法。

The lsmod program produces a list of the modules currently loaded in the kernel. Some other information, such as any other modules making use of a specific module, is also provided. lsmod works by reading the /proc/modules virtual file. Information on currently loaded modules can also be found in the sysfs virtual filesystem under /sys/module. lsmod 程序生成当前加载到内核中的模块列表。 还提供了一些其他信息,例如使用特定模块的任何其他模块。 lsmod 通过读取 /proc/modules 虚拟文件来工作。 当前加载的模块的信息也可以在 /sys/module 下的 sysfs 虚拟文件系统中找到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mounter625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值