EXPLORING LINUX KERNEL: THE EASY WAY! By: Ahmed Bilal Numan 1
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
Virtualization What is QEMU Linux IN THIS PRESENTATION Root File System Linux kernel Linux under QEMU Debugging Linux kernel How to do that 3
VIRTUALIZATION Separation of resources from underlying physical resources Examples Memory virtualization Partitioning the hard disk Server virtualization Hardware virtualization 4
VIRTUALIZATION Perceived from "Virtualization Overview" VMWARE white paper 5
WHY VIRTUALIZATION Resource utility optimization Testing and Development Exploration Dynamic load balancing Etc. 6
WHY VIRTUALIZATION Resource utility optimization Testing and Development Exploration Dynamic load balancing Etc. 7
HOW TO GET VIRTUALIZATION Xen Baremetal hypervisor Open source virtualization VMWare A number of virtualization products QEMU VMWare player Ware workstation 8
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
Networking features QEMU 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
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
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
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
File system hierarchy ROOT FILE SYSTEM /bin Contains binary executables Common linux commands used by all users Example: ls, ping, grep etc. 14
File system hierarchy ROOT FILE SYSTEM /boot Contains boot loader related files File system image file, grub etc. 15
File system hierarchy ROOT FILE SYSTEM /dev Contains device files It include all the devices attached to the system /dev/ram0, /dev/sda etc. 16
File system hierarchy ROOT FILE SYSTEM /home Contains directories of all the users registered to the system Two directories in this folder may not accessible to each other 17
File system hierarchy ROOT FILE SYSTEM /lib Contains libraries of binaries located in /bin or /sbin Example: libc.so.6, libasound.so.2 18
File system hierarchy ROOT FILE SYSTEM /media Contains directories for temporarily mounted removable devices Example: when USB is attached it is mounted to this directory 19
File system hierarchy LINUX FILE SYSTEM /etc Contains configuration files for all the programs Contains startup and shutdown scripts for programs 20
File system hierarchy LINUX FILE SYSTEM /sbin Named after system binaries Contains executable binaries especially for system administration Example: ifconfig, reboot, route 21
File system hierarchy ROOT FILE SYSTEM /sys It is utilized by sysfs Contains information about plugin devices such as USB 22
File system hierarchy ROOT FILE SYSTEM /tmp Contains temporary files generated by system and user File in this directory are deleted at reboot 23
File system hierarchy ROOT FILE SYSTEM /usr Contains files that can be shared across multiple machines. It is often a complete file system 24
File system hierarchy ROOT FILE SYSTEM /var Contains variable files File size varies with time Example: log files, tmp files 25
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
User space All user level programs Limited privilege Kernel space Protected memory space LINUX KERNEL Communicates with user space using system calls and virtual file systems Has complete access to the resources Advantage Provide enhances security and stability 27
LINUX KERNEL Linux kernel development ISBN-10: 0-672-32946-8 28
Linux kernel source http://www.kernel.org/ LINUX KERNEL Linux kernel directory structure 29
LINUX KERNEL Distribution of Linux Debian SUSE Linux Gentoo Slackware Red Hat Enterprise Linux Fedore Project. 30
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
BOOTING THE LINUX KERNEL Booting procedure in vmlinuz Basic hardware startup Basic environment setup Kernel decompression decompress_kernel() Memory management Page tables and paging 32
BOOTING THE LINUX KERNEL Start_kernel() performs initialization of procedure Setup interrupts Load of initial-ram disk (initrd) in memory Initialization of first user space procedure(init) 33
BOOTING THE LINUX KERNEL Initial-RAM disc (initrd.img) Initial root file system 1 st step to load real file system Acts as a complete file system for embedded system Contains minimal set of directories and executable 34
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
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
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
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
LINUX KERNEL DEBUGGING Video Demonstration 39