0

I'm using a centos 8. I made a module where every time the module goes up it prints "Hello World" and every time the module goes down it prints "Goodbye". How do I make the module load automatically during boot?

#include <linux/module.h> 
#include <linux/kernel.h> 
int init_module(void)
{
  printk("Hello world\n");
  return 0;
}
void cleanup_module(void)
{
  printk(KERN_INFO "Goodbye\n");   
}
module_init(init_module);
module_exit(cleanup_module);

The name of this file is mymodule.c .

(I have seen other posts here about this but they do not explain where I place my module, and what I record in a configuration file). So I would love an explanation, thank you very much

pmdj
  • 22,018
  • 3
  • 52
  • 103
  • Depends upon what you mean by "at boot". To truly do this during early boot stages, the module couldn't be a loadable module. (1) It would need to be bound into the kernel `vmlinux` image. Otherwise, look at `systemd`. (2) You could add your module to the ramdisk initrd/initramfs image. (3) You can add your module to `/lib/modules` (4) Or, You can create a one-shot systemd service to do the load by adding your service to `/etc/systemd`. There are standard ways/tools to do these things. In the kernel tree, look at `Documentation/kbuild` for starters – Craig Estey Feb 13 '22 at 16:57
  • "I have seen other posts here about this but they do not explain where I place my module, and what I record in a configuration file" - Have you checked [that question](https://askubuntu.com/questions/299676/how-to-install-3rd-party-module-so-that-it-is-loaded-on-boot) on AskUbuntu? Its answer seems to explain both these aspects. – Tsyvarev Feb 13 '22 at 19:16
  • You can list the module name in a file matching `/etc/modules-load.d/*.conf` to let the **systemd-modules-load.service** load it during system start-up. The module itself needs to be installed somewhere where `depmod` and `modprobe` will find it, and is typically installed by `make M="$(pwd)" modules_install` or something similar. – Ian Abbott Feb 14 '22 at 11:16
  • How to install my module that the depmod and modprobe will find it? – Jonatan Haish Feb 15 '22 at 09:39

1 Answers1

0

The kernel's modules_install Makefile target may be used to install the built module somewhere appropriate for the kernel so that it can be loaded by modprobe.

This can be invoked from a rule in the external module's Makefile. See the install: target in the Makefile below for an example:

# Note: this `ifneq` and its matching `else` and `endif` can be omitted
# for recent kernels.
ifneq ($(KERNELRELEASE),)
# KBuild part of Makefile

obj-m += mymodule.o

else
# Normal part of Makefile
#

# Kernel build directory specified by KDIR variable
# Default to running kernel's build directory if KDIR not set externally
KDIR ?= "/lib/modules/$(shell uname -r)/build"

all:
    $(MAKE) -C "$(KDIR)" M="$(CURDIR)" modules

install: all
    $(MAKE) -C "$(KDIR)" M="$(CURDIR)" modules_install

clean:
    $(MAKE) -C "$(KDIR)" M="$(CURDIR)" clean

.PHONY: all install clean

endif

Install the module using this Makefile with the command: sudo make install. By default, it will be installed in the /lib/modules/$(KERNELRELEASE)/extra directory, but the subdirectory name extra can be changed by setting the INSTALL_MOD_DIR variable. (Also, a prefix can be prepended before the /lib/modules part by setting the INSTALL_MOD_PATH variable, which is useful for installing into a staging directory.)


To load the module automatically on system start-up, the systemd-modules-load.service service can be used. This requires the module name to be listed in a file matching one of the following patterns:

  • /etc/modules-load.d/*.conf
  • /run/modules-load.d/*.conf
  • /usr/lib/modules-load.d/*.conf

The /etc/modules-load.d/*.conf pattern is most appropriate for local system admininistration. For example:

/etc/modules-load.d/local.conf:

mymodule

(Older systems without systemd may have used a different method to specify the modules to be loaded on system start-up, such as listing them in a file called /etc/modules. That is still supported on Debian and Ubuntu systems.)

Modules for device drivers with device probe functions can use the MODULE_DEVICE_TABLE(type, name) macro to register a table of matching device IDs. Then udev will load the module automatically when a matching device is detected and so do not need to be listed in a modules-load.d configuration file.

Ian Abbott
  • 15,083
  • 19
  • 33
  • Thank you . But In the "Makefile" file after the "else", I did not understand what you meant when you wrote "Normal part of Makefile" – Jonatan Haish Feb 15 '22 at 08:47
  • By "Normal part of Makefile" I meant the part that is to be ignored by the kernel's "kbuild" build system. When you run "make" from the command line `KERNELRELEASE` should not be set so that "make" processes the `else` ("normal") part of the Makefile. See the [Shared Makefile](https://www.kernel.org/doc/html/latest/kbuild/modules.html#shared-makefile) section of [Building External Modules](https://www.kernel.org/doc/html/latest/kbuild/modules.html). – Ian Abbott Feb 15 '22 at 13:41