Building of a module (driver) linux kernel by using of configure, automake, autoconf and other autotools.

Modules of kernel Linux are files with the.ko extension. As a rule the insmod command or modprobe command is used for loading of the module team, or the modprobe team is used. For unloading of the module the command rmmod is used. For viewing of the list of the loaded modules there is a lsmod command. For getting of the information about the module the modinfo command is used.

Here is an example for module building by using of autotools:
1. Let's create a directory for the project module.
2. Let's create the directory src in the directory module.
3. Let's create the file module_hello.c in src

/*  src/module_hello.c */

#include <linux/init.h>
#include <linux/module.h>

static int __init
hello_init(void)
{
   printk("Hello, world!\n");
   return 0;
}

module_init(hello_init);

static void __exit
hello_exit(void)
{
   printk("Goodbye, world!\n");
}

module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("noname ");
MODULE_DESCRIPTION("\"Hello, world!\" test module");
MODULE_VERSION("printk");

The building algorithm of the kernel module by means of autotools differs from building algorithm of a simple program or library. It is required to create two more additional Makefile.

Let's create a file in the root directory of the module (in the directory module):

# Makefile.am

SUBDIRS = src

The keyword SUBDIRS means that the source files for building are in this directory.

The file named Makefile.am should be also created in the directory src with the following content:

# src/Makefile.am

EXTRA_PROGRAMS = automake_dummy 
automake_dummy_SOURCES = module_hello.c 
module_DATA = module_hello.o 
include ../Makefile.common

automake_dummy_SOURCES — are sources which the module will be build of.

module_DATA = module_hello.o - the module name.

include ../Makefile.common - it is necessary to connect the external file which will run the module building. It has to exist in the root catalog of the module.

In the root directory of the module (in the directory module) let's create the file named Makefile.common

# Makefile.common
moduledir = @moduledir@
KERNEL_LOCATION=@kerneldir@
KBUILD_VERBOSE = 1
MOD_DEVDIR = $(PWD)

export module_DATA

$(module_DATA): $(automake_dummy_SOURCES) 
	mv Makefile Makefile.automake 
	cp $(srcdir)/../Makefile.kernel Makefile 
	CPPFLAGS="" CFLAGS="" LDFLAGS="" \
	$(MAKE) -C $(KERNEL_LOCATION) \
        ARCH="x86" CC="gcc" M=$(PWD) modules\
		KBUILD_VERBOSE=$(KBUILD_VERBOSE) 
	mv Makefile.automake Makefile 

CLEANFILES = $(module_DATA) .$(module_DATA).flags $(module_DATA:.o=.mod.c) $(module_DATA:.o=.@kernelext@) *~

Here a replacement of Makefile and Makefile.kernel takes place and module building is started. Very important parameter of building is initialization of variable KERNEL_LOCATION. It will be initialized in configure, through transfer of parameters. Or it is possible to replace the line:

$(MAKE) -C $(KERNEL_LOCATION) \ 

на

$(MAKE) -C /lib/modules/$(shell uname -r)/build \

The last file to be created is the Makefile.kernel file. It has to exist in a root directory of the module (in the directory module):

#  Makefile.kernel
obj-m=$(module_DATA)
MI_OBJS      = $(module_DATA)

all clean:
	mv Makefile.automake Makefile
	$(MAKE) $@

That is all.

Let's create configure.in by using of autoscan. This program creates the file configure.scan which is the prototype of configure.in.

Let's run the following:

autoscan

Let's edit the configure.scan and then rename it into configure.in:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/module_hello.c])
AC_CONFIG_HEADERS([config.h])

AM_INIT_AUTOMAKE
AC_PATH_KERNEL_SOURCE

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile
                 src/Makefile])
AC_OUTPUT

The macro of AC_PATH_KERNEL_SOURCE adds to configure an option - with-kerneldir, in which it is necessary to set the path (" / to $ lib/modules/(to shell uname - r) / build"). If this parameter will not be set, the path will be found automatically and it will be set by default. This parameter is needed for cross-compilation.
If this parameter is not specified, the path will be found automatically.

THAT IS ALL! Now let's run the following sequence and Makefile is ready.

aclocal - creates aclocal.m4.

autoconf - the utility creates the script configure in accordance with macros which are listed in the file `configure.in'.

autoheader — creates the header file (config.h.in).

touch NEWS README AUTHORS ChangeLog (these files are needed for automake)

automake --add-missing - the program creates the files "Makefile.in" based on the files "Makefile.am".

configure — creates Makefile

make — creates a module