Okay, let's break down how Raspbian handles kernel modules in .ko.xz format and how you can manage them with your custom-built kernel.
First, let's clarify how Raspbian uses compressed kernel modules and whether the kernel can load them directly. Then, we'll cover where to place your modules for a custom kernel.
Here are some search queries I'll use to get the most accurate information:
Okay, here's the breakdown of how Raspbian uses compressed kernel modules and how to work with them in your custom kernel:
1. Compressed Kernel Modules (.ko.xz):
- Why
.ko.xz? Raspbian (and many other distributions) use .ko.xz to compress kernel modules. This saves disk space, which is especially important on systems with limited storage like the Raspberry Pi. The compression is typically done using the XZ compression algorithm.
- Can the Kernel Load
.ko.xz Directly? Yes, the kernel can load .ko.xz modules directly, provided that the kernel is configured to support compressed modules and the necessary userspace tools (like kmod) are also configured correctly. This means:
- The kernel must be built with
CONFIG_MODULE_COMPRESS=y and CONFIG_MODULE_COMPRESS_XZ=y (or CONFIG_MODULE_COMPRESS_GZIP=y if using gzip compression).
- The
kmod package (which includes tools like modprobe, insmod, rmmod, and depmod) must be compiled with XZ (or gzip) support. Specifically, depmod needs LZMA support to map and allow loading .xz compressed kernel modules.
- How it Works: When you try to load a
.ko.xz module, the kernel uses its built-in decompression capabilities to decompress the module into memory before loading it.
2. Where to Place Modules for a Custom-Built Kernel:
- Conventional Path: The standard location for kernel modules is
/lib/modules/<kernel_version>/. The <kernel_version> part is critical. It must exactly match the output of uname -r on your Raspberry Pi when running your custom kernel.
- Steps:
- Determine Kernel Version: After booting your custom kernel, run
uname -r. Let's say the output is 5.15.100-custom.
- Create Module Directory: Create a directory named
/lib/modules/5.15.100-custom on your Raspberry Pi's root filesystem (the second partition, usually). Make sure you have root privileges (use sudo).
- Copy Modules: Copy all your
.ko (or .ko.xz) files into this directory.
- Run
depmod: This is essential. Run sudo depmod -a 5.15.100-custom. This command analyzes the modules and creates the modules.dep file (and other dependency files) in the module directory. These files tell the kernel which modules depend on others, allowing modules to be loaded in the correct order. Without this step, modprobe will likely fail.
- Loading Modules: Now you can use
modprobe <module_name> to load your modules. Note that you should use the module name without the .ko or .ko.xz extension. modprobe will look in /lib/modules/<kernel_version>/ for the module and handle any dependencies. You can also use insmod /lib/modules/<kernel_version>/<module_name>.ko (or .ko.xz), but modprobe is generally preferred because it handles dependencies.
3. Important Considerations:
- Kernel Configuration: Ensure your kernel is built with module compression enabled (if you want to use
.ko.xz).
- Matching Kernel Version: The kernel version reported by
uname -r must match the directory name under /lib/modules. Mismatches are a very common cause of module loading problems.
depmod is Crucial: Don't skip the depmod step! It creates the module dependency files that modprobe uses.
- Module Dependencies: If your module depends on other modules, make sure those dependencies are also installed in the correct directory and that
depmod is run after all modules are in place.
- Secure Boot: If you have Secure Boot enabled, you may need to sign your kernel modules. This is a more advanced topic.
- Initramfs/initrd: If you need certain modules loaded very early in the boot process (e.g., for accessing the root filesystem), you may need to include them in your initramfs or initrd image. This is distribution-specific.
Example:
Let's say you have a module called my_module.ko that you built for your custom kernel 5.15.100-custom.
sudo mkdir -p /lib/modules/5.15.100-custom
sudo cp my_module.ko /lib/modules/5.15.100-custom/
sudo depmod -a 5.15.100-custom
sudo modprobe my_module
Troubleshooting:
- "Module not found": Double-check the kernel version, the module directory, and that you ran
depmod.
- "Invalid module format": Usually indicates a kernel version mismatch or that the module was built for a different architecture.
- "Unknown symbol in module": A dependency is missing, or the kernel you built the module against doesn't have the necessary symbols.
- Check
/var/log/syslog or use dmesg: These logs often contain helpful error messages when module loading fails.
By following these steps and paying close attention to kernel versions and dependencies, you should be able to get your custom-built kernel modules loaded and working correctly on your Raspberry Pi.