the linux kernel1lin-21]the-linux-kernel.pdf · 2014-08-06 · the linux kernel . course objectives...

58
System’s core Campus-Booster ID : **XXXXX www.supinfo.com Copyright © SUPINFO. All rights reserved The Linux Kernel

Upload: vanthu

Post on 30-Aug-2018

268 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

System’s core Campus-Booster ID : **XXXXX

www.supinfo.com Copyright © SUPINFO. All rights reserved

The Linux Kernel

Page 2: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Your trainer…

Title: **Enter title or job role. Accomplishments: **What makes the presenter qualified to present this course.

Education: **List degrees if important. Publications: **Writings by the presenter on the subject of the course or presentation.

Contact: **Campus-Booster ID: [email protected]

Presenter’s Name

The Linux Kernel

Page 3: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Course objectives

n  Talk about kernel types and Linux architecture. Tanenbaum’s delight.

n  Manage modules and parameters. Make your devices to work.

n  Apply patches. Add third-party features, or even your own !

n  Configure, compile, and install a new kernel. Dive into menuconfig.

By completing this course, you will:

The Linux Kernel

Page 4: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Course topics

n  About kernels. One concept, different implementations.

n  Linux. A focus on your favorite operating system.

n  Module management. Work with modules and parameters.

n  Patching. Apply patches to the kernel sources.

n  Configure and compile. Your very own kernel.

Course’s plan:

The Linux Kernel

Page 5: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

About Kernels

One concept, different implementations

The Linux Kernel

Page 6: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

What’s a kernel ? About Kernels

n  Provide abstractions

n  files / filesystem

n  Network sockets

n  …

n  Manage hardware

n  Allocate CPU time

§ Multitasking

n  Manage memory

§  Process isolation

§  Virtual memory

Turn expensive paperweight into a computer.

Page 7: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Kernel families About Kernels

n  Monolithic kernels

n  everything run in kernel mode (ring 0 on x86)

§  Crash on fault

n  Faster

n  Easier to implement

n  Micro kernel

n  Most services as userland processes

§  No crash on fault, just restart faulty “server”

n  Slower (context switches)

n  Hard to keep in sync

Different implementations to provide the same services.

Page 8: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Monolithic kernel About Kernels

Hardware Drivers

Network stack

Virtual Memory Management

Page 9: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Micro kernel About Kernels

Hardware Drivers

Network stack

Filesystem Micro kernel

Page 10: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think About Kernels

Do you have any questions ?

Page 11: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think About Kernels

Micro kernel

Monolithic

Something else

Both

What do you think about Linux ?

Page 12: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think About Kernels

Micro kernel

Monolithic

Something else

Both

What do you think about Linux ?

Page 13: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Linux

A focus on your favorite operating system

The Linux Kernel

Page 14: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

History Linux

n  Started as a hobby

n  Minix-like

n  Monolithic

n  Released in (19)91

n  To run GNU tools on x86

n  “Never gonna be professional like Hurd”

n  Joined by

n  Individuals

n  Companies

Linus Torvalds’ “Toy OS”.

Page 15: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Linux or GNU/Linux ? Linux

n  Running controversy

n  Considered trivial

n  “Linux” systems

n  GNU Userland

n  Linux kernel

n  Stallman insists on GNU/Linux

n  Getting old

n  FSF “Newspeak”

n  “Linux” more widespread

Vi or Emacs ?

Page 16: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Linux system stack Linux

Page 17: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Development and versioning Linux

n  Sources available from kernel.org

n  Patches

n  Full versions

n  Versioning

n  Version.major.minor-rev

n  Main trunk: 2.6.x

n  Old trunk: 2.4.x

§  Still used for embedded devices

Where is the latest kernel ?

Page 18: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

What’s the running kernel version ? Linux

The uname command

Options Definitions

-a

-s

-r

All information

Kernel name

Kernel release (version no)

[user@linux ~]$ uname [options]

[user@linux ~]$ uname –r 2.6.38.2

Example:

Page 19: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Architecture Linux

n  Monolithic

n  Everything in kernel land

n  Privileged mode

n  Modular

n  Load code at runtime

§  Dynamic linking

n  No difference between loaded and core code

