Thursday 20 February 2014

Linux Kernel

Linux Kernel

The Kernel is the core of the operating system: controls the communication between hardware and process using devices drivers modules. It provides an isolate environment for each running process and communicates each process with others process. The Kernel is stored on /boot partition, is loaded in memory by the boot loader and since this moment takes the system control.

/proc

The /proc directory uses a virtual filesystem, files and directories are not stored on the hard disk. This directory is the interface between the kernel and hardware, the information that contains represent the current state of the system process and the hardware controlled by them. Commands like ps read the process information from /proc :

$ sleep 1000 &
[1] 4329


It takes a PID=4329. All the information about how the Kernel manages this process can be seen on /proc/4329 :

$ ls -lrt /proc/4329

...
-rw------- 1 root root 0 Nov 14 17:44 mem
lrwxrwxrwx 1 root root 0 Nov 14 17:44 exe -> /bin/sleep
lrwxrwxrwx 1 root root 0 Nov 14 17:44 cwd -> /root
...
In can be seen the command that originated the process '/bin/sleep', from which directory the command has been launched '/root', the process memory usage, etc . Once the process has finished the execution the directory that contains the process information is deleted.
* In addition some files on /proc can be modified in order to force a change on how the running Kernel is managing the process. For example :

$ echo 1 >> /proc/sys/net/ipv4/ip_forward
It enables IP forwarding on IPv4 (routing)

$ echo rhel6 > /proc/sys/kernel/hostname
It changes the system hostname to rhel6

This changes are applied directly on the running Kernel and they are lost when the system is rebooted. In order to make this changes permanent the file /etc/sysctl.conf can be used. For more info : man sysctl.conf and man sysctl .
* Some files on /proc can be access in order to obtain system information :

$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 15
model name : Intel(R) Core(TM)2 Duo CPU T7100 @ 1.80GHz

It shows information about system CPUs

$ cat /proc/cmdline
ro root=/dev/mapper/vg_rhel6-lv_root rd_LVM_LV=vg_rhel6/lv_root rd_LVM_LV=vg_rhel6/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=es rhgb quiet

It shows the parameters used by the boot loader in the Kernel initialization

Kernel Modules

The Linux Kernel is modular, his functionality can be extended just adding kernel modules as extensions of the core Kernel. A Kernel module can provide a device driver to control new hardware and can be plugged and removed as needed.

* The Kernel modularity allows that a failure on a Kernel module do not cause the whole system failure.

* Kernel modules keep the initial Kernel core small is size: it decreases the boot time and increases the system performance.
Kernel modules can be managed using the tools installed by the package 'module-init-tools':

To list all loaded Kernel modules use 'lsmod' command :

$ lsmod
Module &nbs Size Used by
xt_CHECKSUM 921 1
...
ip_tables 9541 3 iptable_mangle,iptable_nat,iptable_filter
...
To display more information about a Kernel module use 'modeinfo' command :

$ modinfo ip_tables
filename: /lib/modules/2.6.32-71.el6.i686/kernel/net/ipv4/netfilter/ip_tables.ko
description: IPv4 packet filter
author: Netfilter Core Team
license: GPL
srcversion: DC70E5A33C988577C75C5E0
depends:
vermagic: 2.6.32-71.el6.i686 SMP mod_unload modversions 686



To load/unload a Kernel Module and all its dependencies use 'modprobe' command :

$ modprobe nfs
Now with lsmod (or dmesg) can be verified that the module has been loaded correctly

$ modprobe -lt net
It loads all kernel modules on /lib/modules/`uname -r`/kernel/drivers/net

$ modprobe -r rt2x00usb
It unloads the rt2x00usb Kernel module

