Hello World Module ================== .. tab-set:: .. tab-item:: Linux Hello World Module * In this program, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to write a sample kernel module ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to write Makefile to compile driver module ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to check if module is loaded successfully ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to check if module is unloaded successfully ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to check prints from kernel modules ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use below APIs ? * `MODULE_LICENSE `_ * `MODULE_AUTHOR `_ * `pr_info `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use below Commands ? * `lsmod `_ * `insmod `_ * `rmmod `_ * `modinfo `_ * `dmesg `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Complete program : hello.c ` * :ref:`Makefile ` * :ref:`Compile Program ` * :ref:`check if the .ko file is created ` * :ref:`modinfo : check the information about the module ` * :ref:`insmod : check if the module is loaded successfully ` * :ref:`dmesg : check what happened on module load ` * :ref:`lsmod : checking the module ` * :ref:`rmmod : check if the module is unloaded successfully ` * :ref:`check what happened on module unload ` * :ref:`Summary ` .. _p1_basicModule_0: .. tab-set:: .. tab-item:: hello.c * ``init_module`` function gets called on insertion of module using insmod command * ``cleanup_module`` function gets called on removal of module using rmmod command .. literalinclude:: p1_basicModule/hello.c :language: c :linenos: .. _p1_basicModule_1: .. tab-set:: .. tab-item:: Makefile * In this Makefile, * ``obj-m`` indicates the build system to create a separate loadable .ko module * ``make -C`` informs the make command to look for master make file in the given path * ``$(shell uname -r)`` replaces the output of ``uname -r`` command * ``$(PWD)`` replaces it with Present Working Directory .. literalinclude:: p1_basicModule/Makefile :language: c :linenos: .. _p1_basicModule_2: .. tab-set:: .. tab-item:: Compile Program .. code-block:: c :linenos: :emphasize-lines: 1, 5, 9 $ pwd basic_ldd/chapter1_basics/p1_basicModule $ ls hello.c Makefile README $ make all make -C /lib/modules/6.2.0-32-generic/build M=/home/linux/basic_ldd/chapter1_basics/p1_basicModule modules make[1]: Entering directory /usr/src/linux-headers-6.2.0-32-generic CC [M] basic_ldd/chapter1_basics/p1_basicModule/hello.o MODPOST basicl_ldd/chapter1_basics/p1_basicModule/Module.symvers CC [M] basicl_ldd/chapter1_basics/p1_basicModule/hello.mod.o LD [M] basicl_ldd/chapter1_basics/p1_basicModule/hello.ko BTF [M] basicl_ldd/chapter1_basics/p1_basicModule/hello.ko make[1]: Leaving directory /usr/src/linux-headers-6.2.0-32-generic .. _p1_basicModule_3: .. tab-set:: .. tab-item:: check if the .ko file is created .. code-block:: c :linenos: :emphasize-lines: 1 $ ls -l total 240 -rwxrwxrwx 1 linux linux 233 Sep 11 16:00 hello.c -rw-rw-r-- 1 linux linux 108392 Sep 12 12:37 hello.ko -rw-rw-r-- 1 linux linux 134 Sep 12 12:37 hello.mod -rw-rw-r-- 1 linux linux 891 Sep 12 12:37 hello.mod.c -rw-rw-r-- 1 linux linux 93536 Sep 12 12:37 hello.mod.o -rw-rw-r-- 1 linux linux 16232 Sep 12 12:37 hello.o -rwxrwxrwx 1 linux linux 154 Sep 11 16:00 Makefile -rw-rw-r-- 1 linux linux 134 Sep 12 12:37 modules.order -rw-rw-r-- 1 linux linux 0 Sep 12 12:37 Module.symvers -rwxrwxrwx 1 linux linux 1876 Sep 12 13:12 README .. _p1_basicModule_4: .. tab-set:: .. tab-item:: modinfo : check the information about the module .. code-block:: c :linenos: :emphasize-lines: 1 $ modinfo hello.ko filename: basic_ldd/chapter1_basics/p1_basicModule/hello.ko author: Linux_usr license: GPL srcversion: CC6A6FD6CE9DAF87AC2E72B depends: retpoline: Y name: hello vermagic: 6.2.0-32-generic SMP preempt mod_unload modversions .. _p1_basicModule_5: .. tab-set:: .. tab-item:: insmod : check if the module is loaded successfully .. code-block:: c :linenos: :emphasize-lines: 1 $ sudo insmod ./hello.ko .. _p1_basicModule_6: .. tab-set:: .. tab-item:: dmesg : check what happened on module load .. code-block:: c :linenos: :emphasize-lines: 1 $ dmesg [67117.450399] Hello, world .. _p1_basicModule_7: .. tab-set:: .. tab-item:: lsmod : checking the module .. code-block:: c :linenos: :emphasize-lines: 1 $ lsmod | grep hello hello 16384 0 .. _p1_basicModule_8: .. tab-set:: .. tab-item:: rmmod : check if the module is unloaded successfully .. code-block:: c :linenos: :emphasize-lines: 1 $ sudo rmmod hello .. _p1_basicModule_9: .. tab-set:: .. tab-item:: check what happened on module unload .. code-block:: c :linenos: :emphasize-lines: 1 $ dmesg [67136.900177] Good bye, world .. _p1_basicModule_10: .. tab-set:: .. tab-item:: Summary =============================== ================================================= API Learning =============================== ================================================= MODULE_LICENSE Tells the kernel what license our module is under MODULE_AUTHOR Declares the module's author pr_info Prints an info-level message =============================== ================================================= =============================== ============================================================== Command Learning =============================== ============================================================== insmod Command to insert a kernel module rmmod Command to remove a kernel module lsmod Command to display the status of modules in the Linux kernel modinfo Command to display the information about a Linux Kernel module dmesg Command to display kernel-related messages =============================== ============================================================== .. card:: See Also * Other topic * :doc:`p2_moduleWithParameter` * Next Chapter * :doc:`../chapter2_kthreads/chapter2_kthreads` * Other Chapters * :doc:`../chapter3_tasklets/chapter3_tasklets` * :doc:`../chapter4_workqueue/chapter4_workqueue` * :doc:`../chapter5_timer/chapter5_timer` * :doc:`../chapter8_linkedList/chapter8_linkedList` * :doc:`../chapter9_fileSystem/chapter9_fileSystem` * :doc:`../chapter10_netlink/chapter10_netlink`