n  Reduce memory footprint

Modules doesn’t mean microkernel

Page 20: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Architecture Linux

n  Modules

n  Functional units

n  Provide features

n  Hardware drivers

n  Wide range

n  Gigantic footprint

n  Load only what’s needed

§  Connected hardware

Modules ?

Page 21: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Linux

Do you have any questions ?

Page 22: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Linux

True

False

Linux kernel modules are running in userland.

Page 23: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Linux

True

False

Linux kernel modules are running in userland.

Page 24: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Modules management

Work with modules and parameters

The Linux Kernel

Page 25: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Module files Modules management

n  Modules are stored as

n  .ko (kernel objects)

n  .ko.gz (compressed)

n  Per kernel

n  /lib/modules/kernel-version n  Per category

n  /lib/modules/2.6.18/kernel/drivers/char/drm/radeon.ko

n  Tools work

n  Files

n  Names

Where are they stored ?

Page 26: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

List currently loaded modules Modules management

With the lsmod command

n  Get a list of currently loaded modules and deps:

root@localhost # lsmod Module Size Used by radeon 108832 1 drm 72852 2 radeon ...

Page 27: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

List currently loaded modules Modules management

With the lsmod command

n  Get a list of currently loaded modules and deps:

root@localhost # lsmod Module Size Used by radeon 108832 1 drm 72852 2 radeon ...

Page 28: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Loading modules Modules management

The modprobe command

Arguments Definitions

options

module

params

man 8 modprobe

module name

Module parameters (see modinfo)

[root@linux ~]# modprobe [options] module [params]

[root@linux ~]# modprobe snd-had-intel model=7c

Example:

Page 29: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Loading modules Modules management

The insmod command

Arguments Definitions

module-file

params

Path to the kernel object

module parameters (see modinfo)

[root@linux ~]# insmod module-file [params]

[root@linux ~]# insmod ./testmod.ko idx=2

Example:

Page 30: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Information about modules Modules management

The modinfo command

[user@linux ~]$ modinfo [options] module|module-file

[user@linux ~]$ modinfo snd-had-intel […]

Example:

Arguments Definitions

options

module

module-file

man 8 modinfo

module name

Module file to read from

Page 31: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Modules dependencies Modules management

The depmod command

[root@linux ~]# depmod [options] [version] [filenames]

[root@linux ~]# depmod

Example:

Arguments Definitions

-a

version

filenames

Probe all modules. Default

Kernel version to work with. Defaults to $(uname -r)

Specific modules to work on

Page 32: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Module unloading Modules management

The rmmod command

[root@linux ~]# rmmod [options] module-name

[root@linux ~]# rmmod pcnet32

Example:

Arguments Definitions

-f

-w

Force module unload. Use with caution.

Prevent new usage of the in-use module. When usage count == 0 proceed with unloading

Page 33: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Modules management

Do you have any questions ?

Page 34: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Modules management

You want to insert a module in the running kernel to support your wireless card. You need to set a custom value for the “prefix” parameter. Which command will you use ?

insmod

modprobe

depmod

rmmod

modinfo

Page 35: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Modules management

You want to insert a module in the running kernel to support your wireless card. You need to set a custom value for the “prefix” parameter. Which command will you use ?

insmod

modprobe

depmod

rmmod

modinfo

Page 36: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Patching

Apply patches to the kernel sources

The Linux Kernel

Page 37: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Kernel flavors Patching

n  Vanilla

n  Official version from kernel.org

n  Distro

n  Customized vanilla

§  Distro patches

n  Your flavor

n  Vanilla/Distro

§  Your patches

§  Third-party patches

Kernels in the wild

Page 38: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Why patching ? Patching

n  New features

n  Not in Vanilla

n  Not in your distro

n  Bugfixes

n  Kernel panics solved

n  Enhancements

§  Performance

n  Supported hardware

n  Various hacks

Customized kernels

Page 39: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

About patches Patching

Patches are released in ”diff" format: text file composed of code to add or to remove

diff command generate diff’s file:

# diff –Nur original modified > diff_file

--- soft-orig/src/amazing.c 2008-01-13 22:27:33.000000000 +0100

