1.2. How Do Modules Get Into The Kernel?

本文深入探讨了Linux内核中模块的加载方式,包括通过lsmod查看已加载的模块、使用modprobe加载特定模块的过程,以及如何解析依赖关系。此外,还介绍了如何配置模块搜索路径和别名,并给出了/etc/modules.conf文件的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You can see what modules are already loaded into the kernel by running lsmod, which gets its information by reading the file /proc/modules.

How do these modules find their way into the kernel? When the kernel needs a feature that is not resident in the kernel, the kernel module daemon kmod[1] execs modprobe to load the module in. modprobe is passed a string in one of two forms:

  • A module name like softdog or ppp.

  • A more generic identifier like char-major-10-30.

If modprobe is handed a generic identifier, it first looks for that string in the file /etc/modules.conf. If it finds an alias line like:

   alias char-major-10-30 softdog

it knows that the generic identifier refers to the module softdog.o.

Next, modprobe looks through the file /lib/modules/version/modules.dep, to see if other modules must be loaded before the requested module may be loaded. This file is created by depmod -a and contains module dependencies. For example, msdos.o requires the fat.o module to be already loaded into the kernel. The requested module has a dependancy on another module if the other module defines symbols (variables or functions) that the requested module uses.

Lastly, modprobe uses insmod to first load any prerequisite modules into the kernel, and then the requested module. modprobe directs insmod to /lib/modules/version/[2], the standard directory for modules. insmod is intended to be fairly dumb about the location of modules, whereas modprobe is aware of the default location of modules. So for example, if you wanted to load the msdos module, you'd have to either run:

    insmod /lib/modules/2.5.1/kernel/fs/fat/fat.o
insmod /lib/modules/2.5.1/kernel/fs/msdos/msdos.o

or just run "modprobe -a msdos".

Linux distros provide modprobe, insmod and depmod as a package called modutils or mod-utils.

Before finishing this chapter, let's take a quick look at a piece of /etc/modules.conf:

    #This file is automatically generated by update-modules
path[misc]=/lib/modules/2.4.?/local
keep
path[net]=~p/mymodules
options mydriver irq=10
alias eth0 eepro

Lines beginning with a '#' are comments. Blank lines are ignored.

The path[misc] line tells modprobe to replace the search path for misc modules with the directory /lib/modules/2.4.?/local. As you can see, shell meta characters are honored.

The path[net] line tells modprobe to look for net modules in the directory ~p/mymodules, however, the "keep" directive preceding the path[net] directive tells modprobe to add this directory to the standard search path of net modules as opposed to replacing the standard search path, as we did for the misc modules.

The alias line says to load in eepro.o whenever kmod requests that the generic identifier `eth0' be loaded.

You won't see lines like "alias block-major-2 floppy" in /etc/modules.conf because modprobe already knows about the standard drivers which will be used on most systems.

Now you know how modules get into the kernel. There's a bit more to the story if you want to write your own modules which depend on other modules (we calling this `stacking modules'). But this will have to wait for a future chapter. We have a lot to cover before addressing this relatively high-level issue.

1.2.1. Before We Begin

Before we delve into code, there are a few issues we need to cover. Everyone's system is different and everyone has their own groove. Getting your first "hello world" program to compile and load correctly can sometimes be a trick. Rest assured, after you get over the initial hurdle of doing it for the first time, it will be smooth sailing thereafter.

1.2.1.1. Modversioning

A module compiled for one kernel won't load if you boot a different kernel unless you enable CONFIG_MODVERSIONS in the kernel. We won't go into module versioning until later in this guide. Until we cover modversions, the examples in the guide may not work if you're running a kernel with modversioning turned on. However, most stock Linux distro kernels come with it turned on. If you're having trouble loading the modules because of versioning errors, compile a kernel with modversioning turned off.

1.2.1.2. Using X

It is highly recommended that you type in, compile and load all the examples this guide discusses. It's also highly recommended you do this from a console. You should not be working on this stuff in X.

Modules can't print to the screen like printf() can, but they can log information and warnings, which ends up being printed on your screen, but only on a console. If you insmod a module from an xterm, the information and warnings will be logged, but only to your log files. You won't see it unless you look through your log files. To have immediate access to this information, do all your work from console.

1.2.1.3. Compiling Issues and Kernel Version

Very often, Linux distros will distribute kernel source that has been patched in various non-standard ways, which may cause trouble.

A more common problem is that some Linux distros distribute incomplete kernel headers. You'll need to compile your code using various header files from the Linux kernel. Murphy's Law states that the headers that are missing are exactly the ones that you'll need for your module work.

To avoid these two problems, I highly recommend that you download, compile and boot into a fresh, stock Linux kernel which can be downloaded from any of the Linux kernel mirror sites. See the Linux Kernel HOWTO for more details.

Ironically, this can also cause a problem. By default, gcc on your system may look for the kernel headers in their default location rather than where you installed the new copy of the kernel (usually in /usr/src/. This can be fixed by using gcc's -I switch.

Notes

[1]

In earlier versions of linux, this was known as kerneld.

[2]

If you are modifying the kernel, to avoid overwriting your existing modules you may want to use the EXTRAVERSION variable in the kernel Makefile to create a seperate directory.

 
How is emlog used? 1: Configure, compile, and install emlog If you want to compile emlog for use with the currently running kernel, simply run make Otherwise, you have to set either KVER (for linux kernel sources, located in /lib/modules/<KVER>/build) or KDIR (for any other path): make KDIR=/usr/src/linux Two files should be generated: the kernel module itself (emlog.ko), and the nbcat utility that will be described later. You can use them directly from the current directory or you can install them via make install 2: Load the emlog module into the kernel If you chose to use emlog directly from the current directly, insert the module into the kernel using the insmod command insmod emlog.ko Otherwise, modprobe should work: modprobe emlog To specify a different maximum buffer size limit: modprobe emlog emlog_max_size=2048 If successful, a message similar to emlog:emlog_init: version 0.70 running, major is 251, MINOR is 1, max size 1024 K. should show up in your kernel log (type dmesg to see it). You can also verify that the module has been inserted by typing lsmod or cat /proc/modules. 3: Create device files for emlog By default, a device file /dev/emlog is created (if you have devtmpfs mounted onto /dev and/or have udev running) with a minimal allocated buffer. It's ready to be written to/read from. If you need more devices/buffers, you should can use the mkemlog program to create device files that your processes can write to. Usage mkemlog <logdevname> [size_in_kilobytes] [mode] 3.1: Examples usage mkemlog Create a log file with a 8k buffer with file permissions 0660 mkemlog /tmp/testlog Create a log file with a 17k buffer with file permissions 0660 mkemlog /tmp/testlog 17 Create a log file with a 12k buffer with file permissions 0644 mkemlog /tmp/testlog_12k 12 0644 Create a log file with a 18k buffer with file permissions 0644, owned by a user with UID==1000. mkemlog /tmp/testlog_18k 18 0644 1000 The mkemlog requires the /dev/emlog file to be cemlog
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值