19
CSC414 “Introduction to UNIX/ Linux” Lecture 2

CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Embed Size (px)

Citation preview

Page 1: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

CSC414 “Introduction to UNIX/ Linux”

Lecture 2

Page 2: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Schedule 1. Introduction to Unix/ Linux

2. Kernel Structure and Device Drivers.

3. System and Storage Structure.

4. Processes and Inter- Process Communication.

5. Shell Programming.

6. User Management and Disk Quota.

7. System Management.

8. Network Management.

9. Security.

10. Advanced System Administration.

2015 Qassim University, College of Computer, 9th level 2

Page 3: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Contents:• Linux Kernel

• Overview • Structure.• modules

• Device Drivers• Overview• Types• attributes.

• Interfacing Device Drivers with the Kernel

2015 Qassim University, College of Computer, 9th level 3

Page 4: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Linux KernelThe kernel hides the system’s hardware underneath an

abstract, high-level programming interface.Written mostly in C, with a sprinkling of assembly

language to help it interface with hardware- or chip-specific functions that are not accessible through normal compiler directives.

2015 Qassim University, College of Computer, 9th level 4

Page 5: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Cont. the kernel creates all the following concepts from lower-level hardware features:

• Processes (time-sharing, protected address spaces)

• Signals and semaphores

• Virtual memory (swapping, paging, mapping)

• The filesystem (files, directories, namespace)

• General input/output (specialty hardware, keyboard, mouse,

USB)

• Interprocess communication (pipes and network connections)

2015 Qassim University, College of Computer, 9th level 5

Page 6: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

How the Kernel knows about the system's hardware?• . Informed by the system admin/ developer about the

hardware it should expect to find.

• . Prospects for many devices on its own, either at boot time or dynamically (once the system is running)

2015 Qassim University, College of Computer, 9th level 6

Page 7: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Kernel Structure

• Almost all Unix platforms run monolithic kernels.

• The entire operating system runs in kernel

space, a section of memory reserved for

privileged operating system functions. 

• Services such as device drivers, interprocess

communication, virtual memory, and scheduling

run in the same address space.

2015 Qassim University, College of Computer, 9th level 7

Page 8: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Modules:

• Modern monolithic kernels support on-demand loading of modules.

• Incorporate device drivers and other kernel functions as needed

without rebuilding the kernel and rebooting.

• Drivers, filesystems, and new system calls are all commonly

implemented as modules.

• The memory used by a module is allocated and freed as the code

is loaded or removed, using the insmod and rmmod commands.

• Useful for embedded systems with limited memory.

2015 Qassim University, College of Computer, 9th level 8

Page 9: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

How to load a kernel module?

Two ways:

1.Use the insmod command to manually insert the it into the kernel.

2.Load the module as it is needed; this is known as demand loading.•To view the status of modules in the Linux Kernel:

lsmod•To view a module information use the commend

modinfo (module name)•Demand loaded kernel modules are normally kept in /lib/modules/kernel-version

2015 Qassim University, College of Computer, 9th level 9

Page 10: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Cont.• When the kernel discovers the need for a module, it will request that

the kernel daemon (kerneld) attempts to load it.

• The kernel daemon is a normal user process with super user

privileges.

• When it is started up, usually at system boot time, it opens up an

Inter-Process Communication (IPC) channel to the kernel.

• This link is used by the kernel to send messages to

the kerneld asking for various tasks to be performed.

• Kerneld does not perform these tasks itself, it runs the neccessary

programs such as insmod to do the work.

2015 Qassim University, College of Computer, 9th level 10

Page 11: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Drivers and Device Files

• A device driver is a program that manages the system’s

interaction with a particular type of hardware.

• Translates between the hardware commands understood by

the device and the stylized programming interface used by

the kernel.

• It keeps the kernel reasonably device independent.

• It is part of the kernel, can be accessed both from within the

kernel and from user space

• Device drivers are system specific

