Build the Linux kernel source code tree and load the hello Module

Source: Internet
Author: User

Build the Linux kernel source code tree and load the hello Module

Build the Linux kernel source code tree and load the hello Module

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.

You can see the corresponding version directory under the/url/src/directory.

If no kernel is available, you can use apt-cache search linux-source to obtain the kernel.

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

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

Decompress the file tar jxvf linux-source-3.13.0.tar.bz2

Enter the decompressed directory

 

Then enter the root permission su (the failure prompt Authentication failure is displayed here), and 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
Then, a message indicating success is displayed.

Enter the command make oldconfig to start Kernel configuration.

It took about an hour to view the configuration Kernel on the Internet, but I output the command

After configuration, a new directory/lib/modules/3.16.0-30-generic/will be generated under/lib/modules/

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

# Include <linux/init. h>
# Include <linux/module. h>
MODULE_LICENSE ("Dual BSD/GPL ");
Static int hello_init (void)
{
Printk (KERN_ALERT "Hello, world \ n ");
Return 0;
}
Static void hello_exit (void)
{
Printk (KERN_ALERT "Goodbye, cruel world \ n ");
}
Module_init (hello_init );
Module_exit (hello_exit );

Write Makefile for code

Obj-m: = hello. o
KERNELDIR: =/lib/modules/3.16.0-30/build
PWD: = $ (shell pwd)
Modules:
$ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules
Modules_install:
$ (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.

How to install Kernel 4.0.2 on CentOS 7

How to install Linux Kernel 4.0 on CentOS 7

How to install Linux kernel 4.0 on Ubuntu/CentOS?

How to install Linux kernel 13.10 On Ubuntu 3.12

How to install the 3.16.7 CKT2 kernel in Ubuntu 14.10, Ubuntu 14.04, and its derivative versions

Linux Kernel: click here
Linux Kernel: click here

This article permanently updates the link address:

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.