Linux kernel source tree build load Hello module

Source: Internet
Author: User

Before loading the module, the book said to first establish the kernel source tree, then, how to establish the kernel source tree?

First, you need to know the kernel version of your OS and use Uname-r to find

  

The corresponding version directory can be seen under the/url/src/directory

  

If no kernel can be obtained with Apt-cache search Linux-source

  

Download the kernel with sudo apt-get install linux-source-3.13.0

After the download is complete, a corresponding compressed file is generated in the folder linux-source-3.13.0.tar.bz2

Then unzip the file tar jxvf linux-source-3.13.0.tar.bz2

Enter the extracted directory

Then enter the root permission su (here into the failure prompt authentication failure), enter the following command in the terminal:
sudo passwd
Password: Your current password
Enter New UNIX Password: This is the root password
Retype new UNIX Password: duplicate root password
You will then be prompted for successful information.

Enter the command make oldconfig to start configuring the kernel.

Before checking the data on the Internet to see the configuration kernel will take about one hours, but I input the command output

  

The data says that after the configuration is complete, a new directory is generated under/lib/modules/lib/modules/3.16.0-30-generic/

 in this directory ls a bit found already exists this directory = =! That is, the original system already exists kernel source tree .

  

Now that the kernel source tree already exists, all we have to do is load the Hello module into the kernel.

Here is the source code for Hello

  

1#include <linux/init.h>2#include <linux/module.h>3Module_license ("Dual BSD/GPL"); 4 Static intHello_init (void) 5 { 6PRINTK (Kern_alert"Hello, world\n."); 7     return 0; 8 } 9 Static voidHello_exit (void) Ten {  OnePRINTK (Kern_alert"Goodbye, Cruel world\n");  A }  - Module_init (hello_init); -Module_exit (Hello_exit);

Writing Makefile files to code

1 obj-m: =2 kerneldir: =/lib/modules/3.16. 0-3 PWD: =45     $ (make)-C $ ( Kerneldir) m=67     

Run make

  

Generate the appropriate Hello.ko file

  

Then load the Hello.ko module, note to load in root mode, and then Lsmod view the loaded module (the book says Hello World is output in the console, but my console does not output)

  

Uninstalling the module

  

Although the console does not output, you can view the output of the Hello module under/var/log/syslog

  

Linux kernel source tree build load Hello module

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.