1
0
mirror of https://github.com/bashrc/LKMPG.git synced 2018-06-11 03:06:54 +02:00

Sanitize the intro

This commit is contained in:
Bob Mottram
2016-03-08 11:32:36 +00:00
parent 464928fb4d
commit 77b20ffcb7
2 changed files with 471 additions and 355 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -32,50 +32,92 @@ The source code and discussions should apply to most architectures, but I can't
The following people have contributed corrections or good suggestions: Ignacio Martin, David Porter, Daniele Paolo Scarpazza, Dimo Velev, Francois Audeon, Horst Schirmeier and Bob Mottram.
* Introduction
** Obtaining the Debian kernel
#+BEGIN_SRC sh
mkdir -p ~/develop/kernel
cd ~/develop/kernel
sudo apt-get -y install fakeroot build-essential devscripts
sudo apt-get -y build-dep linux
apt-get -y source linux
cd linux-*
make -f debian/rules clean
make menuconfig
#+END_SRC
Select *save*, enter the name *.config* then exit. The to build the kernel.
#+BEGIN_SRC sh
fakeroot make
#+END_SRC
** What Is A Kernel Module?
So, you want to write a kernel module. You know C, you've written a few normal programs to run as processes, and now you want to get to where the real action is, to where a single wild pointer can wipe out your file system and a core dump means a reboot.
What exactly is a kernel module? Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system. Without modules, we would have to build monolithic kernels and add new functionality directly into the kernel image. Besides having larger kernels, this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.
** How Do Modules Get Into The Kernel?
** Kernel module package
You can see what modules are already loaded into the kernel by running lsmod, which gets its information by reading the file /proc/modules.
Linux distros provide the commands /modprobe/, /insmod/ and /depmod/ within a package.
How do these modules find their way into the kernel? When the kernel needs a feature that is not resident in the kernel, the kernel module daemon kmod[1] execs modprobe to load the module in. modprobe is passed a string in one of two forms:
On Debian:
* A module name like softdog or ppp.
#+BEGIN_SRC sh
sudo apt-get install kmod
#+END_SRC
* A more generic identifier like char-major-10-30.
** What Modules are in my Kernel?
If modprobe is handed a generic identifier, it first looks for that string in the file /etc/modprobe.conf.[2] If it finds an alias line like:
To discover what modules are already loaded within your current kernel use the command *lsmod*.
#+BEGIN_SRC sh
sudo lsmod
#+END_SRC
Modules are stored within the file /proc/modules, so you can also see them with:
#+BEGIN_SRC sh
sudo cat /proc/modules
#+END_SRC
This can be a long list, and you might prefer to search for something particular. To search for the /fat/ module:
#+BEGIN_SRC sh
sudo lsmod | grep fat
#+END_SRC
** How Do I Install Modules Into The Kernel?
When the kernel needs a feature that is not currently resident in the kernel, the kernel module daemon kmod[1] runs the *modprobe* command to load it. modprobe is passed a string in one of two forms:
* A module name like /softdog/ or /ppp/.
* A more generic identifier like /char-major-10-30/.
If modprobe is handed a generic identifier, it first looks for that string in the file */etc/modprobe.conf*.[2] If it finds an alias line like:
#+BEGIN_SRC sh
alias char-major-10-30 softdog
#+END_SRC
it knows that the generic identifier refers to the module softdog.ko.
it knows that the generic identifier refers to the module /softdog.ko/.
Next, modprobe looks through the file /lib/modules/version/modules.dep, to see if other modules must be loaded before the requested module may be loaded. This file is created by depmod -a and contains module dependencies. For example, msdos.ko requires the fat.ko module to be already loaded into the kernel. The requested module has a dependency on another module if the other module defines symbols (variables or functions) that the requested module uses.
Next, modprobe looks through the file */lib/modules/version/modules.dep*, to see if other modules must be loaded before the requested module may be loaded. This file is created by *depmod -a* and contains module dependencies. For example, /msdos.ko/ requires the /fat.ko/ module to be already loaded into the kernel. The requested module has a dependency on another module if the other module defines symbols (variables or functions) that the requested module uses.
Lastly, modprobe uses insmod to first load any prerequisite modules into the kernel, and then the requested module. modprobe directs insmod to /lib/
modules/version/[3], the standard directory for modules. insmod is intended
to be fairly dumb about the location of modules, whereas modprobe is aware of the default location of modules, knows how to figure out the dependencies and load the modules in the right order. So for example, if you wanted to load the msdos module, you'd have to either run:
Lastly, modprobe uses *insmod* to first load any prerequisite modules into the kernel, and then the requested module. modprobe directs insmod to */lib/modules/version/*[3], the standard directory for modules. insmod is intended to be fairly dumb about the location of modules, whereas modprobe is aware of the default location of modules, knows how to figure out the dependencies and load the modules in the right order. So for example, if you wanted to load the /msdos/ module, you'd have to either run:
#+BEGIN_SRC sh
insmod /lib/modules/2.6.11/kernel/fs/fat/fat.ko
insmod /lib/modules/2.6.11/kernel/fs/msdos/msdos.ko
sudo insmod /lib/modules/3.16.0-4-amd64/kernel/fs/fat/fat.ko
sudo insmod /lib/modules/3.16.0-4-amd64/kernel/fs/msdos/msdos.ko
#+END_SRC
or:
#+BEGIN_SRC sh
modprobe msdos
sudo modprobe msdos
#+END_SRC
What we've seen here is: insmod requires you to pass it the full pathname and to insert the modules in the right order, while modprobe just takes the name, without any extension, and figures out all it needs to know by parsing /lib/modules/version/modules.dep.
Linux distros provide modprobe, insmod and depmod as a package called module-init-tools. In previous versions that package was called modutils. Some distros also set up some wrappers that allow both packages to be installed in parallel and do the right thing in order to be able to deal with 2.4 and 2.6 kernels. Users should not need to care about the details, as long as they're running recent versions of those tools.
What we've seen here is: insmod requires you to pass it the full pathname and to insert the modules in the right order, while modprobe just takes the name, without any extension, and figures out all it needs to know by parsing */lib/modules/version/modules.dep*.
Now you know how modules get into the kernel. There's a bit more to the story if you want to write your own modules which depend on other modules (we calling this `stacking modules'). But this will have to wait for a future chapter. We have a lot to cover before addressing this relatively high-level issue.