Linux Session II

27
Linux Session - II

description

about linux

Transcript of Linux Session II

Tekelec Program Program Review Meeting

Linux Session - IISystem InitializationRoot File SystemKernels Last Boot Stepsinit ProcessInitial RAM Disk and use of initramfsDevice Driver BasicsDevice Driver ConceptsKernel Module UtilitiesDriver Methods

AgendaProprietary & Confidential. Aricent Group 2011 2Linux 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.

Root File SystemProprietary & Confidential. Aricent Group 2011 3Root File System HierarchyProprietary & Confidential. Aricent Group 2011 4

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.

Kernels Last Boot StepsProprietary & Confidential. Aricent Group 2011 5

/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 system2. Spawn the first user program, initInit 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 CONSOLEinit processs primary role is to spawn additional process under the direction of a special configuration file.This configuration file is usually stored at/etc/inittabInit has concept of a runlevel. A runlevel can be thought of as a system state.

init ProcessProprietary & Confidential. Aricent Group 2011 6Each 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.dIn this directory, most of the scripts are found to enable and disable individual services, e.g. nfs service.

init ProcessProprietary & Confidential. Aricent Group 2011 7init ProcessProprietary & Confidential. Aricent Group 2011 8

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.

Initial RAM Disk and use of initramfsProprietary & Confidential. Aricent Group 2011 9

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.

Initial RAM Disk PurposeProprietary & Confidential. Aricent Group 2011 10Some 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, 0x14af47tftpboot 0x10000000 kernel-uImagetftpboot 0x10800000 initrd-uboottftpboot 0x10000000 0x10800000

Initial RAM DiskProprietary & Confidential. Aricent Group 2011 11When 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

Initial RAM DiskProprietary & Confidential. Aricent Group 2011 12Fundamental 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.

Device Driver ConceptsProprietary & Confidential. Aricent Group 2011 13Minimal Device Driver ExampleProprietary & Confidential. Aricent Group 2011 14

Device drivers are installed by default in /lib/modules// directory.This can be change by INSTALL_MOD_PATH directive.Make arch=arm CROSS_COMPILE=mipsse64- INSTALL_MOD_PATH=/hme/saurabh/sandbox/embedded-linux/ module_installLoading and unloading of module is done by modprobe command.modprobe hello1 Hello Example Initmodprobe r hello1 Hello Example exitDevice DriverProprietary & Confidential. Aricent Group 2011 15insmod: 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

Kernel Module UtilitiesProprietary & Confidential. Aricent Group 2011 16

Kernel Module UtilitiesProprietary & Confidential. Aricent Group 2011 17

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

Kernel Module UtilitiesProprietary & Confidential. Aricent Group 2011 18modprobe: 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 ext3Modprobe utility is driven by a configuration file modeprobe.conf

Kernel Module UtilitiesProprietary & Confidential. Aricent Group 2011 19modprobe: 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 ext3Modprobe utility is driven by a configuration file modeprobe.confModules 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

Driver MethodsProprietary & Confidential. Aricent Group 2011 20The 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().

20Driver MethodsProprietary & Confidential. Aricent Group 2011 21

21Driver MethodsProprietary & Confidential. Aricent Group 2011 22

22Driver MethodsProprietary & Confidential. Aricent Group 2011 23

23Device Nodes and mknodProprietary & Confidential. Aricent Group 2011 24A 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 0The 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 rootroot234, 0Jul 04 2014 /dev/hello1The 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.

24Use the Device DriverProprietary & Confidential. Aricent Group 2011 25

25Use the Device DriverProprietary & Confidential. Aricent Group 2011 26

26Thanks