2. A kernel is the foundational layer of an
operating system (OS). It functions at a basic
level, communicating with hardware and
managing resources, such as RAM and the CPU.
Since a kernel handles many fundamental
processes, it must be loaded at the beginning of
the boot sequence when a computer starts up.
Kernel
3. Kernel 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.
A module can be configured as built-in or
loadable.
Kernel modules offers an easy way to extend
the functionality of the base kernel without
having to rebuild or recompile the kernel again.
Most of the drivers are implemented as a Linux
kernel modules.
Kernel module
4. When those drivers are not needed, we can
unload only that specific driver, which will reduce
the kernel image size.
To dynamically load or remove a module, it has to
be configured as a loadable module in the kernel
configuration.
The kernel modules will have a .ko extension. On
a normal linux system, the kernel modules will
reside inside
/lib/modules/<kernel_version>/kernel/
directory.Modules are stored
in/usr/lib/modules/kernel_release.
You can use the command uname -r to get your
current kernel release version.
5. Utilities to Manipulate Kernel Modules:
1. lsmod – List Modules that Loaded Already
lsmod command will list modules that are
already loaded in the kernel as shown below.
# lsmod
Module Size Used by
ppp_deflate 12806 0
zlib_deflate 26445 1 ppp_deflate
bsd_comp 12785 0
..
6. 2. insmod – Insert Module into Kernel
insmod command will insert a new module into
the kernel as shown below.
# insmod
/lib/modules/3.5.0-19-generic/kernel/fs/squashfs/
squashfs.ko
# lsmod | grep "squash"
squashfs 35834 0
3. modinfo – Display Module Info
modinfo command will display information
about a kernel module as shown below.
7. # modinfo
/lib/modules/3.5.0-19-generic/kernel/fs/squashfs/squas
hfs.ko
4. rmmod – Remove Module from Kernel
rmmod command will remove a module from the
kernel. You cannot remove a module which is already
used by any program.
# rmmod squashfs.ko
5. modprobe – Add or Remove modules from the kernel
modprobe is an intelligent command which will
load/unload modules based on the dependency
between modules.
8. To load a module:
# modprobe module_name
To load a module by filename (i.e. one that is not
installed in /usr/lib/modules/$(uname -r)/):
# insmod filename [args]
To unload a module:
# modprobe -r module_name
Or, alternatively:
# rmmod module_name
9. To list the options that are set for a loaded
module:
$ systool -v -m module_name
To display the comprehensive configuration of
all the modules:
$ modprobe -c | less
To display the configuration of a particular
module:
$ modprobe -c | grep module_name f.
List the dependencies of a module (or alias),
including the module itself:
$ modprobe --show - depends module_name
10. Kernel modules can be explicitly loaded during
boot and are configured as a static list in files
under /etc/modules-load.d/. Each configuration
file is named in the style of /etc/modules-
load.d/<program>.conf.
Configuration files simply contain a list of kernel
modules names to load, separated by newlines.
Empty lines and lines whose first non-
whitespace character is # or ; are ignored.
11. Files in /etc/modprobe.d/ directory can be used
to pass module settings to udev, which will
use modprobe to manage the loading of the
modules during system boot. Configuration
files in this directory can have any name, given
that they end with the .conf extension.
The syntax is:
/etc/modprobe.d/myfilename.conf options
module_name parameter_name = parameter_value
12. Q.Write a Simple Hello World Kernel Module
1.Installing the linux headers
You need to install the linux-headers-.. first
as shown below. Depending on your distro,
use apt-get or yum.
# apt-get install build-essential linux-headers-$(uname -
r)
2. Hello World Module Source Code
Next, create the hello.c module in C
programming language.
13. 3. Create Makefile to Compile Kernel Module
The following makefile can be used to compile
the above basic hello world kernel module.
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD)
modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD)
clean
Use the make command to compile hello world
kernel module as shown below.
14. # make
make -C /lib/modules/3.5.0-19-generic/build
M=/home/nu11secur1ty/a modules
The above will create hello.ko file, which is our
sample Kernel module.
15. 4. Insert or Remove the Sample Kernel
Module
Now that we have our hello.ko file, we can
insert this module to the kernel by using
insmod command as shown below.
# insmod hello.ko
# dmesg | tail -1
[ 8394.731865] Hello world!
# rmmod hello.ko
# dmesg | tail -1
[ 8707.989819] Cleaning up module.
16. A process is an executing (i.e., running) instance of
a program. Processes are managed by
the kernel (i.e., the core of the operating system),
which assigns each a unique process identification
number (PID). There are three basic types of
processes in Linux: interactive, batch and daemon
Daemons: A daemon is a process that runs in the
background, not connecting to any controlling
terminal. Daemons are normally started at boot
time, are run as root or some other special user
(such as apache or postfix), and handle system-
level tasks.
Kernel daemon
17. As a convention, the name of a daemon often
ends in d (as in crond and sshd), but this is not
required, or even universal.
A daemon has two general requirements:
It must run as a child of init.
It must not be connected to a terminal.
Kernel threads are processes started directly
by the kernel itself. They delegate a kernel
function to a separate process and execute it
there in ‘‘parallel‘‘ to the other processes in the
system (and, in fact, in parallel to execution of
the kernel itself). Kernel threads are often
referred to as (kernel) daemons.
18. They are used to perform, for example, the
following tasks:
• To periodically synchronize modified memory
pages with the block device from which the
pages originate (e.g., files mapped using
mmap ).
• To write memory pages into the swap area if
they are seldom used.
• To manage deferred actions.
• To implement transaction journals for
filesystems.
19. Basically, there are two types of kernel
thread:
Type 1 — The thread is started and waits until
requested by the kernel to perform a specific
action.
Type 2 — Once started, the thread runs at
periodic intervals, checks the utilization of a
specific resource, and takes action when
utilization exceeds or falls below a set limit
value. The kernel uses this type of thread for
continuous monitoring tasks.
23. The bdflush kernel daemon is a simple kernel
daemon that provides a dynamic response to
the system having too many dirty buffers;
buffers which contain data which must be
written out to disk at some time. It is started as
a kernel thread at system startup time and,
rather confusingly, it calls itself ''kflushd'' and
that is the name that you will see if you use the
ps command to show the processes in the
system.
The bdflush Kernel Daemon
24. Mostly this daemon sleeps waiting for the
number of dirty buffers in the system to grow
too large. As buffers are allocated and discarded
the number of dirty buffers in the system is
checked. If there are too many as a percentage
of the total number buffers in the system then
bdflush is woken up.
The default threshold is 60% but, if the system is
desperate for buffers,bdflush will be woken up
anyway.
This value can be seen and changed using the
update command.
25. The kerneld feature was introduced during the
1.3 development kernels by Bjorn Ekwall. It
allows kernel modules such as device drivers,
network drivers and filesystems to be loaded
automatically when they are needed, rather
than having to do it manually with modprobe
or insmod.
And for the more amusing aspects, although
these are not (yet ?) integrated with the
standard kernel:
What is kerneld?
26. It can be setup to run a user-program instead
of the default screen blanker, thus letting you
use any program as a screen-saver.
Similar to the screen-blanker support, you can
also change the standard console beep into
something completely different.
kerneld consists of two components:
Support in the Linux kernel for sending
requests to a daemon requesting a
module for a certain task.
A user-space daemon that can figure out what
modules must be loaded to fulfill
the request from the kernel.
27. Both components must be working for the
kerneld support to function; it is not enough
that only one or the other has been setup.
Why do I want to use it ?
There are some good reasons for using kerneld.
If you have to build kernels for several systems
that only differ slightly -different kind of network
card, for instance - then you can build a single
kernel and some modules, instead of having to
build individual kernels for each system.
28. Modules are easier for developers to test. You
don't need to reboot the system to load and
unload the driver; this applies to all modules, not
just kerneld loaded ones.
It cuts down on the kernel memory usage
leaving more memory available for applications.
Memory used by the kernel is never swapped out,
so if you have 100Kb worth of unused drivers
compiled into your kernel, they are simply
wasting RAM.
29. Some of the things I use, the ftape floppy-tape
driver, for instance, or iBCS, are only available as
modules, but I don't want to bother with loading
and unloading them whenever I need them.
People making Linux distributions don't have
to build 284 different boot images: Each user
loads the drivers he needs for just his hardware.
Most modern Linux distributions will detect your
hardware and will only load those modules
actually required.
30. Kernel modules are object files that contain
code to extend the kernel of an operating
system. Kernel modules are used to add
support for new hardware and/or filesystems,
or for adding system calls. Modules can be built
into the kernel or compiled as loadable kernel
modules.
Advantages
• A module is loadable without reboot (at least
most of them).
/etc/conf.d
31. • Results in smaller kernel memory footprint
(when the module is not loaded).
• Can be loaded on demand by udev (for
example DVB drivers for a DVB stick).
• Allows easy reloading of kernel drivers in
case of module crash.
• Allows specifying module-specific parameters
in /etc/conf.d/modules.
32. Automatic loading:
Loadable modules can be defined in
the /etc/conf.d/modules file in order to load
modules to the kernel during the init process.
Blacklist
To avoid a module from loading, add it to a file
in /etc/modprobe.d/:
FILE /etc/modprobe.d/blacklist.conf
blacklist uhci_hcd blacklist nvidia
Loadable kernel modules
33. Manual loading:
A module can be loaded or unloaded manually
by the modprobe command. For example, to
unload the nvidia module and load
the nouveau module, run:
# modprobe –r nvidia
# modprobe nouveau
To list currently loaded modules, run lsmod.
34. If you load a module using modprobe, you
can set the parameters in
/etc/modprobe.conf (or other file – depends
on your distribution).
If you compile the module with the kernel
(static), you can provide
the parameters using the kernel command
line on boot time.
When you load a module using insmod
command you can supply the parameters as
key=value pairs
Module parameters
35. For example:
The parameter can be a number, a string or an
array (of numbers or strings)
# insmod ./mymod.ko irq=20 name=mydev debug=1
address=0x1000,0x2000,0x3000
36. Kernel modules (drivers) are in the
subdirectory /lib/modules/'kernel-version'.
/lib/modules/'kernel-version'
-The home of all the kernel modules.
/lib/modules/'kernel-version'/isapnpmap.dep
-has details on ISA based cards, the modules
that they require and various other attributes.
/lib/modules/'kernel-version'/modules.dep
-lists all modules dependencies.This file can
be updated using the depmod command.
/lib/modules
37. /lib/modules/'kernel-version'/pcimap
-is the PCI equivalent of the
/lib/modules/'kernel-version'/isapnpmap.dep file.
/lib/modules/'kernel-version'/usbmap
-is the USB equivalent of the
/lib/modules/'kernel-version'/isapnpmap.dep file.