{"id":431,"date":"2025-12-16T11:09:55","date_gmt":"2025-12-16T03:09:55","guid":{"rendered":"https:\/\/www.garfield-blog.top\/?p=431"},"modified":"2025-12-16T11:10:12","modified_gmt":"2025-12-16T03:10:12","slug":"%e3%80%90linux%e3%80%91%e7%ae%a1%e7%90%86%e5%92%8c%e6%96%b0%e5%a2%9e%e5%86%85%e6%a0%b8%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/www.garfield-blog.top\/?p=431","title":{"rendered":"\u3010Linux\u3011\u7ba1\u7406\u548c\u65b0\u589e\u5185\u6838\u6a21\u5757"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u6982\u8ff0<\/h2>\n\n\n\n<p><code>linux<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u79f0\u4e3a<strong>\u6a21\u5757\uff08Module\uff09\u7684\u673a\u5236<\/strong>\uff0c\u6a21\u5757\u5177\u6709\u5982\u4e0b\u7684\u7279\u70b9<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6a21\u5757\u81ea\u8eab\u4e0d\u88ab\u7f16\u8bd1\u5230\u5185\u6838\u6620\u50cf\u4e2d\uff0c\u4ece\u800c\u4e0d\u5f71\u54cd\u5185\u6838\u6620\u50cf\u7684\u5927\u5c0f<\/li>\n\n\n\n<li>\u4e00\u65e6\u6a21\u5757\u88ab\u52a0\u8f7d\uff0c\u6a21\u5757\u548c\u5185\u6838\u4e2d\u7684\u5176\u4ed6\u90e8\u5206\u7684\u529f\u80fd\u5b8c\u5168\u4e00\u6837<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ba1\u7406\u65b9\u5f0f<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u663e\u793a\u5f53\u524d\u88c5\u5165\u7684\u5185\u6838\u6a21\u5757\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   lsmod<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u663e\u793a\u6a21\u5757\u4fe1\u606f\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   modinfo module_name<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u663e\u793a\u6240\u6709\u6a21\u5757\u7684\u914d\u7f6e\u4fe1\u606f\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   modprobe -c | less<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>\u663e\u793a\u67d0\u4e2a\u6a21\u5757\u7684\u914d\u7f6e\u4fe1\u606f\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   modprobe -c | grep module_name<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li>\u663e\u793a\u4e00\u4e2a\u88c5\u5165\u6a21\u5757\u4f7f\u7528\u7684\u9009\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sysfsutils<\/code>\u5305\u4e2d\u7684<code>systool(1)<\/code><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   systool -v -m module_name<\/code><\/pre>\n\n\n\n<ol start=\"6\" class=\"wp-block-list\">\n<li>\u663e\u793a\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   modprobe --show-depends module_name<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u521b\u5efa\u4e00\u4e2a\u81ea\u5df1\u7684\u5185\u6838\u6a21\u5757<\/h2>\n\n\n\n<p><code>linux<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u5916\u90e8\u6a21\u5757\u6784\u5efa\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u5916\u90e8\u6a21\u5757\u4e0e<code>linux<\/code>\u6e90\u7801\u72ec\u7acb\u5b58\u653e\u5728\u4e0d\u540c\u7684\u8def\u5f84\u4e0b<\/p>\n\n\n\n<p>\u9996\u5148\u9700\u8981\u4e0b\u8f7d\u5e76\u7f16\u8bd1\u4e00\u5957<code>linux<\/code>\u4ee3\u7801\uff0c\u63a5\u7740\u5c31\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u5185\u6838\u6a21\u5757\u3002\u8fd9\u4e2a\u72ec\u7acb\u6a21\u5757\u7684\u76ee\u5f55\u7ed3\u6784\u4e5f\u975e\u5e38\u7684\u7b80\u5355\uff0c\u91cd\u70b9\u5728\u4e8e<code>Makefile<\/code>\u7684\u7f16\u5199<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>demo\n   \u251c\u2500\u2500 Makefile\n   \u2514\u2500\u2500 demo.c<\/code><\/pre>\n\n\n\n<p><strong>demo.c\u5185\u5bb9<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;linux\/module.h&gt;    \/* Needed by all modules *\/\n#include &lt;linux\/kernel.h&gt;    \/* Needed for KERN_INFO *\/\n#include &lt;linux\/init.h&gt;        \/* Needed for the macros *\/\n\nstatic int __init demo_init(void)\n{\n   printk(KERN_INFO \"Hello, world 2\\n\");\n   return 0;\n}\n\nstatic void __exit demo_exit(void)\n{\n   printk(KERN_INFO \"Goodbye, world 2\\n\");\n}\n\nmodule_init(demo_init);\nmodule_exit(demo_exit);<\/code><\/pre>\n\n\n\n<p><strong>Makefile\u5185\u5bb9<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>obj-m += demo.o\nKERNELBUILD += \/home\/pi\/code\/linux-5.4#\u8fd9\u91cc\u7684\u8def\u5f84\u662f\u4e00\u5957\u5df2\u7ecf\u7f16\u8bd1\u5b8c\u6210\u7684linux\u6e90\u7801\n\nall:\n   make -C ${KERNELBUILD} M=${shell pwd} modules\n\nclean:\n   make -C ${KERNELBUILD} M=${shell pwd} clean<\/code><\/pre>\n\n\n\n<p><strong>\u7f16\u8bd1\u65b9\u5f0f<\/strong><\/p>\n\n\n\n<p>\u8fd9\u4e2a\u72ec\u7acb\u6a21\u5757\u7684\u7f16\u8bd1\u548c\u6e05\u7406\u65b9\u5f0f\u548c\u666e\u901a\u7684\u6a21\u5757\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u53ea\u9700\u8981\u5728\u81ea\u5df1\u7684\u6a21\u5757\u4ee3\u7801\u6839\u76ee\u5f55\u4e0b\u6267\u884c<code>make all<\/code>\u6216\u8005<code>make clean<\/code>\u5c31\u53ef\u4ee5\u5b9e\u73b0\u7f16\u8bd1\u6216\u6e05\u7406<\/p>\n\n\n\n<p><strong>\u7f16\u8bd1\u4ea7\u7269<\/strong><\/p>\n\n\n\n<p>\u5728\u6267\u884c<code>make all<\/code>\u547d\u4ee4\u4e4b\u540e\u53ef\u4ee5\u76ee\u5f55\u4e0b\u591a\u51fa\u4e86\u5f88\u591a\u7f16\u8bd1\u4ea7\u7269\uff0c\u5176\u4e2d\u5c31\u5305\u62ec\u5185\u6838<code>ko<\/code>\u6a21\u5757<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>demo\n   \u251c\u2500\u2500 Makefile\n   \u251c\u2500\u2500 Module.symvers\n   \u251c\u2500\u2500 demo.c\n   \u251c\u2500\u2500 demo.ko\n   \u251c\u2500\u2500 demo.mod\n   \u251c\u2500\u2500 demo.mod.c\n   \u251c\u2500\u2500 demo.mod.o\n   \u251c\u2500\u2500 demo.o\n   \u2514\u2500\u2500 modules.order<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u6587\u6863<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/wiki.archlinuxcn.org\/wiki\/\u5185\u6838\u6a21\u5757\">\u5185\u6838\u6a21\u5757 &#8211; Arch Linux \u4e2d\u6587\u7ef4\u57fa<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/tldp.org\/LDP\/lkmpg\/2.6\/html\/index.html\">Linux \u5185\u6838\u6a21\u5757\u7f16\u7a0b\u6307\u5357<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 linux\u63d0\u4f9b\u4e86\u4e00\u79cd\u79f0\u4e3a\u6a21\u5757\uff08Module\uff09\u7684\u673a\u5236\uff0c\u6a21\u5757\u5177\u6709\u5982\u4e0b\u7684\u7279\u70b9 \u7ba1\u7406\u65b9\u5f0f \u521b\u5efa\u4e00\u4e2a\u81ea\u5df1\u7684\u5185\u6838\u6a21\u5757 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-431","post","type-post","status-publish","format-standard","hentry","category-linux-knowledge"],"_links":{"self":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=431"}],"version-history":[{"count":1,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":432,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions\/432"}],"wp:attachment":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}