L120: Linux System Administration II



Yüklə 1,05 Mb.
səhifə2/16
tarix11.10.2017
ölçüsü1,05 Mb.
#4275
1   2   3   4   5   6   7   8   9   ...   16

The Linux Kernel



Prerequisites


    • Understand shell tools and commands (see LPI 101)

    • Experience compiling and installing software from source (see LPI 101)



Goals


    • Manage Linux kernel modules

    • Configure the kernel source

    • Compile and install a kernel


1. Kernel Concepts

The two different types of Linux kernel are:


A: Monolithic
A monolithic kernel is one which has support for all hardware, network, and filesystem

compiled into a single image file.


B: Modular
A modular kernel is one which has some drivers compiled as object files, which the kernel can load and remove on demand. Loadable modules are kept in /lib/modules.


The advantage of a modular kernel is that it doesn’t always need to be recompiled when hardware is added or replaced on the system. Monolithic kernels boot slightly faster than modular kernels, but do not outperform the modular kernel.

2. The Modular Kernel

Many components of the Linux kernel may be compiled as modules which the kernel can dynamically load and remove as required.





  • The modules for a particular kernel are stored in /lib/modules/.



  • The best components to modularise are ones not required at boot time, for example peripheral devices and supplementary file systems.



  • Kernel modules are controlled by utilities supplied by the modutils package:




    • lsmod list currently loaded modules

    • rmmod remove a single module

    • insmod insert a single module

    • modprobe insert a module and dependencies listed in modules.dep

    • modinfo list information about the author, license type and module parameters

Many modules are dependant on the presence of other modules. A flat file database of module dependencies /lib/modules//modules.dep is generated by the command. This command is run at boot time (for example by the rc.sysinit script).

-- modprobe will load any module and dependent modules listed in modules.dep (or conf.modules)

Search for example for modules that will be loaded at the same time as tvaudio.




grep tvaudio /lib/modules/kernel-version/modules.dep

/lib/modules/kernel-version/kernel/drivers/media/video/tvaudio.o: \

/lib/modules/kernel-version/kernel/drivers/i2c/i2c-core.o

This means that the module i2c-core.o will also be loaded when using modprobe. This dependency is also apparent when listing the module with lsmod:




lsmod

Module Size Used by Not tainted

tvaudio 16796 0 (unused)

i2c-core 19236 0 [tvaudio]

-- /etc/modules.conf, /etc/modprobe.conf or /etc/modprobe.d is consulted for module parameters (IRQ and IO ports) but most often contains a list of aliases. These aliases allow applications to refer to a device using a common name. For example the first ethernet device is always referred to as eth0 and not by the name of the particular driver.




Sample /etc/modules.conf file

alias eth0 e100

alias usb-core usb-uhc

alias sound-slot-0 i810_audio

alias char-major-108 ppp_generic

alias ppp-compress-18 ppp_mppe
# 100Mbps full duplex

options eth0 e100_speed_duplex=4



--modinfo will give information about modules.





modinfo tvaudio

filename: /lib/modules/kernel-version/kernel/drivers/media/video/tvaudio.o

description: "device driver for various i2c TV sound decoder / audiomux chips"

author: "Eric Sandeen, Steve VanDeBogart, Greg Alexander, Gerd Knorr"

license: "GPL"

parm: debug int

parm: probe short array (min = 1, max = 48), description "List of adapter,address pairs to scan additionally"

parm: probe_range short array (min = 1, max = 48), description "List of adapter,start-addr,end-addr triples to scan additionally"

parm: ignore short array (min = 1, max = 48), description "List of adapter,address pairs not to scan"

parm: ignore_range short array (min = 1, max = 48), description "List of adapter,start-addr,end-addr triples not to scan"

parm: force short array (min = 1, max = 48), description "List of adapter,address pairs to boldly assume to be present"

parm: tda9874a_SIF int

parm: tda9874a_AMSEL int

parm: tda9874a_STD int

parm: tda8425 int

parm: tda9840 int

To get information only about parameter option use modinfo -p, to get information about the license type use modinfo -l , etc.

-- kmod is a mechanism that allows the kernel to automatically load modules as needed (one seldom needs to insert modules manually). This is in fact a statically compiled (resident) module that needs to be configured before compiling the kernel. The command used by the kernel to load the modules is defined in /proc/sys/kernel/modprobe.

3. Routine Kernel Recompilation




3.1 Source extraction

The kernel source is stored in the /usr/src/linux directory tree, which is a symbolic link to the



/usr/src/(kernel-version) directory. When extracting a new kernel source archive it is recommended to:


  • remove the symbolic link to the old kernel source directory tree




rm linux

Kernel sources which have been packaged as an RPM often create a link called linux-2-4




  • extract the new source archive (e.g linux-2.4.20.tar.bz2)




tar xjf linux-2.4.29.tar.bz2


Note: The archived 2.2 series kernels create a directory called linux instead of linux-version. This is why the first step is important, otherwise you may overwrite an old source tree with the new one. Since kernel 2.4 the name of the directory is linux-version.


  • create a symbolic link called linux from the newly created directory




ln -s linux-2.4.20 linux




  • The kernel is almost ready to be configured now, but first we need to make sure that all old binary files are cleared out of the source tree, and this is done with the make mrproper command.


Warning: this command will also delete the kernel configuration file .config discussed later.


cd /usr/src/linux

make mrproper




Note: mrproper is a Scandinavian brand of cleaner that gets things “cleaner than clean”, it is one step beyond “make clean”.

3.2 Kernel Configuration

First edit the Makefile and make sure that the “EXTRAVERSION” variable is different from the existing version:

VERSION = 2

PATCHLEVEL = 4

SUBLEVEL = 20

