Linux
Kernel module (asas)
Posted on .Nota ringkas untuk mengompil kernel module pada sistem x86. Anda memerlukan kod sumber kernel:
fairuz@themachine:~/work/x86$ apt-get source linux-image-$(uname -r)
fairuz@themachine:~/work/x86$ ll
total 102940
drwxrwxr-x 4 fairuz fairuz 4096 Oct 21 23:44 ./
drwxrwxr-x 4 fairuz fairuz 4096 Oct 21 22:37 ../
drwxrwxr-x 29 fairuz fairuz 4096 Oct 22 00:11 linux-3.5.0/
-rw-rw-r-- 1 fairuz fairuz 3098275 Oct 9 21:33 linux_3.5.0-17.28.diff.gz
-rw-rw-r-- 1 fairuz fairuz 4212 Oct 9 21:33 linux_3.5.0-17.28.dsc
-rw-rw-r-- 1 fairuz fairuz 102283735 Oct 9 21:33 linux_3.5.0.orig.tar.gz
Anda perlu menyediakan kod sumber kernel yang dimuat turun tadi untuk proses kompilasi modul kernel.
fairuz@themachine:~/work/x86/linux-3.5.0$ cp /boot/config-3.5.0-17-generic .config
fairuz@themachine:/data/x86/linux-3.5.0$ make oldconfig
scripts/kconfig/conf --oldconfig Kconfig
#
# configuration written to .config
#
fairuz@themachine:/data/x86/linux-3.5.0$ make -j12 modules_prepare
...
...
Kernel module yang paling ringkas kita boleh bina adalah module yang memaparkan mesej apabila dimuatkan dan dikeluarkan. Untuk ini, kita memerlukan satu fail kod sumber dan satu fail Makefile.
fairuz@themachine:~/work/x86$ mkdir ldd
fairuz@themachine:~/work/x86$ cd ldd
fairuz@themachine:~/work/x86/ldd$ touch Makefile
fairuz@themachine:~/work/x86/ldd$ touch sample.c
Buka kedua fail ini menggunakan gedit.
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_INFO "Hello, world\n");
printk(KERN_INFO "The process is \"%s\" (pid %i)\n", current->comm, current->pid);
return 0;
}
static void hello_exit(void)
{
printk(KERN_INFO "Goodbye, cruel world\n");
printk(KERN_INFO "The process is \"%s\" (pid %i)\n", current->comm, current->pid);
}
module_init(hello_init);
module_exit(hello_exit);
obj-m := sample.o
all:
make -C /home/fairuz/work/x86/linux-3.5.0 M=/home/fairuz/work/x86/ldd modules
clean:
make -C /home/fairuz/work/x86/linux-3.5.0 M=PWD clean
Untuk kompilasi, anda hanya perlu jalankan make di terminal.
fairuz@themachine:~/work/x86/ldd$ make
make -C /home/fairuz/work/x86/linux-3.5.0 M=/home/fairuz/work/x86/ldd modules
make[1]: Entering directory `/home/fairuz/work/x86/linux-3.5.0'
CC [M] /home/fairuz/work/x86/ldd/sample.o
Building modules, stage 2.
MODPOST 1 modules
LD [M] /home/fairuz/work/x86/ldd/sample.ko
make[1]: Leaving directory `/home/fairuz/work/x86/linux-3.5.0'
fairuz@themachine:~/work/x86/ldd$
fairuz@themachine:~/work/x86/ldd$
fairuz@themachine:~/work/x86/ldd$ ls
Makefile modules.order Module.symvers sample.c sample.ko sample.mod.c sample.mod.o sample.o
module yang siap dikompil akan mempunyai lanjutan .ko. Sekarang kita boleh mencuba kernel module ini. Untuk memuatkan module, gunakan arahan insmod. rmmod pula digunakan untuk mengeluarkan module. Senarai module yang dimuatkan boleh dilihat dengan arahan lsmod.
fairuz@themachine:~/work/x86/ldd$ sudo insmod sample.ko
[sudo] password for fairuz:
fairuz@themachine:~/work/x86/ldd$ dmesg | tail -n 2
[ 993.295670] Hello, world
[ 993.295672] The process is "insmod" (pid 3177)
fairuz@themachine:~/work/x86/ldd$ lsmod | grep sample
sample 12496 0
fairuz@themachine:~/work/x86/ldd$ sudo rmmod sample
fairuz@themachine:~/work/x86/ldd$ dmesg | tail -n 2
[ 1039.055207] Goodbye, cruel world
[ 1039.055211] The process is "rmmod" (pid 3187)
Selamat mencuba!
Fairuz
http://www.tutorialmelayu.comSystem Engineer (Texas Instruments France), Masters in Electronics , Embedded System Engineering,