exploring linux kernel: the easy way! · problem explore linux kernel tcp/ip stack •solution...

39
EXPLORING LINUX KERNEL: THE EASY WAY! By: Ahmed Bilal Numan 1 Presented in Open Source Series Workshop 2010 22-24 December, 2010 © ICOSST 2010

Upload: others

Post on 04-Jul-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

EXPLORING LINUX KERNEL: THE EASY WAY!

By: Ahmed Bilal Numan

1

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 2: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

PROBLEM Explore linux kernel TCP/IP stack

• Solution

– Try to understand relative kernel code

– Available text

– Run kernel in virtualized environment

• Selected solution

– Develop a virtual machine using QEMU

– Build Linux kernel as Eclipse project

– Interconnect Eclipse with QEMU

– Run Linux kernel in debug mode to see path followed by network packet

2

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 3: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

IN THIS PRESENTATION • Virtualization

• What is QEMU

• Linux

• Root File System

• Linux kernel

• Linux under QEMU

• Debugging Linux kernel

• How to do that

3

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 4: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

VIRTUALIZATION

• Separation of resources from underlying physical resources

• Examples

─ Memory virtualization

─ Partitioning the hard disk

─ Server virtualization

─ Hardware virtualization

4

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 5: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

VIRTUALIZATION

Perceived from "Virtualization Overview" VMWARE white paper 5

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 6: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

WHY VIRTUALIZATION

• Resource utility optimization

• Testing and Development

• Exploration

• Dynamic load balancing

• Etc.

6

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 7: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

WHY VIRTUALIZATION

• Resource utility optimization

• Testing and Development

• Exploration

• Dynamic load balancing

• Etc.

7

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 8: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

HOW TO GET VIRTUALIZATION

• Xen

– Baremetal hypervisor

– Open source virtualization

• VMWare

– A number of virtualization products

• VMWare player

• Ware workstation

• QEMU

8

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 9: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

QEMU • QEMU

– CPU Emulator

– Open source

– Emulation of various architectures

• x86, x86_64

• ARM

• MIPS

– Supports large variety of devices

• Network cards

• CD-ROMs

• Sound cards

9

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 10: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

QEMU • Networking features

– Connection to multiple virtualized instances

– Local host connection using NAT

– Use same network as the host

• Communication between host and guest system

• SMP system simulation

– Multiple CPUs can be virtualized to get an SMP machine

• Remote debugging

10

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 11: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

INTRODUCTION TO LINUX

• Robust, open source OS

• Applications range form servers to embedded systems

• Best option for research purpose

• Two major parts

– Root File system

– Linux kernel

11

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 12: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM

• File system

– File system is hierarchy of directories

• Types of file system

– How data is organized on the disc

– Rules to handle the data

– Examples: ext2, ext3,ext4,NTFS etc.

12

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 13: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM

• Linux file system

– Supports large number of file system types

– Everything starts from “root” directory

– Hierarchy may vary with Linux distribution or system requirement

• Custom file system can also be developed

13

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 14: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /bin

– Contains binary executables

– Common linux commands used by all users

– Example: ls, ping, grep etc.

14

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 15: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /boot

– Contains boot loader related files

– File system image file, grub etc.

15

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 16: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /dev

– Contains device files

– It include all the devices attached to the system

– /dev/ram0, /dev/sda etc.

16

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 17: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /home

– Contains directories of all the users registered to the system

– Two directories in this folder may not accessible to each other

17

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 18: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /lib

– Contains libraries of binaries located in /bin or /sbin

– Example: libc.so.6, libasound.so.2

18

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 19: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /media

– Contains directories for temporarily mounted removable devices

– Example: when USB is attached it is mounted to this directory

19

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 20: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX FILE SYSTEM • File system hierarchy

• /etc

– Contains configuration files for all the programs

– Contains startup and shutdown scripts for programs

20

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 21: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX FILE SYSTEM • File system hierarchy

• /sbin

– Named after system binaries

– Contains executable binaries especially for system administration

– Example: ifconfig, reboot, route

21

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 22: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /sys

– It is utilized by sysfs

– Contains information about plugin devices such as USB

22

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 23: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /tmp

– Contains temporary files generated by system and user

– File in this directory are deleted at reboot

23

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 24: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /usr

– Contains files that can be shared across multiple machines.

– It is often a complete file system

24

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 25: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

ROOT FILE SYSTEM • File system hierarchy

• /var

– Contains variable files

– File size varies with time

– Example: log files, tmp files

25

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 26: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX KERNEL • Core of Linux

• Manages hardware

• Distributes the resources

• Basic components of kernel

– Scheduler

– Interrupt handler

– Memory management

– System services

Network services & inter-process communication

26

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 27: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX KERNEL • User space

– All user level programs

– Limited privilege

• Kernel space

– Protected memory space

– Communicates with user space using system calls and virtual file systems

– Has complete access to the resources

• Advantage

Provide enhances security and stability

27

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 28: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX KERNEL

28 Linux kernel development ISBN-10: 0-672-32946-8

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 29: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX KERNEL

29

• Linux kernel source

– http://www.kernel.org/

• Linux kernel directory structure

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 30: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX KERNEL

30

• Distribution of Linux

– Debian

– SUSE Linux

– Gentoo

– Slackware

– Red Hat Enterprise Linux

– Fedore Project

– ……….

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 31: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

BOOTING THE LINUX KERNEL • System startup

• BIOS

– Power on self test

• Checks the hardware

– Search for bootable device

• CD-ROM

• Disk partition

• USB

• Bootable kernel file

– vmlinuz

31

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 32: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

BOOTING THE LINUX KERNEL

32

• Booting procedure in vmlinuz

– Basic hardware startup

– Basic environment setup

– Kernel decompression

• decompress_kernel()

– Memory management

• Page tables and paging

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 33: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

BOOTING THE LINUX KERNEL

33

• Start_kernel() performs

– initialization of procedure

– Setup interrupts

– Load of initial-RAM disk (initrd) in memory

– Initialization of first user space procedure(init)

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 34: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

BOOTING THE LINUX KERNEL

34

Initial-RAM disc (initrd.img)

– Initial root file system

– 1st step to load real file system

– Acts as a complete file system for embedded system

– Contains minimal set of directories and executable

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 35: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX UNDER QEMU

• Requirement to run Linux on QEMU

– Virtualized hardware

– Root file system (initrd.img)

– Linux kernel (bzimage of Linux kernel)

• Initialization of QEMU

35

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 36: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX UNDER QEMU

• BIOS in QEMU

– QEMU uses SeaBIOS

– SeaBIOS is a free BIOS implementation

– SeaBIOS can be used to boot emulator/hardware

• -kernel option allows to load bzImage of kernel

• -initrd option allows to load initial RAM disk

36

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 37: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX UNDER QEMU

• -append option specifies the device on which the file system is mounted

– ‘root=/dev/ram rw’

• ‘rw’ gives read/write permission

• Flags are added to support debuging

– Example: -s and –S

• -s: start servergdb

• -S: stops the emulator till project startup

37

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 38: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX KERNEL DEBUGGING

Procedure of linux debugging

– Setup kernel source code as Eclipse project

– Build the project

– Setup emulator to run virtualized machine

– Connect Eclipse with emulator

– Setup break points in kernel source

– Run debug configuration.

– Run debug to debug the linux kernel

38

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 39: EXPLORING LINUX KERNEL: THE EASY WAY! · PROBLEM Explore linux kernel TCP/IP stack •Solution –Try to understand relative kernel code –Available text –Run kernel in virtualized

LINUX KERNEL DEBUGGING

39

Video Demonstration

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010