Modules module compilation and loading under Ubuntu16.04

Source: Internet
Author: User

First, the corresponding driver to write the relevant content:(the simplest hello.c program)

1#include <linux/init.h>2#include <linux/module.h>3Module_license ("Dual BSD/GPL");4Module_author ("Mdaxia");5 6 Static int__init Hello_init (void)7 {8PRINTK (Kern_alert"Hello world!\n");9         return 0;Ten } One Static void__exit Hello_exit (void) A { -PRINTK (Kern_alert"Goodbye,cruel world!"); - } the Module_init (hello_init); -Module_exit (Hello_exit);

second, the preparation of the corresponding makefile file:(note makefile, first letter capital M)

1 ifeq ($ (kernelrelease),)2kdir:=/lib/modules/$ (shell uname-r)/Build3pwd:=$ (shell pwd)4 Modules:5$ (make)-C $ (kdir) m=$ (PWD) modules6 Modules_install:7$ (make)-C $ (kdir) m=$ (PWD) Modules_install8 Clean :9RM-RF *.o *.ko. Depend *.mod.o *.mod.c module.* modules.*Ten . Phony:modules Modules_install Clean One Else AObj-m: =hello.o -endif

C. Compile the program with the make command to generate the target file Hello.ko

sudo make

The instructions for sudo make are used to guarantee the execution of the run and files, and so on, and the result of the compilation is as follows:

Here we have successfully generated the. ko file that we need

Use the sudo make clean command to clear the associated intermediate and target files:

sudo make clean

This clears all the files.

Four, install the loading module, the need is root permissions:

The collection of related errors is as follows (I do not know how to solve the problem, and finally fluke solved, but back to think that the analysis of these error hints is really helpful):

Question1: here focus on the third line, error is often modified from top to bottom of the way, so here is the hint is-fstack-protector-strong not supported by compiler, The meaning is clear, our current GCC compiler version does not support-fstack-protector-strong this parameter, When this parameter is called in the Linux-headers-4.4.0-96-generic related file, since it is not supported, then we will find a supported GCC version will be compiled and run it, by consulting the relevant information to get: '- Fstack-protector-strong ' option is gcc4.9 later version to join, that is, need to install gcc4.9 later version can be compiled through, so directly installed gcc5.4 on the line, about installing multiple versions of the GCC content please refer to my article: Ubuntu16.04 Multiple versions of the GCC compiler installation and switchover after the installation is complete, the above problem does not occur without direct make compilation.

1Make-c/lib/modules/4.4.0- the-generic/build m=/home/ubuntu-mm/armtools/code_arm/Modulesprogram Modules2make[1]: Entering Directory'/usr/src/linux-headers-4.4.0-96-generic'3Makefile:702: Cannot use Config_cc_stackprotector_strong:-fstack-protector-strong not supported by compiler4Scripts/makefile.build: -:/home/ubuntu-mm/armtools/code_arm/modulesprogram/makefile:no such file or directory5make[2]: * * * No rule to make target'/home/ubuntu-mm/armtools/code_arm/modulesprogram/makefile'. Stop.6Makefile:1423: recipe forTarget'_module_/home/ubuntu-mm/armtools/code_arm/modulesprogram'failed7make[1]: * * * [_module_/home/ubuntu-mm/armtools/code_arm/modulesprogram] Error28make[1]: Leaving directory'/usr/src/linux-headers-4.4.0-96-generic'9Makefile7: recipe forTarget'Modules'failedTenMake: * * * [modules] Error2

Question2: Here we focus on the description of line fourth, the first make[1] Error tip:/home/ubuntu-mm/armtools/code_arm/modulesprogram/makefile: No such file or directory, look carefully, found here the file name is makefile, recall my file name is makefile, may be the problem, so rename: MV Makefile makefile results successfully compiled ~ cheers! So it is better to use makefile to write it ~

1[Email protected]:~/armtools/code_arm/modulesprogram$ sudo make2Make-c/lib/modules/4.4.0- the-generic/build m=/home/ubuntu-mm/armtools/code_arm/Modulesprogram Modules3make[1]: Entering Directory'/usr/src/linux-headers-4.4.0-96-generic'4Scripts/makefile.build: -:/home/ubuntu-mm/armtools/code_arm/modulesprogram/makefile:no such file or directory5make[2]: * * * No rule to make target'/home/ubuntu-mm/armtools/code_arm/modulesprogram/makefile'. Stop.6Makefile:1423: recipe forTarget'_module_/home/ubuntu-mm/armtools/code_arm/modulesprogram'failed7make[1]: * * * [_module_/home/ubuntu-mm/armtools/code_arm/modulesprogram] Error28make[1]: Leaving directory'/usr/src/linux-headers-4.4.0-96-generic'9Makefile7: recipe forTarget'Modules'failedTenMake: * * * [modules] Error2

Question3:insmod in the process of inserting the module, the following questions, check the relevant blog is the module signature related issues, see more: http://blog.csdn.net/hui872370036/article/details/69950869

Insmod:ERROR:could not insert module./hello.ko:required Key Not available

The kernel supports the module signature from 3.7 onwards, which enables the kernel to only allow the installation of modules with specific key signatures, as shown in the kernel configuration:

 CONFIG_MODULE_SIG=y Indicates that the signature mechanism is turned on, but it can be used when the module is signed or not signed.

 CONFIG_MODULE_SIG_FORCE=y If the above configuration item is enabled, the module must have the correct signature in order to work properly.

 CONFIG_MODULE_SIG_ALL=y The kernel does not take the initiative to sign the module when it is compiled, unless you open the above configuration item.

Finish ~

Modules module compilation and loading under Ubuntu16.04

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.