Linux Kernel-module compilation and installation, and Linux kernel module Compilation

Source: Internet
Author: User

Linux Kernel-module compilation and installation, and Linux kernel module Compilation
I didn't install the source code when installing Ubuntu. Before installing the source code, the/usr/src/directory contains only two folders containing the header file of the kernel:

 

 

My kernel version is:

 

 

So the next step is to first install the kernel source code:

 

After execution, the/usr/src/directory has two more folders:

 

Then download the source code and decompress the source code:

 

After decompression/usr/src/linux-3.13.0/folder is the kernel source code.

Then compile the source code.

First go to the/usr/src/linux-3.13.0/folder:

 

Then execute make oldconfig, make prepare, and make scripts in sequence:

Now the source code is installed.

Linux Kernel Directory structure

 

=>/Arch: This subdirectory contains all kernel code related to the architecture. Each subdirectory of the subdirectory represents a supported architecture.

=>/Include: This subdirectory includes most of the header files required to compile the kernel. Files unrelated to the platform are in the/include/ude/linux subdirectory, header files related to intel cpu are in the/include/asm-i386 subdirectory, while the/sort des/scsi/directory is the header file directory for the scsi device.

=>/Init: This subdirectory contains the kernel initialization code.

=>/Mm: This sub-Directory includes all memory management code independent of the cpu architecture, such as page-based storage management memory allocation and release; the Memory Management Code related to the architecture is located at/arch/*/mm/For example/arch/x86/mm/fault. c

=>/Kernel: the main core code. The files in this directory implement kernel functions of most linux systems. The most important file is sched. c; similarly, the architecture-related code is in/arch/*/kernel.

=>/Drivers: Place all the device drivers of the system. Each driver occupies a subdirectory.

=>/Net: core network-related code.

=>/Ipc: the code for inter-process communication between core processes.

=>/Fs: All File System Code and various types of file operation code. Each subdirectory of the Code supports one file system.

 

Let's simply implement a Hello World! Before starting, let's take a look at the Linux kernel module mechanism!

Module mechanism: You can dynamically load or remove a Module from the kernel without re-compiling the kernel as needed.

A module is a program with independent functions. It can be compiled but cannot run independently. It is linked to the kernel during runtime and runs as part of the kernel in the kernel space.

A module is usually composed of a group of functions and data structures to implement a file system, a driver, or other kernel upper-layer functions.

The Kernel Module is an external plug-in provided by the Linux Kernel. It is called the dynamic loadable Kernel Module (LKM.

 

Module programming

There are some restrictions on programming in kernel mode:

I. You cannot use the C standard library in user mode. Because the lib library does not exist in kernel mode, these user functions are not available.

II. floating point operations are not supported because the floating point status of the processor is not saved during Linux kernel switching.

III. Keep the code clean and tidy as much as possible, because kernel debugging is not convenient, concise code can be reduced and convenient for later debugging.

IV. module programming is closely related to kernel versions, because the function names of some functions vary in different kernel versions. Therefore, module programming can also be called Kernel programming.

V. Only Super Users can run on it.

 

Start our Hello World!

No directory structure before Compilation:

Edit the hello. c file first:
 1 #include<linux/module.h> 2 #include<linux/kernel.h> 3 #include<linux/init.h> 4  5 static int hello_init(void) 6 { 7         printk("Hello word"); 8         return 0; 9 }10 static void hello_exit(void)11 {12         printk("Goodbye world");13 }14 15 module_init(hello_init);16 module_exit(hello_exit);17 18 MODULE_LICENSE("GPL");

Then edit the makefile file:

 1 ifneq ($(KERNELRELEASE),) 2 obj-m:=hello.o 3 else 4 PWD:=$(shell pwd) 5 KDIR:=/lib/modules/$(shell uname -r)/build 6 all: 7         $(MAKE) -C $(KDIR) M=$(PWD) 8 clean: 9         rm -rf *.o *.mod.c *.ko *.symvers *.order *.markers10 endif

 

After the editing is complete and check whether there are any errors, run the make command:

 

Then, check the directory structure after compilation:

Many intermediate files are generated. The. o file and the. ko file are kernel objects. Then you can install the module (insmod Module name. ko)

No output indicates that the installation is successful.

We cannot see the output of our own modules on the console, because kernel programming can only view our output by viewing system logs. use dmesg | tail-1 to view the output, or directly view the log file.

Hello: module verification failed: signature and/or required key missing-tainting kernel does not affect module loading.

 

Exit the final module (rm mod Module name ):

 

Such a hello World is complete!

It is necessary to explain the module programming of the hello. c file.

 

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.