Linux 如何在 Ubuntu 启动时加载自定义模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4356224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:13:04  来源:igfitidea点击:

How to load a custom module at the boot time in Ubuntu?

linuxmodulelinux-kernelkernel

提问by Gonzalo

I created a custom and simple module named Hello.ko I install the module with the command "insmod hello.ko", I check it with "dmesg" and it's working, but when I restart the system, I have to load it manually. So, how can I do for loading my custom module automatically, and where do I have to put the hello.ko? After loading the module, I would like to show the message Hello Worlduntil I press the Enter Key. Can anybody help me?

我创建了一个名为 Hello.ko 的自定义和简单模块,我使用命令“ insmod hello.ko”安装该模块,我用“ dmesg”检查它并且它正在工作,但是当我重新启动系统时,我必须手动加载它。那么,如何自动加载我的自定义模块,以及我必须将 hello.ko 放在哪里?加载模块后,我想在按下 Enter 键之前显示消息Hello World。有谁能够帮助我?

回答by Swind

Add the module to the /etc/modules file.

将模块添加到 /etc/modules 文件中。

And then put the module in your /lib/modules/kernelname catalogue.

然后将模块放入您的 /lib/modules/kernelname 目录中。

回答by Bob Enohp

This technique didn't work on Ubuntu 13.10. So after a few trial and error tries I ended up with this:

此技术不适用于 Ubuntu 13.10。所以经过几次反复试验后,我最终得到了这个:

Copy my kernel module to the drivers directory.

将我的内核模块复制到驱动程序目录。

$ sudo cp mymodule.ko /lib/modules/$(uname -r)/kernel/drivers/

Add the simple name of my module to the file /etc/modules. You can edit the file or just append to it as shown here.

将我的模块的简单名称添加到文件 /etc/modules.conf 中。您可以编辑文件或只是附加到它,如下所示。

$ echo 'mymodule' | sudo tee -a /etc/modules

Update the list of module dependencies.

更新模块依赖项列表。

$ sudo depmod

Reboot the computer and voila, it worked.

重新启动计算机,瞧,它起作用了。