+++ soft/src/amazingV2.c 2008-01-13 22:16:32.000000000 +0100

@@ -3,5 +3,6 @@

int main(int argc, char **argv)

{

int result = 0;

+ result =+ 1;

return result;

}

Page 40: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Patch level Patching

n  Patches are creating and applied in different contexts

n  --- soft-orig/src/main.c n  +++ soft-modified/src/main.c

n  Will be applied to

n  soft/src/main.c §  Need to rebase

§  Strip X path components

n  Rebase 1 level

§  src/main.c

§  Apply from soft “root” dir

Rebasing paths.

Page 41: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Working with patches Patching

Apply patch

In the parent directory :

Remove patch :

[user@linux soft-1.2.3]$ patch -p1 < mypatch.diff

[user@linux soft-1.2.3]$ patch -R < mypatch.diff

Page 42: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Patching

Do you have any questions ?

Page 43: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Patching

1

2

3

4

5

The kernel source is in /usr/src/linux. You are in this directory. You want to apply a (poorly written) patch. The patch begins with “--- usr/src/linux/fs/aio.c”. Which patch level are you going to use ?

Page 44: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Patching

1

2

3

4

5

The kernel source is in /usr/src/linux. You are in this directory. You want to apply a (poorly written) patch. The patch begins with “--- usr/src/linux/fs/aio.c”. Which patch level are you going to use ?

Page 45: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Configure and compile

Your very own kernel

The Linux Kernel

Page 46: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Why ? Configure and compile

n  Optimize

n  Only needed features

n  Smaller binary

n  Faster kernel

n  Compile for your arch

n  New features

n  Patched version

n  New version not in your distro repositories

n  The geek touch

Two good reasons and the real one.

Page 47: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

How ? Configure and compile

n  Download the sources

n  kernel.org

n  Be sure to select the ‘F’ull version

n  Unpack in /usr/src

n  You get /usr/src/linux-x.y.z

n  Create /usr/src/linux symlink

n  Configure

n  Compile

n  Install

n  You may need to manually update your bootloader

Quick howto.

Page 48: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Tools Configure and compile

n  Working toolchain

n  Compiler (gcc)

n  Assembler (gas)

n  Linker (ld)

n  The make tool

n  libncurses to do menuconfig

n  development headers

n  Various graphic libs for other *config targets

What you’ll need.

Page 49: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Configure Configure and compile

n  make menuconfig

n  Navigate with arrows

n  Select with enter

n  Read help

n  Search with ‘/’

n  Feature status with space

n  ‘*’ Builtin/enabled

n  ‘M’ Module

n  ‘ ‘ Not build/disabled

n  Use ‘exit’ button to save

Most important step.

Page 50: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Guided tour Configure and compile

Page 51: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Compile and install Configure and compile

n  You may now have to configure the bootloader

n  Many distros have hooks for “make install” stage

# make # make install

# make modules_install

n  Kernel compilation and installation

Page 52: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Configure and compile

Do you have any questions ?

Page 53: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Configure and compile

True

False

The “make install” command copies the kernel image to /boot and the modules to /lib/modules/kernel-version.

Page 54: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Stop-and-think Configure and compile

True

False

The “make install” command copies the kernel image to /boot and the modules to /lib/modules/kernel-version.

Page 55: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Applying patches

Kernel configuration

dialog Kernel types

Compile and install

Course summary

Linux kernel architecture &

modules management

The Linux Kernel

Page 56: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

For more The Linux Kernel

If you want to go into these subjects more deeply, …

Courses Publications

Web sites

www.labo-linux.com

www.blackbeltfactory.com

Linux Technologies: Edge Computing

Conferences

FOSDEM

RMLL

Solutions Linux

If you want to go into these subjects more deeply, …

www.supinfo.com

Understanding the Linux kernel

Page 57: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

Congratulations

You have successfully completed the SUPINFO course module n°22

The Linux kernel

Page 58: The Linux Kernel1LIN-21]The-Linux-kernel.pdf · 2014-08-06 · The Linux Kernel . Course objectives ! ... You want to insert a module in the running kernel to support your wireless

The end

n  Read all config items help text and don’t hesitate to try n  Always keep a working kernel to boot your system

The Linux Kernel