# 2024q1 Homework6 (integration) contributed by < `kevinzxc1217` > ## 閱讀〈[Linux 核心模組運作原理](https://hackmd.io/@sysprog/linux-kernel-module)〉 ### 前期準備 練習Linux 核心模組的撰寫、掛載及卸載。 #### 撰寫核心模組 **hello.c** ```c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_INFO "Hello, world\n"); return 0; } static void hello_exit(void) { printk(KERN_INFO "Goodbye, cruel world\n"); } module_init(hello_init); module_exit(hello_exit); ``` **Makefile** ```c obj-m := hello.o clean: rm -rf *.o *.ko *.mod.* *.symvers *.order *.mod.cmd *.mod ``` #### 編譯核心模組 ```c $ make -C /lib/modules/`uname -r`/build M=`pwd` modules warning: the compiler differs from the one used to build the kernel The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0 You are using: gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0 CC [M] /linux_module/hello/hello.o MODPOST /linux_module/hello/Module.symvers CC [M] /linux_module/hello/hello.mod.o LD [M] /linux_module/hello/hello.ko BTF [M] /linux_module/hello/hello.ko Skipping BTF generation for /linux_module/hello/hello.ko due to unavailability of vmlinux make: Leaving directory '/usr/src/linux-headers-6.5.0-27-generic' ``` #### 掛載核心模組 ```c $ sudo insmod hello.ko $ sudo dmesg [254738.085528] Hello, world ``` #### 卸載核心模組 ```c $ sudo rmmod hello $ sudo dmesg [254849.049232] Goodbye, cruel world ``` ### 解釋 insmod 後,Linux 核心模組的 symbol 如何被 Linux 核心找到
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up