udev is loading the necessary driver modules when plugging in a hotplug-capable device. This is done by the following udev-rule
DRIVER!="?*", ENV{MODALIAS}=="?*", RUN{builtin}="kmod load $env{MODALIAS}"
Therefore kmod gets invoked by e.g. kmod load hid:b0005g0001v0000045Ep000002E0
The source of the builtin kmod is documented here
hid:b0005g0001v0000045Ep000002E0
is just an alias for a module like hid_xpadneo
.
I know that there is generated a file called modules.alias in /usr/lib/modules/$(uname -r)/modules.alias
where those aliases are listed like this:
alias hid:b0005g*v0000045Ep000002E0 hid_xpadneo
alias hid:b0005g*v0000045Ep000002FD hid_xpadneo
This file is generated out of all drivers and their MODULE_DEVICE_TABLEs while compile-time.
Unfortunately I cannot find where exactly kmod does read in that file or where it gets the alias information from.
Therefore: How does kmod know which alias stands for which module?
From systemd/udev-builtin-kmod.c:
static int builtin_kmod_init(void) {
...
kmod_load_resources(ctx);
...
}
From kmod/libkmod.c:
static struct _index_files {
const char *fn;
const char *prefix;
} index_files[] = {
[KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" },
[KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " },
[KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "},
[KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = ""},
};
...
KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
{
for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
...
snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
index_files[i].fn);
ctx->indexes[i] = index_mm_open(ctx, path,
&ctx->indexes_stamp[i]);
...
}
}
From kmon/libkmod-index.c:
struct index_mm *index_mm_open(struct kmod_ctx *ctx, const char *filename,
unsigned long long *stamp)
{
...
if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) < 0) {
...
}
kmod
reads and indexes all the possible files modules.dep
modules.alias
modules.symbols
and modules.builtin
and it get's it's alias information from that files.
Then later in udev-buildin-kmod.c:
static int builtin_kmod(sd_device *dev, int argc, char *argv[], bool test) {
...
for (i = 2; argv[i]; i++)
(void) module_load_and_warn(ctx, argv[i], false);
...
}
And from systemd/module-util.c:
int module_load_and_warn(struct kmod_ctx *ctx, const char *module, bool verbose) {
...
r = kmod_module_new_from_lookup(ctx, module, &modlist);
if (r < 0)
return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
"Failed to lookup module alias '%s': %m", module);
...
}
And inside kmod_module_new_from_lookup
the lookup for module alias is performed, ie. the data read into kmod object is searched through.