Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 265a7483afc48e27c236b36e810be507 > files > 175

lkmpg-1.1.0-23.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Compiling Kernel Modules</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="The Linux Kernel Module Programming Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Hello World"
HREF="c147.html"><LINK
REL="PREVIOUS"
TITLE="Hello, World (part 1): The Simplest Module"
HREF="x149.html"><LINK
REL="NEXT"
TITLE="Hello World (part 2)"
HREF="hello2.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>The Linux Kernel Module Programming Guide</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x149.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 2. Hello World</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="hello2.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN209"
></A
>2.2. Compiling Kernel Modules</H1
><P
>Kernel modules need to be compiled with certain gcc options to make them work. In addition, they also need to be
	compiled with certain symbols defined. Former kernel versions required us to care much about these settings,
	which are usually stored in Makefiles. Although hierarchically organized, many redundant settings accumulated in
	sublevel Makefiles and made them large and rather difficult to maintain.

	Fortunately, there is a new way of doing these things, called kbuild, and the build process for external loadable modules is now 
	fully integrated into the standard kernel build mechanism.  To learn more on how to compile modules which are not part of the
	official kernel (as ours), see file <TT
CLASS="FILENAME"
>linux/Documentation/kbuild/modules.txt</TT
>.</P
><P
>So, let's look at a simple Makefile for compiling a module named <TT
CLASS="FILENAME"
>hello-1.c</TT
>:</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN217"
></A
><P
><B
>Example 2-2. Makefile for a basic kernel module</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>obj-m += hello-1.o
</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><P
>Now you can compile the module by issuing the command <B
CLASS="COMMAND"
> make -C /usr/src/linux-`uname -r` SUBDIRS=$PWD modules </B
>.
	You should obtain an output which resembles the following:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>[root@pcsenonsrv test_module]# make -C /usr/src/linux-`uname -r` SUBDIRS=$PWD modules
make: Entering directory `/usr/src/linux-2.6.x
  CC [M]  /root/test_module/hello-1.o
  Building modules, stage 2.
  MODPOST
  CC      /root/test_module/hello-1.mod.o
  LD [M]  /root/test_module/hello-1.ko
make: Leaving directory `/usr/src/linux-2.6.x
	</PRE
></FONT
></TD
></TR
></TABLE
><P
>Please note that kernel 2.6 introduces a new file naming convention: kernel modules now have a <TT
CLASS="FILENAME"
>.ko</TT
> 
	extension (in place of the old <TT
CLASS="FILENAME"
>.o</TT
> extension) which easily distinguishes them from conventional object files.
	Additional details about Makefiles for kernel modules are available in <TT
CLASS="FILENAME"
>linux/Documentation/kbuild/makefiles.txt</TT
>.
	Be sure to read this and the related files before starting to dig into Makefiles.</P
><P
>Now it is time to insert your freshly-compiled module it into the kernel with <B
CLASS="COMMAND"
>insmod ./hello-1.ko</B
> 
	(ignore anything you see about tainted kernels; we'll cover that shortly).</P
><P
>	All modules 
	loaded into the kernel are listed in <TT
CLASS="FILENAME"
>/proc/modules</TT
>.  Go ahead and cat that file to see that your module
	is really a part of the kernel.  Congratulations, you are now the author of Linux kernel code!  When the novelty wares off,
	remove your module from the kernel by using <B
CLASS="COMMAND"
>rmmod hello-1</B
>.  Take a look at
	<TT
CLASS="FILENAME"
>/var/log/messages</TT
> just to see that it got logged to your system logfile.</P
><P
>Here's another exercise to the reader.  See that comment above the return statement in
	<TT
CLASS="FUNCTION"
>init_module()</TT
>?  Change the return value to something non-zero, recompile and load the module again.  What
	happens?</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x149.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="hello2.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Hello, World (part 1): The Simplest Module</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c147.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Hello World (part 2)</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>