100% found this document useful (1 vote)
147 views

KernelBuild - Linux Kernel Newbies

The document provides a guide to building the Linux kernel from source. It outlines the tools needed such as git, make, gcc and libraries. It describes how to download the latest stable or release candidate kernel source code, set up the kernel configuration, build and install the kernel, and test the new kernel. The guide also covers patching kernels and tips for faster builds.

Uploaded by

myhomenet1191881
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
147 views

KernelBuild - Linux Kernel Newbies

The document provides a guide to building the Linux kernel from source. It outlines the tools needed such as git, make, gcc and libraries. It describes how to download the latest stable or release candidate kernel source code, set up the kernel configuration, build and install the kernel, and test the new kernel. The guide also covers patching kernels and tips for faster builds.

Uploaded by

myhomenet1191881
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

09/08/2016

KernelBuild - Linux Kernel Newbies

KernelBuild
GuidetobuildingtheLinuxkernel.

WheredoIfindthekernel?
ThelatestsourcecodefortheLinuxkerneliskeptonkernel.org.Youcaneitherdownloadthefullsourcecodeasatarball(not
recommendedandwilltakeforevertodownload),oryoucancheckoutthecodefromthereadonlygitrepositories.

WhattoolsdoIneed?
TobuildtheLinuxkernelfromsource,youneedseveraltools:git,make,gcc,libssldevand(optionally)ctags,cscope,and/or
ncursesdev.ThetoolpackagesmaybecalledsomethingelseinyourLinuxdistribution,soyoumayneedtosearchforthepackage.
Thencursesdevtoolsareusedifyou"makemenuconfig"or"makenconfig".
OnUbuntu,youcangetthesetoolsbyrunning:
sudoaptgetinstalllibncurses5devgccmakegitexuberantctagsbclibssldev
OnRedHatbasedsystemslikeFedora,ScientificLinux,andCentOSyoucanrun:
sudoyuminstallgccmakegitctagsncursesdevelopenssldevel
AndonSUSEbasedsystems(likeSLESandLeap),youcanrun:
sudozypperingitgccncursesdevellibopenssldevelctagscscope

Whichkerneltobuild?
Ifyouwanttotesttoseeifabugisfixedthentestagainstthelateststablekernelfromkernel.org.Ifyouarebraveandyoursystemis
backedupthelatestreleasecandidatefromLinus'streeisagreattarget.Sometimesthemaintainermaywantyoutousean
experimentalbranchfromtheirowngittree.YoushouldusethegitURLtheygaveyouinsteadofthegitURLsbelow.
Ifyou'redoingdevelopmentforanewfeature,ortryingtotestabugfix,youshoulduseLinus'tree,orthesubsystemmaintainer's
nexttree.Mostsubsystemmaintainerskeeptheirgittreesonkernel.org.Whenindoubt,useLinus'tree.
Ifyoudon'tunderstandwhatastableorreleasecandidatekernelis,youshouldreadtheKernelDevProcesspage.

Downloadingthelateststabletree
First,checkoutthestablekernelgitrepository:
gitclonegit://git.kernel.org/pub/scm/linux/kernel/git/stable/linuxstable.git
cdlinuxstable
Next,findthelateststablekerneltagbyrunning
gittagl|less
FindthelateststablekernelbylookingforthelargestvX.Y.Zvalues.Forexample,usethev3.1tagoverthev3.0.46tag.Ifv3.1.1is
available,usethatinsteadofv3.1.ThekerneltagsthatendwithrcXarereleasecandidatekernels,notstablekernels.
https://kernelnewbies.org/KernelBuild

1/5

09/08/2016

KernelBuild - Linux Kernel Newbies

Nowcheckoutthecodeassociatedwiththatkernelwiththecommand
gitcheckoutbstabletag
WheretagisthelatestvX.Y.Ztagyoufound.

Downloadingthelatestrctree
CheckoutLinus'tree:
gitclonegit://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cdlinux

Settingupyourkernelconfiguration
Manykerneldriverscanbeturnedonoroff,orbuiltasmodules.The.configfileinthekernelsourcedirectorydetermineswhich
driversarebuilt.Whenyoudownloadthesourcetree,itdoesn'tcomewitha.configfile.Youhaveseveraloptionsongeneratinga
.configfile.Theeasiestistoduplicateyourcurrentconfig.

