Linux kernel source code tree build and load the hello module, linuxhello

Source: Internet
Author: User

Linux kernel source code tree build and load the hello module, linuxhello

Before loading the module, the book said that we should first establish the kernel source code tree. So, how should we establish the kernel source code tree?

First, you must first know the kernel version of your OS and use uname-r to obtain the kernel version.

In this directory ls, we can see that this directory already exists =! That is, the original system already has the kernel source code tree.

  

 

Now that the kernel source code tree already exists, we need to load the hello module into the kernel.

Below is the source code of hello

  

 1 #include <linux/init.h>  2 #include <linux/module.h>  3 MODULE_LICENSE("Dual BSD/GPL");  4 static int hello_init(void)  5 {  6     printk(KERN_ALERT "Hello, world\n");  7     return 0;  8 }  9 static void hello_exit(void) 10 { 11     printk(KERN_ALERT"Goodbye, cruel world\n"); 12 } 13 module_init(hello_init); 14 module_exit(hello_exit); 

 

Write Makefile for code

1 obj-m := hello.o 2 KERNELDIR := /lib/modules/3.16.0-30/build 3 PWD := $(shell pwd) 4 modules: 5     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 6 modules_install: 7     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install 

Run make

  

Generate the corresponding hello. ko File

  

Then load the hello. ko module. Be sure to load it in the root mode. Then, lsmod will view the loaded module (the book says hello world will be output on the console, but my console does not output it)

  

Uninstall Module

  

Although no output is made on the console, you can view the output of the hello module in/var/log/syslog.

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.