EXTRAVERSION = -test


The kernel is now ready to be configured. This essentially means creating a configuration file called .config. This is done from the kernel source tree directory /usr/src/linux with any of the following


make menuconfig

make xconfig

make config




All these methods will save the configuration file as /usr/src/linux/.config

It is often easier to configure a new kernel using an older .config file by using the make oldconfig command. This will prompt the user only for new features in the kernel source tree (if the kernel is newer or has been patched).


Notice: Some distributions such as RedHat have a configs subdirectory containing files to be used as .config files with predefined configurations.

To enable kernel features (with make menuconfig) you will enter the top level category by moving with the arrow keys and pressing enter to access the desired category. Once in the particular category, pressing the space bar will change the kernel support for a feature or driver.


Possible support types are


  • supported (statically compiled) [*]

  • modular (dynamically compiled) [M]

  • not supported [ ]

The same choices are available with the other menu editors config and xconfig.


Troubleshooting: The make menuconfig target needs the ncurses header files. These are provided by the ncurses-devel package and must be installed for this target to work.



Fig 2: The make xconfig top level menu


3.3 Kernel Compilation


make clean


The make command gets instructions from the Makefile and will build what is needed. If some files are already present make will use them as is. In particular files with *.o extensions. To make sure that all the configuration options in .config are used to rebuild the files needed one has to run make clean (this deletes *.o files)
Notice: you do not need to do “make clean” at this stage if you already prepared the source directory with “make mrproper”

make dep
Once the kernel configuration is complete, it is necessary to reflect these choices in all the subdirectories of the kernel source tree. This is done with the make dep command. The files named .depend containing paths to header files present in the kernel source tree (/usr/src/linux/include) are generated this way.

The kernel itself is compiled with one of the commands:



make zImage

make bzImage

When the command exits without any errors, there will be a file in the /usr/src/linux/ directory called vmlinux. This is the uncompressed kernel.


The two other commands will write an additional file in /usr/src/linux/arch/i386/boot/ called zImage and bzImage respectively. These are compressed kernels using gzip and bzip2. See the next section Installing the New Kernel to find out how to proceed with these files.
make modules
The modules are compiled with make modules.
make modules_install
Once the modules are compiled they need to be copied to the corresponding subdirectory in /lib/modules. The make modules_install command will do that.
The sequence of commands are depicted in Fig 3.

Kernel compilation commands:


make dep

make clean

make bzImage

make modules

make modules_install



3.4 Installing a New Kernel


The new kernel can be found in /usr/src/linux/arch/i386/boot/bzImage, depending on your architecture of your system. Install the new kernel on the system:




make install

The make install command will create the following files in the /boot directory:



  • vmlinuz-3.9.3 – The actual kernel

  • System.map-3.9.3 – The symbols exported by the kernel

  • initrd.img-3.9.3 – initrd image is temporary root file system used during boot process

  • config-3.9.3 – The kernel configuration file

The command “make install” will also update the grub.cfg by default. So we don’t need to manually edit the grub.cfg file.

3.5 The full kernel version

On a system, the version of the running kernel can be printed out with


uname -r
This kernel version is also displayed on the virtual terminals if the \k option is present in /etc/issue.

3.5 Initial Ramdisks

If any dynamically compiled kernel modules are required at boot time (e.g a scsi driver, or the filesystem module for the root partition) they will be loaded using an initial ramdisk.


The initial ramdisk is created with the mkinitrd command which only takes two parameters: the filename, and the kernel version number.
If you use an initial ramdisk then you will need to add an initrd= line in your /etc/lilo.conf


mkinitrd /boot/initrd-full-version.img full-version

3.6 Optional

It is recommended to copy the /usr/src/linux/.config file to /boot/config-, just to keep track of the capabilities for the different kernels that have been compiled.




4. Exercises and Summary




Files

Description

/etc/modules.conf

used by modprobe before inserting a module

/lib/modules/<kernel-version>/

directory where the modules for given kernel version are stored

/lib/modules/<kernel-version>/modules.dep

list of module dependencies created by depmod



Command

Description

depmod

depmod(8) – kernel modules can provide services (called "symbols") for other modules to use (using EXPORT_SYMBOL in the code). If a second module uses this symbol, that second module clearly depends on the first module. Depmod creates a list of module dependencies, by reading each module under /lib/modules/version and determining what symbols it exports, and what symbols it needs. By default this list is written to modules.dep in the same directory

insmod

insmod(8) – a trivial program to insert a module into the kernel: if the filename is a hyphen, the module is taken from standard input. Most users will want to use modprobe(8) instead, which is cleverer

make clean

delete all object files in the source tree

make config

configure the Linux kernel

make dep

creates a list of extra headers in files called .depend needed to satisfy module dependencies

make menuconfig

configure the Linux kernel using a menu

make modules

compile all the external/dynamic modules for this kernel

make modules_install

install the compiled modules in /lib/module/kernel-version

make oldconfig

create a default .config if it doesn't exist. If a .config file already exists the chosen configuration is unchanged. If the source tree has changed, for example after a patch (see LPI 201) or the .config file corresponds to an older kernel, then extra configuration options must be supplied

make xconfig

configure a Linux kernel using a menu

lsmod

list all dynamically loaded modules

modinfo

print information about a kernel module such as the author (-a), the description (-d), the license (-l) or parameters (-p)

modprobe

modprobe(8) - will automatically load all base modules needed in a module stack, as described by the dependency file modules.dep. If the loading of one of these modules fails, the whole current stack of modules loaded in the current session will be unloaded automatically

rmmod

rmmod(8) – tries to unload a set of modules from the kernel, with the restriction that they are not in use and that they are not referred to by other modules


Yüklə 1,05 Mb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   ...   16




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə