Loads and unloads out-of-tree Linux kernel modules via insmod/rmmod.
KmodLoader provides idempotent kernel module lifecycle management for embedded
Linux and Nerves systems that need to load .ko
files from non-standard paths. It wraps the low-level insmod and rmmod
commands with proper error handling, /proc/modules introspection, device node
polling, and pseudo-filesystem mounting.
modprobe relies on module dependency metadata in /lib/modules/<version>/,
which doesn't exist for out-of-tree modules copied from vendor partitions.
insmod loads a .ko directly by path, which is what we need.
Add kmod_loader to your list of dependencies in mix.exs:
def deps do
[
{:kmod_loader, "~> 0.1.0"}
]
end# Load a kernel module from a .ko file
:ok = KmodLoader.insmod("/lib/modules/my_driver.ko")
# Load idempotently (skip if already loaded)
:ok = KmodLoader.insmod("/lib/modules/my_driver.ko", skip_if_loaded: true):ok = KmodLoader.rmmod("my_driver")true = KmodLoader.loaded?("my_driver")
# Hyphens are normalized to underscores automatically
true = KmodLoader.loaded?("my-driver")After loading a driver, you may need to wait for its device node to appear:
:ok = KmodLoader.System.wait_for_device("/dev/wmtdetect", 10)Idempotently mount pseudo-filesystems like configfs or debugfs:
:ok = KmodLoader.System.mount_pseudo_fs("configfs", "/sys/kernel/config")
:ok = KmodLoader.System.mount_pseudo_fs("debugfs", "/sys/kernel/debug")| Function | Description |
|---|---|
KmodLoader.insmod/2 |
Load a .ko kernel module by path |
KmodLoader.rmmod/1 |
Unload a kernel module by name |
KmodLoader.loaded?/1 |
Check if a module is loaded via /proc/modules |
KmodLoader.System.safe_cmd/2 |
Execute a system command with error handling |
KmodLoader.System.wait_for_device/2 |
Poll for a device node to appear |
KmodLoader.System.mount_pseudo_fs/2 |
Idempotently mount a pseudo-filesystem |
KmodLoader.System.mounted?/1 |
Check if a path is a mount point |
MIT