Você está na página 1de 27

Linux Session - II

Agenda
System Initialization
Root File System
Kernels Last Boot Steps
init Process
Initial RAM Disk and use of initramfs

Device Driver Basics


Device Driver Concepts
Kernel Module Utilities
Driver Methods

Proprietary & Confidential. Aricent Group 2011

Root File System


Linux like many other advanced operating system requires a root file
system.
The root file system refers to the file system mounted at the base of the
file system hierarchy.
Linux typically mounts several file systems on different locations in the
file system hierarchy.
The proc file system is a special purpose file system mounted at /proc
under the root file system.
Root file system has special requirements for a Linux system.
Linux expects the root file system to contain programs and utilities to
boot a system, initialize services such as networking and a system
console, load device drivers and mount additional file systems.

Proprietary & Confidential. Aricent Group 2011

Root File System Hierarchy

Proprietary & Confidential. Aricent Group 2011

Kernels Last Boot Steps


The final snippet of the code from /init/main.c is given below.
This is the final sequence of events for the kernel thread called init
spawned by the kernel during the final stage of boot.
Run_init_process() is the small wrapper around the execve() function.
The execve() function never returns if there is no error.

Proprietary & Confidential. Aricent Group 2011

init Process
/sbin/init is spawned by the kernel on boot and becomes the first user
space program to run.
Kernel does following:
1. Mount the root file system
2. Spawn the first user program, init
Init is the ultimate parent of all user space processes in Linux system.
init provides the default set of environment parameters for all other
processes to inherit, e.g. PATH and CONSOLE
init processs primary role is to spawn additional process under the
direction of a special configuration file.
This configuration file is usually stored at/etc/inittab
Init has concept of a runlevel. A runlevel can be thought of as a system
state.

Proprietary & Confidential. Aricent Group 2011

init Process
Each runlevel is defined by the services enabled and programs
spawned upon entry to that runlevel.
init can exist in a single runlevel at any given time.
Runlevels used by init includes runlevels from 0 to 6 and a special
runlevel S
For each runlevel, a set of startup and shutsown scripts is usually
provided that define the action a system should take for each runlevel.
Action to perform for a given runlevel are determined by the /etc/inittab
configuration file.
The runlevel scripts are commonly found under a directory called
/etc/rc.d/init.d
In this directory, most of the scripts are found to enable and disable
individual services, e.g. nfs service.

Proprietary & Confidential. Aricent Group 2011

init Process

Proprietary & Confidential. Aricent Group 2011

Initial RAM Disk and use of initramfs


Linux kernel contains a mechanism to mount an early root file system to
perform certain startup related system initialization and configuration.
This is known as the initial RAM disk or initrd.
Support for this functionality must be compiled into the kernel.
This kernel configuration option is under Block devices->RAM disk
support in the kernel configuration utility.

Proprietary & Confidential. Aricent Group 2011

Initial RAM Disk Purpose


Initial RAM disk is a small self contained root file system that usually
contains directives to load specific device drivers before the completion
of the boot cycle.
For example in Red Hat and Fedora Core, uses initial RAM disk to load
the device driver for the EXT3 file system before mounting the real root
file system.
initird is frequently used to load a device driver that is required to
access the real root file system.
To use the initrd functionality, the bootloaders are involved on most
architectures to pass the initrd image to the kernel.
Boot loader loads the compressed kernel image into the memory and
then loads an initrd image into another section of memory .
It is bootloader responsibility to pass the load address of the initrd
image to the kernel before passing control to it.

Proprietary & Confidential. Aricent Group 2011

10

Initial RAM Disk


Some architecture constructs a simple composite binary image.
In this case kernel and initrd image are simply concatenated together.
This is done when the bootloader doesnt have specific linux support for
loading initrd images.
Kernel knows where to find the initrd image. It is done via passing the
initrd image start address and size to the kernel via the kernel command
line.
console=ttyS0,115200 root=/dev/nfs
nfsroot=192.168.1.10:/home/saurabh/sandbox/embedded-linux
initrd=0x10800000, 0x14af47
tftpboot 0x10000000 kernel-uImage
tftpboot 0x10800000 initrd-uboot
tftpboot 0x10000000 0x10800000

Proprietary & Confidential. Aricent Group 2011

11

Initial RAM Disk


