[Desire General] lnxtux:
Folks,

While it's easy to update a kernel using Koush's excellent AnyKernel script, we all know the /system partition on our devices is not normally writable. Thus, kernel updates within the same version going just fine, but changing the kernel version renders your modules useless. Of course, one can easily retrofit the kernel update package to add all of the modules and deploy them to /system/lib/modules. No magic there, the tricky part is to remove unneeded modules, but there is no harm if we leave some unused modules behind. So if you not playing with the modules often, this won't bring much to you.

However, things are getting a bit complicated when you need to experiment with the modules a little bit and would like to preserve your custom modules during reboots. I would like to suggest the kernel modules autoconfiguration script. The idea is simple:

Deploy the kernel modules somewhere on a writable FS, e.g. /data/local/kernel/VERSION/modules

Replace the /system/lib/modules directory with a symlink (destination does not matter, as long as it's on a writable FS, e.g. /data/local/modules)

Drop a script under /system/etc/init.d to analyze the kernel version and setup the /data/local/modules to point to the appropriate modules dir

Change the AnyKernel updater-script to deploy the modules under /data/local/kernel/VERSION/modules

This way we can distribute an update.zip with the kernel image and all required modules plus have an ability to change/update the modules at any time.

Of course, /init.rc should be able to run the stuff from /system/etc/init.d

updater-script changes

# do it at the end of the updater-script, replace @VERSION@ with the actual kernel version
ui_print("Installing kernel modules...");
# hack to create a dir
mount("MTD", "cache", "/data/system/kernel/@VERSION@/modules");
unmount("/data/system/kernel/@VERSION@/modules");
package_extract_dir("modules", "/data/system/kernel/@VERSION@/modules");
set_perm_recursive(0, 0, 0755, 0644, "/data/system/kernel/@VERSION@/modules");

/system/etc/init.d/01modules

#!/system/bin/sh

SYSKODIR=/system/lib/modules
KVER=$(uname -r)
KERNELDIR=/data/local/kernel/$KVER/modules

if [ ! -h "$SYSKODIR" ]; then
echo "KOCFG: System modules directory $SYSKODIR is not a symlink, skipping autoconfig"
elif [ ! -d "$KERNELDIR" ]; then
echo "KOCFG: Kernel $KVER: no modules directory $KERNELDIR, skipping autoconfig"
else
KODIR=$(busybox readlink "$SYSKODIR")
if [ -e "$KODIR" -a -h "$KODIR" ]; then
LINKTO=$(busybox readlink "$KODIR")
if [ "x$LINKTO" != "x$KERNELDIR" ]; then
busybox rm -f "$KODIR"
busybox ln -s -f "$KERNELDIR" "$KODIR"
echo "KOCFG: Kernel modules reconfigured: $KODIR -> $KERNELDIR"
fi
else
KOBASE=$(busybox dirname "$KODIR")
if [ ! -e "$KOBASE" ]; then
busybox mkdir -p -m 755 "$KOBASE"
fi
busybox ln -s -f "$KERNELDIR" "$KODIR"
echo "KOCFG: Kernel modules configured: $KODIR -> $KERNELDIR"
fi
fi

# EOF

A script to set up /system/lib/modules symlink yet to be developed, something similar to

# trick, how to find the boot kernel version when you have write access to /system
# and thus running the recovery kernel? read vermagic from .ko files?
busybox mkdir -p /data/local/kernel/$KVER/modules
mv /system/lib/modules /data/local/kernel/$KVER/modules
ln -s /system/lib/modules /data/local/modules