Post on 11-Apr-2018
transcript
Building,RunningandMonitoringtheLinuxkernel
Prak6kumKernelProgrammingUniversityofHamburgScien6ficCompu6ng
Wintersemester2015/2016
Konstan6nosChasapisKonstan6nos.chasapis@informa6k.uni-hamburg.de
Outline
• TheLinuxkernelsourcetree• Configure,compile,installtheLinuxkernel• Linuxkernelbootandini6aliza6on• Monitoring
11/11/15 Kernel Build 1
WherecanIfindit
• DownloadfromhUp://www.kernel.org• Treenaviga6on
– Webbrowserviewlxr• hUp://lxr.sourceforge.com• hUp://lxr.free-electrons.com
– Symbolicdatabasefromallfiles,cscope• hUp://cscope.sourceforge.net
11/11/15 Kernel Build 2
Kernelsourcetree• arch
– containsallofthearchitecturespecifickernelcode.Ithasfurthersubdirectories,onepersupportedarchitecture,forexamplei386andalpha.
• block– containstheimplementa6onoftheblockI/Olayer
• crypto– implementscipheropera6onsandthecryptography
API
11/11/15 Kernel Build 3
Kernelsourcetree• Documenta2on
– kernelsourcedocumenta6on• drivers
– allofthesystem'sdevicedriversliveinthisdirectory.Theyarefurthersub-dividedintoclassesofdevicedriver,forexampleblock
• firmware– devicefirmwarethatisneededtousecertaindrivers
11/11/15 Kernel Build 4
Kernelsourcetree• fs
– filesystemcode.Thisisfurthersub-dividedintodirectories,onepersupportedfilesystem
• include– containsmostoftheincludefilesneededtobuildthe
kernelcode.Ittoohasfurthersubdirectoriesincludingoneforeveryarchitecturesupported
• init– containstheini6aliza6oncodeforthekernelanditisa
verygoodplacetostartlookingathowthekernelworks
11/11/15 Kernel Build 5
Kernelsourcetree• ipc
– containsthekernelsInter-ProcessCommunica6on(IPC)mechanismsuchasmessagequeues,semaphores,sharedmemory.
• kernel– coresubsystems,forexamplethescheduler.The
architecturespecifickernelcodeisinarch/*/kernel.• lib
– thisdirectorycontainsthekernel'slibrarycode.Thearchitecturespecificlibrarycodecanbefoundinarch/*
11/11/15 Kernel Build 6
Kernelsourcetree• mm
– containsallofthememorymanagementcode.Thearchitecturespecificmemorymanagementcodelivesdowninarch/*/mm/
• modules– directoryusedtoholdbuiltmodules
• net– thekernel'snetworkingcode,(ethernet,ipv4)
• samples– demonstra6vecode
11/11/15 Kernel Build 7
Kernelsourcetree• scripts
– containsthescripts(forexampleawkandtkscripts)thatareusedwhenthekernelisconfigured
• security– Linuxsecuritymodule,includingSELinux
• sound– AdvanceLinuxSoundArchitecture(ALSA),soundcard
drivers.
11/11/15 Kernel Build 8
Kernelsourcetree• usr
– user-spaceinterac6on(initramfs)• tools
– kernelanduserdevelopmenttools,mostlyusedforperformancecoun6ng
• virt– thevirtualiza6oninfrastructure
11/11/15 Kernel Build 9
Outline
• TheLinuxkernelsourcetree• Configure,compile,installtheLinuxkernel• Linuxkernelbootandini6aliza6on• Monitoring
11/11/15 Kernel Build 10
Configura6on• Typicalkernelhas>>1000configura6onop6ons• Defaultconfigura6onpartoftheboardsupportpackage
(BSP)• Configura6onfile.config• Configura6onop6onsaretypicallyBooleansorTristate
– Yes– No– Module
• Examples– CONFIG_SMP,enablesordisablesSMPsupports– CONFIG_LOCK_STAT,enablesordisableslocksta6s6cs
11/11/15 Kernel Build 11
Tweakconfigura6onusingmake,targets
• config– interac6veforeachop6on
• menuconfig– ncursestextmenu
• xconfig– graphicalmenususingQt
• gconfig– graphicalmenususingGtk+
• defconfig– defaultconfigura6onbasedonthearchitecture
11/11/15 Kernel Build 12
Tweakconfigura6onusingmake,targets
• oldconfig– validateandupdatetheconfigura6on
• randconfig– randomanswertoallop6ons
• allmodconfig– selec6ngmoduleswhenpossible
• allyesconfig– allop6onsareacceptedwithyes
• allnoconfig– allop6onsareansweredwithno
11/11/15 Kernel Build 13
NamingthenewKernel
• EditthetoplevelMakefile• VERSION=3
• PATCHLEVEL=6
• SUBLEVEL=35
• EXTRAVERSION=-rc2
• NAME=mykernel
11/11/15 Kernel Build 14
Compile
• Requiredpackages– developmenttools,make,gcc,gzip,etc.– Severaldistribu6onsofferpackages
• Crosscompile– exportARCH=...– exportCROSS_COMPILE=...
11/11/15 Kernel Build 15
Compilemaketargets• default
– buildskernel+modules• bzImage
– buildskernel– generates:arch/arm/boot/bzImage
• modules– buildsloadablemodules– generates:lib/modules/<kernel.versio-name>
11/11/15 Kernel Build 16
Compilemaketargets• -j<n>e.g.-j2
– spawnmul6plebuildjobs• clean
– generatedfiles• mrproper
– generatedfiles+config+backupfiles• distclean
– alltheabove+patchfiles
11/11/15 Kernel Build 17
Install
• makeinstall– copykernelimagetotheproperdirectory/
boot• makemodule_install
– installbuildmodulesinthecorrecthomeunder/lib/modules
• Updatethebootloader– LILOorgrubconfigura6onfile– addthenewentryforthenewlybuildkernel
11/11/15 Kernel Build 18
Kernelcommandline• Kernelbehavioursetbyboot“commandline”• seeDocumenta6on/kernel-parameters.txt• Examples
– root:setdevicetoloadrootfilesystemfrom,• e.g.root=/dev/sda1
– quiet:outputfewerconsolemessages– debug:outputallconsolemessages– maxcpus:controltheac6veCPU
• CanbesetinBootloader,e.g.GRUB
11/11/15 Kernel Build 19
Therootfilesystem• Mountedbythekernelduringboot
– Providesaddi6onalkernelmodulesthatareneeded• specifiedtherootkernelcommandlineparameter• Loadedfrom:
– memory(ramdisk/initramfs)– storagedevice– network
• The“module”thatprovidesaccessmustbeembeddedinthekerneloritcannotmount..
11/11/15 Kernel Build 20
Initramfs• ini6alramfilesystem
– successorofinitrd• cpioarchiveoftheini6alfilesystem
– cpio• filearchiveandfileformat• copyinandout
• getsloadedintomemoryduringstartup• containsdevicedriversandtoolsneededto
mounttherealfilesystem11/11/15 Kernel Build 21
Outline
• TheLinuxkernelsourcetree• Configure,compile,installtheLinuxkernel• Linuxkernelbootandini6aliza6on• Monitoring
11/11/15 Kernel Build 22
Bootprocessofthekernel• BIOSloadsMasterBootRecord(MBR)fromtheboot
device• CodethatexistintheMBRreadsthepar66ontableof
thebootdeviceandreadsthebootloader(GRUB,LILO)fromthebootpar66on
• Thebootloaderreadsthecompressedkernelimage– Passesthecontroltoitusingthecommandline
op6ons• Thekernelun-compressesitself
11/11/15 Kernel Build 23
Bootprocessofthekernel• Proceedsto"real"modewherethefirstlevel
ini6aliza6onsaredone– Inreadmode,itcanaccessonlythefirst1MBof
memory• Startupisperformedinthe"protected"modeand
beginsini6alizingtheCPUsubsystem– In"protected"modeyoucanusemanyadvance
featureoftheprocessorsuchaspaging• Itfollowsthememoryandtheprocessmanagements
subsystems• Peripheralbuses,I/Obusesarestatednext
11/11/15 Kernel Build 24
Initprocess• Atlastthekernelinvokestheinitprogramthatisthe
parentofallLinuxprocesses• Firstprogramtoberun/sbin/init
– Beginsbyreading/etc/iniUab• Runlevels(systemstates)forSystemVinit
– 0ishalt– 1issingleuser– 2-5aremul6-user– 6reboot
• Startsini6alisa6onscripts– Fountat/ect/init.d
11/11/15 Kernel Build 25
Outline
• TheLinuxkernelsourcetree• Configure,compile,installtheLinuxkernel• Linuxkernelbootandini6aliza6on• Monitoring
– Metrics– Tools
11/11/15 Kernel Build 26
CPUmetrics/proc/stat• U2liza2on
– overallu6liza6onperprocessor• User2me
– percentagespentonuserprocesses• System2me
– percentagespentonkernelopera6ons• Wai2ng
– 6mespentwai6ngforI/Oopera6ons• Idle2me
– systemwasidlewai6ngtasks
11/11/15 Kernel Build 27
CPUmetrics/proc/stat• Nice2me
– 6mespentonre-nicingprocesses• Runnableprocesses
– processesreadytorun• Blocked
– processesblockedbyI/Oopera6ons• Contextswitch
– numberofcontextswitches• Interrupts
– numberofhardandsorinterrupts
11/11/15 Kernel Build 28
Memorymetrics• Freememory
– amountoffreememoryinthesystem• Swapusage
– amountofswapused• Bufferandcache
– memoryallocatedforI/O• Slabs
– kernelusageofmemory• Ac2veVS.inac2vememory
– Inac6vememoryisacandidatetobeswapped
11/11/15 Kernel Build 29
Networkinterfacemetrics• Packetssent/received• Bytessent/received• Collisionspersecond
– SustainedvaluesindicatenetworkinfrastructureboUlenecks• Packetsdropped
– Canbecausedbythefirewallorlimitedbuffers• Overruns
– howmany6mesrunnedoutofbuffers• Errors
– countthepacketsthataremarkedfaulty
11/11/15 Kernel Build 30
Blockdevicemetrics• Iowait
– the6meCPUspendswai6ngforI/Otocomplete• Averagequeuelength
– amountofoutstandingI/Orequests– highvalueindicateI/OboUleneck
• Averagewait– average6meinmsthattakesforanI/Oopera6onto
complete• Transferpersecond
– howmanyI/Oopera6onsareperformed
11/11/15 Kernel Build 31
Blockdevicemetrics• Blocksread/writepersecond
– numberofblocksthatwerereadorwriUen(usuallyeachblockis1024Bytes)
• Kilobytesread/writepersecond– numberofblocksthatwerereadorwriUeninKBytes
11/11/15 Kernel Build 32
Genericadmintools
• dmesg– Printsthemessagebufferofthekernel
• strace– Monitorinterac6onbetweenuserandkernel
• oprofile– System-widesta6s6calprofilingtool
11/11/15 Kernel Build 33
CPUTools• top
– Processac6vity• ps,pstree
– Displaytherunningprocesses• kill
– SendstheSIGTERMsignaltotheprocess• mpstat
– Displaysac6vi6esforeachavailableprocessor• numastat
– NUMA-relatedsta6s6cs• pmap
– Processmemoryusage
11/11/15 Kernel Build 34
I/OTools• vmstat
– Reportvirtualmemorysta6s6cs• free
– Displaytheamountoffreeandusedmemory• iostat
– Reportblockdevicesta6s6cs• lsblk
– Listblockdevices• lsof
– Listopenfiles11/11/15 Kernel Build 35
NetworkTools
• ping– checkifaserverresponds
• traceroute– displaytheroutepath
• nslookup– getdomainnameorIPaddress
• netstat– displaysnetworkstats
• tcpdump– dumptrafficonanetwork
11/11/15 Kernel Build 36
Procfiles
• /proc/stat– cpu
• /proc/diskstats– disk
• /proc/meminfo– Memorystats
11/11/15 Kernel Build 37