Wednesday, November 16, 2011

Kernel Modules


TARGETS
è Understanding the purpose and organization of the kernel.
è Understand how to load and configure kernel modules.
è Configuration of kernel.

Linux Kernel

            The kernel is the core part of the Linux operating system. It is kernel responsibility to control the hardware, enforce security and allocation resources such as CPU and memory. Kernel code is divided into core functionally such as scheduling and memory management and kernel modules for device drives, file-systems.

Kernel duties are:

·         System initialization
It detects the hardware resources and boot up the system.
·         Process scheduling
This takes care of when processes should run and for tow long.
·         Memory management
This allocates memory on behalf of running processes.
·         Security
This constantly verifies file system permissions, SELinux contexts and firewall rules.
·         Provides buffers and caches to speed up hardware access.
·         Implements standard network protocols and file system formats.

Kernel Images and Variants

            The main idea behind providing a variety of kernel version is that the more features are built in, the more overhead it will bring. The standard kernel is installed by default. It supports multiple processes known as Symmetric Multiprocessing (SMP). The memory limit per process is 3GB.
            The kernel is always installed on /boot/vmlinuz-*.
The architectures supported are:
            x86, x86_64, IA64/Itanium, PowerPC64, s390x.
The kernel versions available for x86
·         Regular
One or more processes can be installed, but 4GB of RAM or less
·         PAE
Multiple processors can be installed up to 64GB of RAM.
·         XEN
Needed for virtualization .Xen kernel contains the Xen enabled kernel for the host and the guest operating system as well as the hyper visor. Xen is a virtual machine that can securely run multiple operating systems in their own sand boxed domains.

Kernel modules

          A kernel module is an optional portion of kernel code that may be loaded after the kernel initialization .Only a few modules that are essential to all systems, are included directly into the kernel. Dynamic modules that are needed at boot time are loaded by grub into the initial RAM disk (initrd). Other modules may be loaded as needed later and are found in the /lib/modules/ directory. Kernel modules can implement drivers, file systems, firewall, and more.
Kernel modules are used for several reasons. They are:
·         Reduce memory foot print i.e. memory is not used for drivers that are not required.
·         Flexibility
Modules may be added to the system, after it has been installed. These modules are often called third-party modules.
ü Maximizing update
A module may be uploaded and reloaded as many times as wanted with no reboot.

Kernel module utilities

            Modules may be loaded or unloaded using the mod probe utility.
--To load a module:    
Syntax:            #modprobe usb_storage
--To load dependencies automatically and to figure out, where the module is located on the          file system is insmod.
            EX:      [root@server ~]# lsmod
                        Module                  Size  Used by
                        i915                   21569  2
                        drm                    65493  3 i915
                        nfsd                  198769  17
                        exportfs                9665  1 nfsd
                        lockd                  58569  2 nfsd
                         [root@server ~]#       
--To remove a currently loaded module
            Syntax:                        #modprobe     –r        devicename
            EX:                  #modprobe     -r         usb_storage

--To display information of a module,
            Syntax:                        #modinfo device name
            EX.1:               [root@server ~]# modinfo video
                                    filename:       /lib/modules/2.6.18-8.el5/kernel/drivers/acpi/video.ko
                                    license:        GPL
                                    description:    ACPI Video Driver
                                    author:         Bruno Ducrot
                                    srcversion:     5D5108C3A16D85EE29E6AE6
                                    depends:
                                    vermagic:       2.6.18-8.el5 SMP mod_unload 686 REGPARM                                                                                 4KSTACKS gcc-4.1
                                     [root@server ~]#



Management the initrd image
            The initial RAM disk (initrd) provides the system with a series of modules that are not provided by the kernel image but which are still critical to boot the system correctly. These modules are usually associated with storage devices and file systems, but may support other features and hardware peripherals.
            The initrd file is located under /boot/initrd-$ (uname –r).img. Although the file name is typically uses the format above, the name may be modified as long as the grub configuration file, /boot/grub/grub/conf refers to the right file name.
Sometimes extra modules are needed to be added due to:
Ø  New hardware added to the system.
Ø  New features needed to be added such as USB devices.
Ø  Modules need to load automatically at boot time.

To rebuild the initrd file, the mkinitrd command is provided.
--To rebuild initrd file with an extra module:
Syntax:            mkinitrd --with=modulename /boot/initrd-$(uname –r).img $(uname –r)
            Where $ (uname –r) is replaced with the current kernel version.