To load Kernel modules in the init process the configuration files on /etc/modprobe.d/*.conf, for example to load the ALSA Sound Kernel Modules in the init process the system uses the file /etc/modprobe.d/dist-alsa.conf :

$ cat /etc/modprobe.d/dist-alsa.conf

# ALSA Sound Support
#
# We want to ensure that snd-seq is always loaded for those who want to use
# the sequencer interface, but we can't do this automatically through udev
# at the moment...so we have this rule (just for the moment).
#
# Remove the following line if you don't want the sequencer.

install snd-pcm /sbin/modprobe --ignore-install snd-pcm && /sbin/modprobe snd-seq



* The Kernel modules are located on /lib/modules/`uname -r`/kernel/drivers and are module_name.ko binary files, where the module_name is the name of the module to be used on modprobe command.

Kernel RPMS

The following are the rpms contained in a standard distribution :

kernel
Contains the kernel for single/multicore/multiprocessor system.

kernel-devel
Development kernel version, contains the kernel header files needed to build Kernel modules for the matching Kernel.

kernel-debug
Contains the Kernel with debug options enabled for debugging process.

kernel-debug-devel
Development version of kernel-debug.

kernel-doc
Kernel documentation files. They are installed on /usr/share/doc/kernel-doc-* directory

kernel-headers
Includes the C code files needed to compile the Kernel

kernel-firmware
Contains the devices firmware files.

Kernel Upgrade

If your system is connected to a rpm repository the Kernel upgrade is done automatically or running 'yum install kernel'. Sometimes the newest rpm Kernel version required is not available on the rpm repository, in this case a manually upgrade process must be followed :

1.- Download the latest rpm Kernel version from a trusted site.

2.- Install the latest rpm version.

rpm -ihv kernel*.rpm

* Note : Use always the installations options (-ihv) instead of upgrade options (-Uhv). If upgrade options are used the old kernel will be REMOVED and in case the new Kernel fails the old Kernel will no be available !!!.

Kernel Source Code and Compilation (NOT supported on RHEL6)

The Linux Kernel source code is available via the Kernel Source Code RPM. One of the reasons to use the Kernel Source RPM is to recompile the Linux Kernel with specific options that in the standard Kernel Compilation (used to build Kernel binary RPM) has not been set-up. These are the steps that must be followed in order to recompile the Kernel source code in order to get a customized kernel :

Kernel source code installation

Download and install the Kernel Source Code.

$ rpm -ihv kernel-2.6.32-19.el6.src.rpm

It installs all the files needed to build the Kernel Source Code on /root/rpmbuild/SOURCES and the spec file to build the Kernel Source code on /root/rpmbuild/SPECS/kernel.spec

Build and install the Kernel Source Code with the rpmbuild command (installed by rpm-build rpm)

$ cd /root/rpmbuild/SPECS/
$ rpmbuild -bp kernel.spec
...
it takes a while...
It installs the Kernel source code on /root/rpmbuild/BUILD directory.

* Note: In order to build the Kernel source code 4G of free space on "/" are needed.

Kernel Configuration

Once the Kernel source code has been installed, the next step is customize the Kernel configuration in order to fit our specials requirements :

$ cd /root/rpmbuild/BUILD/kernel-2.6.32/linux-2.6.32.i686/
$ vi Makefile
modify line -->EXTRAVERSION=lso-customWhere is the string that will identify our customized Kernel, in this case 'lso-custom'
:wq!

$ make mrproper
...
It cleans up previous kernel configurations if needed and verify that all files are ready for the Kernel configuration.

$ cp /boot/config-2.6.18-53.el5 /tmp
It makes a backup of the running Kernel configuration just before start the configuration process.

$ make menuconfigIts shows a menu where kernel configuration options like filesystem support, devices drivers, etc. can be added/removed in the kernel compilation. In this case we have added 'KVM Virtualization Support'

* Note: 'make menuconfig' uses the ncurses*.rpm, so these packages must be installed.

Kernel Compilation

Once the Kernel source code has been configured with our special requirements is time to compile the new Kernel.

$ make rpm
...
it takes a while...
Wrote: /root/rpmbuild/RPMS/i386/kernel-2.6.32lso_custom-1.i386.rpm
...


In /root/rpmbuild/RPMS/i386 has been created or customized kernel rpm : kernel-2.6.32lso_custom-1.i386.rpm

Kernel Installation

Last step is install the new customized Kernel on the system with the 'rpm' command, configure manually the boot loader to load the new Kernel and create manually the initial RAM space for the new Kernel. These actions are not needed when the Kernel is installed with the standard binary rpm because of these actions are performed automatically on the rpm installation process.

$ cd /root/rpmbuild/RPMS/i386/
$ rpm -ihv kernel-2.6.32lso_custom-1.i386.rpm


* As mentioned this custom rpm do not update the /etc/grub.conf in order to be booted. This action needs to be done manually :

1.- Creation of the initial RAM disk to boot the new kernel on /boot.

$ cd /boot
$ dracut initramfs-2.6.32lso_custom.el6.i686.img 2.6.32lso_custom


Now in /boot we have the new kernel 'vmlinuz-2.6.32lso_custom' and his initial RAM disk used to be booted 'initramfs-2.6.32lso_custom.el6.i686.img'

2.- Last step is modify /etc/grub.conf in order to boot the new kernel.

$ vi /etc/grub.confAdd the following lines :
title Red Hat Enterprise Linux LSO (2.6.32lso_custom.el6.i686)
root (hd0,0)
kernel /vmlinuz-2.6.32lso_custom ro root=/dev/mapper/vg_rhel6c-lv_root rhgb quiet
initrd /initramfs-2.6.32lso_custom.el6.i686.img

Change the line 'default=0' to 'default=1' to load the new kernel by default
:wq!

If all goes fine in the next reboot the new kernel will be loaded, if not the old kernel will be available.

No comments :

Post a Comment