You are on page 1of 27

Shikhar Verma 1

 WHAT IS A KERNEL

 Basically we can define a Kernel is the main component of most


computer operating systems. It provides an interface between
applications and actual data processing at the hardware level.

 It is the kernel’s responsibility to control hardware, enforce


security and allocate resources such as CPU & memory.

2
 BASIC FUNCTION OF KERNEL:

 There are various tasks and functions of a kernel but some of the
important are given below:

 Resource allocation- The kernel's primary function is to manage


the computer's resources and allow other programs to run and
use these resources. These resources are- CPU, Memory and I/O
devices.

3
 Process Scheduling: Determine when processes should run and
for how long

 Memory Management : Allocates memory on behalf of running


processes.

 System Initialization: Detects hardware resources and boots up


the systems.

 Security: Constantly verifies FS permissions, Selinux context and


firewall rules
4
 Provides buffer and cache to speed up hardware access.

 A kernel maintains a list of available devices. A device manager


first performs a scan on different hardware buses, such as
Peripheral Component Interconnect (PCI) or Universal Serial Bus
(USB), to detect installed devices, then searches for the
appropriate drivers. The kernel provides the I/O to allow drivers
to physically access their devices through some port or memory
location.

5
What Is A Kernel Module?

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.

6
Module Commands

There are a few commands that allow you to manipulate the kernel.

insmod - install loadable kernel module.


lsmod - list loaded modules.
modinfo - display information about a kernel module.
modprobe - high level handling of loadable modules.
rmmod - unload loadable modules.

7
About modinfo

modinfo shows information about a Linux kernel module.

Description

modinfo extracts information from the Linux kernel modules given on


the command line. If module name is not a file name, then the
/lib/modules/kernel-version directory is searched — the same
directory searched by modprobe when loading kernel modules.

8
About insmod

insmod is a simple program which inserts a module into the Linux


kernel.

insmod is similar to modprobe: it can insert a module into the Linux


kernel. Unlike modprobe, however, insmod does not read its
modules from a set location and automatically insert them and take
care of any dependencies. insmod can insert a single module from
any location, and does not consider dependencies when doing so. It's
a much lower-level program; in fact, it's the program modprobe uses
to do the actual module insertion.

insmod /path/to/snd-usb-audio.ko
9
About lsmod

lsmod shows (or "lists") the status of modules in the Linux kernel.
Overview

Linux kernel modules (LKMs) are pieces of code which can be loaded
into the kernel much like a hot-swappable piece of hardware: they
can be inserted into the kernel and activated without the system
needing to be rebooted.

10
lsmod is a very simple program with no options: it nicely formats the
contents of the file /proc/modules, which contains information about
the status of all currently-loaded LKMs.
lsmod syntax

To list all active kernel modules, simply run lsmod at the command
line:
#lsmod

This is essentially the same as running "cat /proc/modules“

11
You'll see three columns of information:

lsmod will resemble the following:


Module Size Used by
fuse 52176 3
cryptd 14125 0
aes_i586 16647 2

You'll see three columns of information:

12
Module: the name of the module. This is usually the name of the
module file, minus the extension (.o or .ko), but it may have a
custom name, which can be specified as an option when the module
is inserted with the insmod command.

Size: the amount of memory used by the resident module, in bytes.

Used by: This column contains a number representing how many


instances of the module are being used. If the number is zero, the
module is not currently being used. Text after the number represents
any available information about what is using the module: this is
commonly a device name, a filesystem identifier, or the name of
another module.
13
 To get the name of the module that provides that driver:

 ls -l /sys/class/net/<devname>/device/driver/module

 [root@example lib]# ls -l
/sys/class/net/eth0/device/driver/module
 lrwxrwxrwx 1 root root 0 Sep 14 19:32
/sys/class/net/eth0/device/driver/module ->
../../../../module/e1000

 Here e1000 is the module


14
About modprobe

modprobe adds and removes modules from the Linux kernel.


Overview

Modules are pieces of code which extend the functionality of the


operating system kernel without the need to reboot. Once loaded,
modules reside in memory, and can be instantiated multiple times;
they can be thought of as analogous to a device driver.

15
About modprobe

modprobe uses the dependency lists and hardware maps generated


by depmod to intelligently load or unload modules into the kernel. It
performs the actual insertion and removal using the lower-level
programs insmod and rmmod, respectively.
While it's possible to call insmod and rmmod manually, it's
recommended to load and unload modules using depmod to ensure
that any inter-module dependencies are considered before changes
are made.
Technical Description

modprobe searches the module directory, 16


About modprobe

modprobe examples