Accessing a device through /dev
            Device nodes provide access points for device drivers and map standard file permission onto that access.
The two types of device nodes are:
o   Block devices.
        This handles data storage. Block devices use kernel buffered IO.
o   Character devices.
                             This handles data streams. These are do not use buffers.
            Reading from a serial port can be accomplished by using the command
                        #cat /dev/ttyS0
            Writing a word to the same serial port is:
                        #echo “message” > /dev/ttyS0
In general device files are located under /dev.
--To display the attributes of a device file, the command is: 
            #ls –l    /dev/ttyS0
                        crw - rw – r - - - 1 root uucp 4, 64 Jul 21 16:24 /dev/ttyS0
            Here ‘c’ represents the file type (here c is character file).
--To display a block device such as USB disk
            #ls –l    /dev/sda1
                        brw – r - - - - - 1 root disk 8, 1 Jul 4 16:20 dev/sda1

Some of block device names are
ü  /dev/had, /dev/hdc IDE hard disk, CDROM
ü  /dev/sda, /dev/sdb –SCSI, SATA, or USB Storage
ü  /dev/md0, /dev/md1 – Software RAID
Some of character device names are
ü  /dev/tty[0-6] –virtual consoles
ü  /dev/null, /dev/zero –software devices
ü  /dev/random, /dev/urandom –random numbers
            These sudo terminals are dynamically created by a special virtual file system, devpts, under the /dev/pts directory.
Managing /dev with udev
          Linux distributions used to ship with thousands of files under the /dev directory. A newer, better, and more efficient system was then added to replace static files under /dev. This system is called udev. It consists in a series of utilities and configuration files which provide rules that apply whenever a device is connected to the system and detected by the kernel.
            Udev makes is possible to create or remove files on the fly, when the corresponding devices is plugged or disconnected. It is also lets system administrator and rules in order to modify the default names and permissions used under /dev. Rules are located under /dev/udev/rules.d/.




Addition of files under /dev
Adding a device special file under /dev/ can be done in 2 ways. They are:       
u Using mknod command
      EX:         #mknod       /dev/usbdevice            b          8          0
u By adding a new rule to udev
·         The udev method involves adding a file under /dev/udev/rules.d/. This file should contain one or more statements that comply with the udev syntax. A statement such as the following would replace the default /dev/sda file with /dev/usbkey. A symbolic link called /dev/usbstorage would be created. All these files would get automatically created when the device plugged in, and removed when disconnected.
·         Alternatively a file can be quickly created under /dev using  mknod command
      EX:            mknod            /dev/usbdevice b 8 0 
Kernel Configuration with /proc
/proc is a virtual system that can provides detailed information about the kernel, hardware and running process. This is used to find the process information. memory resources, hardware devices, kernel memory, etc. These files are not stored under hard disk. Any modifications on these files can be modified after rebooting. These files can be used to modify network and memory sub systems or modify kernel features.
Some examples on /proc:
Read only files under /proc
      These files are only for read purpose, we can’t change the info. In these files.
u /proc/cupinfo   …………………………………………..contains CPU information
u /proc/partitions …………………………....................contains partition information
u /proc/meminfo …………………………………..…...contains memory information
Read-Write entries under /proc/sys/:
u /proc/sys/kernel/hostname ……………..…....contains information about host name
u /proc/sys/net/ipv4/ip_forward.......contains information about the packet forwarding
u /proc/sys/vm/drop_catches
u /proc/sys/vm/swappiness….....indicates how aggressively memory will be swapped                       
out to the swap device
            kernel parameters provide a mechanism to adjust the functioning of the Linux kernel. We can view and set these kernel parameters.
--To get the list of all parameters and their values
Syntax:            #sysctl –a
--To set a parameter, like net.ipv4.tcp_syscookies
            Syntax:            #sysctl –w net.ipv4.tcp_syncookies=1
        To make this setting permanently add this parameter to the file /etc/sysctl.conf
 Once this done new configuration file could be synchronized with the kernel by.
                                    #sysctl –p
Hardware devices can be monitored in a number of different ways. The /proc file system has historically been the man method, with files such as /proc/devices, /proc/interrupts, /proc/iomem and /proc/ioports.
To make the /proc entries more reliable, utilities such as lspci and lsusb are also provided.

No comments:

Post a Comment