2015 Qassim University, College of Computer, 9th level 11

Page 12: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Devices Types:

2015 Qassim University, College of Computer, 9th level 12

• Character devices: are read and written directly without

buffering, for example the system's serial

ports /dev/cua0 and /dev/cua1.

• Block devices: can only be written to and read from in

multiples of the block size, typically 512 or 1024 bytes. Block

devices can be accessed via their device special file but more

commonly they are accessed via the file system. Only a block

device can support a mounted file system.

• Network devices: are accessed via the BSD socket 

Page 13: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Attributes

2015 Qassim University, College of Computer, 9th level 13

• kernel code: Device drivers are part of the kernel and, like other code within the kernel, if they go wrong they can seriously damage the system. A badly written driver may even crash the system, possibly corrupting file systems and losing data.

• Kernel interfaces: Device drivers must provide a standard interface to the Linux kernel or to the subsystem that they are part of. For example, the terminal driver provides a file I/O interface to the Linux kernel and a SCSI device driver provides a SCSI device interface to the SCSI subsystem which, in turn, provides both file I/O and buffer cache interfaces to the kernel.

• Kernel mechanisms and services: Device drivers make use of standard kernel services such as memory allocation, interrupt delivery and wait queues to operate.

Page 14: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Cont.• Loadable: Most of the Linux device drivers can be loaded on

demand as kernel modules when they are needed and unloaded when they are no longer being used. This makes the kernel very adaptable and efficient with the system's resources.

• Configurable: Linux device drivers can be built into the kernel. Which devices are built is configurable when the kernel is compiled.

• Dynamic: As the system boots and each device driver is initialized it looks for the hardware devices that it is controlling. It does not matter if the device being controlled by a particular device driver does not exist. In this case the device driver is simply redundant and causes no harm apart from occupying a little of the system's memory.

2015 Qassim University, College of Computer, 9th level 14

Page 15: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Where to find the device files?

• Most devices have a corresponding file in /dev.

• Files in /dev each have a major and minor device number associated

with them. The kernel uses these numbers to map device-file

references to the corresponding driver.

• The major device number identifies the driver with which the file is

associated (in other words, the type of device). The minor device

number usually identifies which particular instance of a given device

type is to be addressed. The minor device number is sometimes

called the unit number.

2015 Qassim University, College of Computer, 9th level 15

Page 16: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Where to find the device files?• You can see the major and minor number of a device file with ls -l:

• This example shows the first SCSI disk on a Linux system. It has a major number of 8 and a minor number of 0.

2015 Qassim University, College of Computer, 9th level 16

Page 17: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Interfacing Device Drivers with the Kernel

• The Linux kernel must be able to interact with the drivers in standard ways.

• Each class of device driver, character, block and network, provides common interfaces that the kernel uses when requesting services from them.

• The kernel can treat often very different devices and their device drivers absolutely the same.

• For example, SCSI and IDE disks behave very differently but the Linux kernel uses the same interface to both of them.

2015 Qassim University, College of Computer, 9th level 17

Page 18: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Interfacing Device Drivers with the Kernel

• Every time a Linux kernel boots it may encounter different physical devices and thus need different device drivers.

• Linux allows to include device drivers at kernel build time via its configuration scripts.

• When these drivers are initialized at boot time they may not discover any hardware to control. Other drivers can be loaded as kernel modules when they are needed.

• Device drivers register themselves with the kernel as they are initialized.

• Linux maintains tables of registered device drivers as part of its interfaces with them. These tables include pointers to routines and information that support the interface with that class of devices.

2015 Qassim University, College of Computer, 9th level 18

Page 19: CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage

Reading• Chapter 13 form “Unix® and Linux® System Administration

Handbook, Fourth Edition, Video Enhanced Edition”.• The Linux Kernel on the tld Project (http://www.tldp.org/LDP/tlk/)

2015 Qassim University, College of Computer, 9th level 19