The following series of commands illustrate a common way to use


modprobe. Each command is prefixed with sudo, since they require
root permissions:

sudo ln -s /path/to/your-kernel-module.ko /lib/modules/`uname -r`

sudo depmod -a

17
sudo modprobe your-kernel-module
About Modprobe
These commands perform the following operations:
 In the first command, we use ln to create a symbolic link to our
module file in the directory /lib/modules/kernel-release. The
command uname -r, enclosed in back quotes, is executed by the
shell and translates to the appropriate string representing our
kernel release version.

 In the second command, an updated dependency list is generated


by depmod -a to make sure that the module we're installing is
aware of all existing modules and dependencies. This dependency
list will be used by modprobe when installing the module in the
third command.

18
 modprobe installs the kernel module.
How to Load and Unload Kernel Modules in Linux

A kernel module is a program which can loaded into or unloaded


from the kernel upon demand, without necessarily recompiling it (the
kernel) or rebooting the system, and is intended to enhance the
functionality of the kernel.

In general software terms, modules are more or less like plugins to a


software such as WordPress. Plugins provide means to extend
software functionality, without them.

Likewise without modules, the kernel would have to be built with all
functionalities integrated directly into the kernel image. This would
mean having bigger kernels, and system administrators would need
19
to recompile the kernel every time a new functionality is needed.
A simple example of a module is a device driver – which enables the
kernel to access a hardware component/device connected to the
system.

In Linux, all modules end with the .ko extension, and they are
normally loaded automatically as the hardware is detected at system
boot. However a system administrator can manage the modules
using certain commands.

To list all currently loaded modules in Linux, we can use the lsmod
(list modules) command which reads the contents of /proc/modules
like this.

20
To list all currently loaded modules in Linux, we can use the lsmod
(list modules) command which reads the contents of /proc/modules
like this.

# lsmod
List Kernel Modules in Linux
Module Size Used by
rfcomm 69632 2
pci_stub 16384 1
vboxpci 24576 0
vboxnetadp 28672 0
vboxnetflt 28672 0
21
…………
How to Load and Unload (Remove) Kernel Modules in Linux

To load a kernel module, we can use the insmod (insert module)


command. Here, we have to specify the full path of the module. The
command below will insert the speedstep-lib.ko module.
# insmod /lib/modules/4.4.0-21-
generic/kernel/drivers/cpufreq/speedstep-lib.ko

To unload a kernel module, we use the rmmod (remove module)


command. The following example will unload or remove the
speedstep-lib.ko module.
# rmmod /lib/modules/4.4.0-21-
generic/kernel/drivers/cpufreq/speedstep-lib.ko 22
How to Manage Kernel Modules Using modprobe Command

modprobe is an intelligent command for listing, inserting as well as


removing modules from the kernel. It searches in the module
directory /lib/modules/$(uname -r) for all the modules and related
files, but excludes alternative configuration files in the
/etc/modprobe.d directory.
Here, you don’t need the absolute path of a module; this is the
advantage of using modprobe over the previous commands.

To insert a module, simply provide its name as follows.


# modprobe speedstep-lib
To remove a module, use the -r flag like this.
# modprobe -r speedstep-lib 23
How to set kernel parameters in Red hat Enterprise Linux

Setting Kernel parameters permanently in Red hat Enterprise Linux

Every time the system boots, the ‘/etc/rc.d/rc.sysinit‘ script is


executed by init process. This shell script contains a call to sysctl
command and reads the values from /etc/sysctl.conf file as the ones
to be set.

Therefore, any values added to /etc/sysctl.conf will take effect after


the system boot or without downtime using “ sysctl -p ” command

sysctl.conf is a simple file containing sysctl values to be read in and


set by sysctl 24
To modify the kernel runtime parameters we will use the sysctl
command.

# echo 0 > /proc/sys/net/ipv4/ip_forward


# sysctl -w net.ipv4.ip_forward=0

Then run following command to apply the changes to the running


configuration.

# sysctl -p
If you want to view the complete list of Kernel parameters, just do:

25
systctl -a|less
 For Example:
# sysctl -a | grep -i dev.parport.parport0.devices.lp.timeslice
 dev.parport.parport0.devices.lp.timeslice = 200

 Please note that the first characters in each line match the names
of the directories inside /proc/sys
 # ls /proc/sys
 crypto debug dev fs kernel net sunrpc vm

 # dev.parport.parport0.devices.lp.timeslice
 # cd /proc/sys/dev/parport/parport0/devices/lp/
 # cat timeslice
26
 200
END of this Course Module.

Thanks
27

You might also like