Duplicatingyourcurrentconfig
Ifyou'retryingtoseeifabugisfixed,youprobablywanttoduplicatetheconfigurationonyourrunningkernel.Thatconfigfileis
storedsomewherein/boot/.Theremightbeseveralfilesthatstartwithconfig,soyouwanttheoneassociatedwithyourrunning
kernel.Youcanfindthatbyrunninguname-aandfindingtheconfigfilethatendswithyourkernelversionnumber.Copythatfile
intothesourcedirectoryas.config.Orjustrunthiscommand:
cp/boot/config`unamer`*.config

Makingthedefaultconfig
Remember,adefaultconfigmaynothavetheoptionsyouarecurrentlyusing.Run
makedefconfig

Makingaminimalconfig
Compilingakernelfromscratchfromadistributionconfigurationcantake"forever"becausethedistrosturnoneveryhardware
configurationpossible.Forpeoplewantingtodokerneldevelopmentfast,youwanttomakeaminimalconfiguration.SteveRostedt
usesktest.plmake_min_configtogetatruelyminimumconfig,butitwilltakeadayortwotobuild.Warning:makesureyouhaveall
yourUSBdevicespluggedintothesystem,oryouwon'tgetthedriversforthem!

Changingyourconfig
Ifyouneedtomakeanychangestoyourconfiguration,youcanrunoneofthefollowingcommands.Theserequirethecursestoolsto
beinstalled.
makemenuconfig
or
makenconfig
https://kernelnewbies.org/KernelBuild

2/5

09/08/2016

KernelBuild - Linux Kernel Newbies

Buildingthekernel
Run
make
Or,ifyouhaveamulticoreprocessor,run
makejX
WhereXisanumberlike2or4.Ifyouhaveadualcore,2or3mightbegood.Quadcore,4or6.Donotrunwithreallybignumbers
unlessyouwantyourmachinetobedogslow!
Walkaway,getsomecoffee,lunch,orgoreadsomecomics.

Installingthekernel
Toinstallakernel,youwillneedtoeithermanuallyupdateyourGRUBconfigurationfile,orhaveaninstallkernelscript.Thisscript
installsthekernelto/boot/,installsmodulesto/lib/modules/X.Y.Z/(whereX.Y.Zissomethinglike3.1.5),andupdatesfile
/boot/grub/grub.conf.Fortunately,Ubuntuprovidesaninstallkernelscriptin/sbin/installkernel.ThegrubbyRPMprovidesitforRPM
basedsystems.
Ifyouhaveaninstallkernelscript,youcanjustrun
sudomakemodules_installinstall
Orifyoudon'thavesudoinstalled,run
suc"makemodules_installinstall"

Runningyourkernel
First,makesureyouknowhowtoselectakernelatboottime.Ifyournewkernelisbroken,youwantawaytobootintoyourold
kernel.Thegrubbootloaderusuallypresentsuserswithachoiceofkernelsandyoucanrebootintoaknowngoodkernelifyournew
compiledoesn'twork.Somedistrosuseadefaultgrubconfigthathidesthatmenu.Youcanusuallygetthemenutoappearby
mashingtheESCkeyduringbootaftertheBIOSdisplaydisappears.
Ubuntu:TomakethegrubmenualwaysappearonbootunderUbuntu,removetheGRUB_HIDDEN_TIMEOUT_QUIETlinefrom
/etc/default/grub.YoumaywanttoincreasetheGRUB_DEFAULTtimeoutfrom0to15secondsormore.Afteryou'vefinishedediting
thegrubfileyoumayneedtoupdateyourgrubfile.
sudoupdategrub2
Youwill(usually)needtorebootintoyournewkernel.

Patchingyourkernel
Thereareseveralwaystoapplyapatchtoyourkernel.Usuallythemaintainerwillsendyouapatchasattachment,orinlineinthe
mail.Youshouldeithersavethefile,orcopyandpastethepatchintoanewfile.
Toapplythepatch,gotothebasekerneldirectoryandrun
gitampatchfile
Wherepatchfileisthefileyousaved.Ifpatchfails,youcanrun:

https://kernelnewbies.org/KernelBuild

3/5

09/08/2016

KernelBuild - Linux Kernel Newbies

gitamabort
gitresethardHEAD
gitam3patchfile
Thisappliesthepatch,andattemptstorunathreewaymergeifthepatchapplicationfails.Ifallelsefails,youcanattemptto
duplicatethepatchchangesbyhand.
Thenyouneedtorebuildyourkernelandreboot.
(Note:theolderwayofmanuallypatchingthekernelwithpatch-p1<patchfiledoesnotcreateanygithistory,which
makesithardtorevertandretrydifferentpatches.Youwilloftenhavetogothroughseveralpatcheswithamaintainertofindtheright
fixforabug,sohavingthegithistoryisuseful.)

Revertingapatch
Ifamaintainerwantsyoutorevertapatchyouhaveapplied,andtryadifferentpatch,youcanusegittoresetthehistorytothepoint
beforethepatchwasapplied.
Ifgitlogshowsthepatchtoberemovedisthefirstlogentry,youcanrun
gitresethardHEAD^
Ifyouneedtorevertseveralpatches,youcanusegitlogtofindthecommitIDofthefirstcommitbeforethosepatches.Forinstance,
sayyouhaveappliedtwopatchestothestabletree3.4.17,andyouwanttorevertthosepatches.gitlogwilllooklikethis:
$gitlogpretty=onelineabbrevcommit
8901234Testingpatch2
1234567Testingpatch1
5390967Linux3.4.17
1f94bd4drm/i915:nolvdsquirkforZotacZDBOXSDID12/ID13
0187c24x86,mm:Usememblockmemoryloopinsteadofe820_RAM
a0419castaging:comedi:amplc_pc236:fixinvalidregisteraccessduringdetach
Toresetyourtreeto3.4.17,youcanrun:
gitresethard5390967
Ifyoulookatthecommitswithgitkyouwillnoticethatthe3.4.17commitisalsotaggedasv3.4.17.Youcanresetbytagaswell:
gitresethardv3.4.17

TipsandTricks
Ifyouhaveadriverinstalledasamodule,youcanrecompilejustthatdriver.Thissavestime,becausethekernelbuildsystemdoesn't
havetolookforchangesacrosstheentirekerneltreeorcompileanyofthebuiltincode.
Allmodulefilesendwith.ko.Thekernelmakesystemwillcompilejustone.kofileifyougiveitthefullpathtothefile:
makedrivers/usb/host/xhcihcd.ko
Noteyouwillneedtobeinthebasekernelsourcedirectoryforthistowork.Youcan'tmakefromadifferentdirectory.
Youcanalsoreloaddriverswithoutrebootingyourkernel.Forexample,IcanremovethexHCIdriverandreloaditwith
sudormmodxhcihcd&&sudoinsmoddrivers/usb/host/xhcihcd.ko
Makesurethatyouunderstandtheconsequencesofunloadingyourdriver!Forinstance,ifyouunloadtheUSBcoredriverinorderto
tryoutchanges,yourUSBmouseandkeyboardaren'tgoingtoworkuntiltheUSBcoredriverisreloaded.
https://kernelnewbies.org/KernelBuild

4/5

09/08/2016

KernelBuild - Linux Kernel Newbies

Youmayhavetounloadotherdriversthatdependonyourdriverbeforeyoucanreloadit.Uselsmodtofindwhichdriversthatare
loadeddependonyourdriver.E.g.
$lsmod|grepusb
usbnet265962rndis_host,cdc_ether
mii51981usbnet
btusb165750
usbhid446211hid_logitech
usbcore1910789
xhci_hcd,rndis_host,cdc_ether,usbnet,btusb,uvcvideo,usbhid,ehci_hcd
usb_common10931usbcore
Inthiscase,usbcoreisusedbyxhci_hcd,rndis_host,cdc_ether,usbnet,btusb,uvcvideo,usbhid,andehci_hcd.Iwouldhaveto
unloadallthosedriversinordertoreloadtheusbcoremodule.

https://kernelnewbies.org/KernelBuild

5/5

You might also like