When the kernel mounts the initial ramdisk, it looks for a special file
linuxrc. It treats this file as a script file and proceed to execute the
commands in this file.
Initramfs is new mechanism (kernel 2.6.x) for executing early user
space programs.
Conceptually similar to initrd.
Kernel documentation for initramfs is available at
/Documentation/filesystems/ramfs-rootfs-initramfs.txt

Proprietary & Confidential. Aricent Group 2011

12

Device Driver Concepts


Fundamental purpose of device driver is to isolate the users programs
from aceess to critical kernel data structures and hardware devices.
Linux has the capability to add and remove kernel components at
runtime.
Device drivers can also be statically compiled into the kernel.
Loadable modules are installed after the kernel has booted.
Startup scripts can load device driver modules and modules can also be
loaded on need basis.
Loadable modules are commonly referred as LKM (Loadable Kernel
Module)
Device drivers are broadly classified into categories, character devices
(serial streams of sequential data) and block devices (read and write
blocks of data from random locations.

Proprietary & Confidential. Aricent Group 2011

13

Minimal Device Driver Example

Proprietary & Confidential. Aricent Group 2011

14

Device Driver
Device drivers are installed by default in /lib/modules/<kernel-version>/
directory.
This can be change by INSTALL_MOD_PATH directive.
Make arch=arm CROSS_COMPILE=mipsse64INSTALL_MOD_PATH=/hme/saurabh/sandbox/embedded-linux/
module_install
Loading and unloading of module is done by modprobe command.
modprobe hello1

Hello Example Init

modprobe r hello1

Hello Example exit

Proprietary & Confidential. Aricent Group 2011

15

Kernel Module Utilities


insmod: It is the simplest way to insert a module into a running kernel.

$ insmod
/lib/modules/2.6.14/kernel/drivers/char/examples/hello1.ko

Proprietary & Confidential. Aricent Group 2011

16

Kernel Module Utilities

lsmod: It displays a formatted list of the modules inserted into the


kernel.

Proprietary & Confidential. Aricent Group 2011

17

Kernel Module Utilities

modprobe: modprobe utility can discover the relationship between


modules and load the dependent modules in proper order.

$ modprobe ext3 command loads the jbd.ko and ext3.ko modules.

$modprobe r ext3

Modprobe utility is driven by a configuration file modeprobe.conf

Proprietary & Confidential. Aricent Group 2011

18

Kernel Module Utilities

modprobe: modprobe utility can discover the relationship between


modules and load the dependent modules in proper order.

$ modprobe ext3 command loads the jbd.ko and ext3.ko modules.

$modprobe r ext3

Modprobe utility is driven by a configuration file modeprobe.conf

Modules inter-dependency is determined via modules.dep file in the


same location where modules are installed.

rmmod: It is used to remove a module from a running kernel.

modinfo: It is used to show information about the kernel module

Proprietary & Confidential. Aricent Group 2011

19

Driver Methods

The open() method is used to prepare the driver for subsequent operations.

read()/ write() routines are used for reading and writing to the driver.

A release() routine is used to clean up after operations are completed, basically a close()
call.

A special system call is provided for non-standard communication to the driver. This is
called ioctl().

Proprietary & Confidential. Aricent Group 2011

20

Driver Methods

Proprietary & Confidential. Aricent Group 2011

21

Driver Methods

Proprietary & Confidential. Aricent Group 2011

22

Driver Methods

Proprietary & Confidential. Aricent Group 2011

23

Device Nodes and mknod

A device node is a special file type in Linux that represents a device.

Device nodes are generally kept in a common location, /dev.

A dedicated utility mknod is used to create a device node on a file system.

$ mknod /dev/hello1 c 234 0

The above command creates a new file /dev/hello1, that represents sample device driver
shown in earlier slides.

Listing of this file shows as follows:

$ ls l /dev/hello1
crw-rr--

1 root root

234, 0 Jul 04 2014

/dev/hello1

The parameters passed to mknod include the name, type major and minor numbers for
/dev/hello1 device.

Because of its special status as a device node, application developer is to bind it to an


installed device driver.

If an application process issues an open() system call with this device node as the path
parameter, the kernel searches for a valid device driver registered with a major number
that matches the device node, in this case, 234, This is the mechanism by which kernel
associates particular device driver to device node.
Proprietary & Confidential. Aricent Group 2011

24

Use the Device Driver

Proprietary & Confidential. Aricent Group 2011

25

Use the Device Driver

Proprietary & Confidential. Aricent Group 2011

26

Thanks

Você também pode gostar