{"id":51,"date":"2024-03-03T10:51:37","date_gmt":"2024-03-03T10:51:37","guid":{"rendered":"https:\/\/rethinkit.blog\/?p=51"},"modified":"2024-05-17T08:32:13","modified_gmt":"2024-05-17T08:32:13","slug":"managing-linux-kernel-modules","status":"publish","type":"post","link":"https:\/\/rethinkit.blog\/?p=51","title":{"rendered":"Managing Linux Kernel Modules"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\"\/>\n\n\n<div class=\"wp-block-post-excerpt\"><p class=\"wp-block-post-excerpt__excerpt\">Learn how to manage kernel modules in your system. Dynamically loadable modules make things flexible within your environment. <\/p><\/div>\n\n\n<h4 class=\"wp-block-heading has-urbanist-font-family\" id=\"zfp9r178735\"><strong>Table of Contents<\/strong><\/h4>\n\n\n\n<ol style=\"padding-right:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--30)\" id=\"b6jjhb33wke3859532\" class=\"is-style-default wp-block-list\">\n<li><a href=\"#riuz3182579\"><u>Overview<\/u><\/a><\/li>\n\n\n\n<li><a href=\"#5bhg9184888\"><u>Modules-load.d<\/u><\/a><\/li>\n\n\n\n<li><a href=\"#7hnn7188550\"><u>Inserting Modules<\/u><\/a><\/li>\n\n\n\n<li><a href=\"#m2pqc21194\"><u>Removing Modules<\/u><\/a><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading has-urbanist-font-family\" id=\"di59q\"><strong>Overview<\/strong><\/h4>\n\n\n\n<p id=\"4rlrp13484\">In most Linux userspace distributions, modprobe is an available application that aides in loading dynamic kernel modules (KOs). Systemd can load these modules for a system based off a configuration file defining a list of available modules. The service utilizes modprobe for this.<\/p>\n\n\n\n<p id=\"1mf2u17079\">This post teaches users:<\/p>\n\n\n\n<ul style=\"margin-right:0;margin-left:0;padding-top:0;padding-right:var(--wp--preset--spacing--30);padding-bottom:0;padding-left:var(--wp--preset--spacing--30)\" id=\"dnxcib33wke3859544\" class=\"is-style-default has-urbanist-font-family wp-block-list\">\n<li>The configuration file defining available modules<\/li>\n\n\n\n<li>Manually inserting modules using modprobe\/insmod<\/li>\n\n\n\n<li>Manually removing modules<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading has-urbanist-font-family\" id=\"yetu221035\"><strong>Modules-load.d<\/strong><\/h4>\n\n\n\n<p id=\"rngfe23126\">Under the &#8220;<strong>\/etc<\/strong>&#8221; directory there should be another directory named &#8220;<strong>modules-load.d<\/strong>&#8220;. This is where configuration files are kept, which defines the list of modules that will be loaded.<\/p>\n\n\n\n<pre class=\"wp-block-code has-urbanist-font-family\"><code><strong>\/etc\/modules-load.d\/<\/strong> \n\u251c\u2500\u2500 10-abc.conf \n\u251c\u2500\u2500 20-def.conf \n\u2514\u2500\u2500 30-xyz.conf<\/code><\/pre>\n\n\n\n<p id=\"rngfe23126\">Within each .conf file is a newline separated list of module names that users are interested in loading when systemd starts.<\/p>\n\n\n\n<pre class=\"wp-block-code has-urbanist-font-family\"><code><strong>10-abc.conf:<\/strong> \n\nhci_uart \nraspberrypi_hwmon \n...<\/code><\/pre>\n\n\n\n<p id=\"rngfe23126\">The names of kernel modules can be found by looking at the &#8220;Makefile&#8221; within the driver&#8217;s directory. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-urbanist-font-family\"><code><strong># Module \n<\/strong>rndis_host.ko \n\n<strong># drivers\/net\/usb\/Makefile \n<\/strong>obj-$(CONFIG_USB_NET_RNDIS_HOST) += rndis_host.o<\/code><\/pre>\n\n\n\n<p id=\"0atqj15723\">By creating a .conf file, modules contained within the file will be loaded by default on device boot up by systemd. This avoids users from having to manually loading modules, or writing a separate startup script.<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-urbanist-font-family\" id=\"5ema49669\"><strong>Inserting Modules<\/strong><\/h4>\n\n\n\n<p id=\"exp9w1398\">In case users want to manually load a kernel module, that can be done using either modprobe or insmod. Some differences between the two include:<\/p>\n\n\n\n<ul style=\"margin-right:var(--wp--preset--spacing--30);margin-left:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--30)\" id=\"yb68tb33wke3859570\" class=\"has-urbanist-font-family wp-block-list\">\n<li>modprobe\n<ul class=\"wp-block-list\">\n<li>Looks for modules under a predefined directory, i.e.<strong> \/lib\/modules\/$(uname -r)<\/strong><\/li>\n\n\n\n<li>No need to specify the .ko extension, just the module name is sufficient.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>insmod\n<ul class=\"wp-block-list\">\n<li>Module can be located anywhere in the filesystem.<\/li>\n\n\n\n<li>Fille name and extension must be passed in.<\/li>\n\n\n\n<li>Good for modules that aren&#8217;t built in, i.e. test modules.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code has-urbanist-font-family\"><code><strong># modprobe on a raspberry pi zero w<\/strong> \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# modprobe hci_uart\n\nroot@raspberrypi0-wifi:\/etc\/modprobe.d# \n&#91;1730395.760177] Bluetooth: hci0: BCM: firmware Patch file not found, tried: ... \n\n<strong># insmod of hci_uart<\/strong> \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# insmod \/lib\/modules\/6.1.31+\/kernel\/drivers\/bluetooth\/hci_uart.ko \n\nroot@raspberrypi0-wifi:\/etc\/modprobe.d# \n&#91;1732213.870181] Bluetooth: hci0: BCM: firmware Patch file not found, tried: ...<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading has-urbanist-font-family\"><strong>Removing Modules<\/strong><\/h4>\n\n\n\n<p id=\"gjuxi114879\">Part of removing modules will require users to first identify which modules are currently loaded. &#8220;<strong>lsmod<\/strong>&#8221; can be utilized to list all currently loaded modules in the system. The output module names are the strings that will be passed to the application which removes kernel modules.<\/p>\n\n\n\n<pre class=\"wp-block-code has-urbanist-font-family\"><code>root@raspberrypi0-wifi:\/etc\/modprobe.d# lsmod \nModule Size Used by \nhci_uart 35599 0 \nalgif_hash 5846 1 \naes_arm 4436 2 \naes_generic 28270 1 aes_arm \necb 2011 1 \ncmac 3264 2 \nalgif_skcipher 4368 1 \naf_alg 16671 6 algif_hash,algif_skcipher \n...<\/code><\/pre>\n\n\n\n<p id=\"gjuxi114879\">Now that users can find what modules can be unloaded, the &#8220;<strong>rmmod<\/strong>&#8221; command can be used to unload the specific module.<\/p>\n\n\n\n<pre class=\"wp-block-code has-urbanist-font-family\"><code><strong># check for loaded modules<\/strong> \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# lsmod \nModule Size Used by \nhci_uart 35599 0 \nalgif_hash 5846 1 \n... \n\n<strong># remove hci_uart module<\/strong> \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# rmmod hci_uart \n\n<strong># hci_uart module unloaded<\/strong> \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# lsmod \nModule Size Used by \nalgif_hash 5846 0 \naes_arm 4436 0 \n...<\/code><\/pre>\n\n\n\n<p id=\"3q0u433249\">Sometimes there are module dependencies, which would kick in if users attempted to remove a module that was referenced\/used by another module. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-urbanist-font-family\"><code><strong># attempt to remove aes_generic <\/strong>\nroot@raspberrypi0-wifi:\/etc\/modprobe.d# lsmod \nModule Size Used by \nalgif_hash 5846 0 \n<mark style=\"background-color:#f8d984\" class=\"has-inline-color\">aes_arm <\/mark>4436 0 \n<strong>aes_generic 28270 1 <mark style=\"background-color:#f8d984\" class=\"has-inline-color\">aes_arm<\/mark><\/strong>\n\n<strong># error when removing due to dependent module<\/strong> \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# rmmod aes_generic \nrmmod: ERROR: Module aes_generic is in use by: aes_arm \n\n<strong># remove dependent modules first, and then desired module<\/strong> \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# rmmod aes_arm \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# rmmod aes_generic \n\n<strong># both aes_arm and aes_generic removed<\/strong> \nroot@raspberrypi0-wifi:\/etc\/modprobe.d# lsmod \nModule Size Used by \nalgif_hash 5846 0 \necb 2011 0 \ncmac 3264 0<\/code><\/pre>\n\n\n\n<p id=\"3q0u433249\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to manage kernel modules in your system.  Dynamically loadable modules make things flexible within your environment.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-technical"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/rethinkit.blog\/index.php?rest_route=\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rethinkit.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rethinkit.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rethinkit.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rethinkit.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":11,"href":"https:\/\/rethinkit.blog\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/rethinkit.blog\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions\/324"}],"wp:attachment":[{"href":"https:\/\/rethinkit.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rethinkit.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rethinkit.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}