Please use a variable to keep the kernel version we want to compile. In the above example, it is $(KVERSION). We usually run dkms build under the various kernel version, and the DKMS will pass the target kernel version to Makefile.

Please put your Makefile and source code at /usr/src/<module>-<module-version>. For example, I put above two files at /usr/src/hello-0.1

PACKAGE_NAME, PACKAGE_VERSION is necessary information, it shall be the same with the information or your folder name.

CLEAN is the command to clean up the folder. Every time before build, it will be executed. If unset, it is assumed to be "make clean"

MAKE[0] is the first command to build the kernel object. Since this is an example, this is the only command we have. This field tells DKMS how to build the kernel object, and we can pass the target kernel version by using $kernelver that DKMS provides us.

DEST_MODULE_LOCATION[0] tells DKMS where to put the kernel object when installing. In this example, the kernel object will be "/lib/modules/$kernelver/updates". In Ubuntu, this information will be overrade to "/updates/dkms". It means our kernel objects will be put at /lib/modules/$kernelver/updates/dkms". Although this information will be overrade, it is still a necessary field.

AUTOINSTALL set to yes means DKMS will try to build and install the kernel object when booting and it won't be re-built nor re-installed if the kernel object has been installed.

DKMS commands

Now, use "dkms add" to tell DKMS we have a module is ready. DKMS will create symlink from /var/lib/dkms to /usr/src.

The pre-built modules will be included in the DKMS deb made later. When the versions of the pre-built modules and the kernel on the target computer match, the pre-built modules will just be used rather than compiling on installation.

Debug when DKMS build failed

If DKMS build kernel failed, it will leave the make.log for you to tell what happened.The log will be at /var/lib/dkms/<your_module_name>/<your_module_version>/build/make.logIn this example, the log will be /var/lib/dkms/hello/0.1/build/make.log

Generate DKMS deb

Using "dkms mkdeb" to build deb package. You shall run "dkms mkdeb" after "dkms add" and "dkms build"

with AUTOINSTALL=yes in dkms.conf, you can boot with various kernel and dkms will build the kernel module automatically for you.

Limitation of DKMS

1. DKMS may not work well after a large change of kernel ABI - Using DKMS on a stable release kernel will be a better idea.2. Make sure the we have toolchains and kernel header installed.3. Using DKMS on a netbook is not a smart idea - It takes too much time to build the kernel module after kernel upgrading.4. DKMS modules will not be loaded if there are another ones in initrd.