You are on page 1of 4

06/11/13

KernelBuild - Linux Kernel Newbies

PginanoEditvel
Informaes
Anexos

MaisAes:

KernelBuild
GuidetobuildingtheLinuxkernel.

WheredoIfindthekernel?
ThelatestsourcecodefortheLinuxkerneliskeptonkernel.org.Youcaneitherdownloadthefullsourcecodeasatarball(notrecommendedandwill
takeforevertodownload),oryoucancheckoutthecodefromthereadonlygitrepositories.

WhattoolsdoIneed?
TobuildtheLinuxkernelfromsource,youneedseveraltools:git,make,gcc,and(optionally)ctagsand/orncursesdev.Thetoolpackagesmaybecalled
somethingelseinyourLinuxdistribution,soyoumayneedtosearchforthepackage.Thencursesdevtoolsareusedifyou"makemenuconfig"or"make
nconfig".
OnUbuntu,youcangetthesetoolsbyrunning:

sudoaptgetinstalllibncurses5devgccmakegitexuberantctags
OnRedHatbasedsystemslikeFedora,ScientificLinux,andCentOSyoucanrun:

sudoyuminstallgccmakegitctagsncursesdevel

Whichkerneltobuild?
Ifyouwanttotesttoseeifabugisfixedthentestagainstthelateststablekernelfromkernel.org.Ifyouarebraveandyoursystemisbackedupthelatest
releasecandidatefromLinus'streeisagreattarget.Sometimesthemaintainermaywantyoutouseanexperimentalbranchfromtheirowngittree.You
shouldusethegitURLtheygaveyouinsteadofthegitURLsbelow.
Ifyou'redoingdevelopmentforanewfeature,ortryingtotestabugfix,youshoulduseLinus'tree,orthesubsystemmaintainer'snexttree.Most
subsystemmaintainerskeeptheirgittreesonkernel.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.1isavailable,usethat
insteadofv3.1.ThekerneltagsthatendwithrcXarereleasecandidatekernels,notstablekernels.
Nowcheckoutthecodeassociatedwiththatkernelwiththecommand

gitcheckoutbstabletag
WheretagisthelatestvX.Y.Ztagyoufound.

Downloadingthelatestrctree
CheckoutLinus'tree:

kernelnewbies.org/KernelBuild

1/4

06/11/13

KernelBuild - Linux Kernel Newbies

CheckoutLinus'tree:

gitclonegit://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cdlinux

Settingupyourkernelconfiguration
Manykerneldriverscanbeturnedonoroff,orbuiltasmodules.The.configfileinthekernelsourcedirectorydetermineswhichdriversarebuilt.When
youdownloadthesourcetree,itdoesn'tcomewitha.configfile.Youhaveseveraloptionsongeneratinga.configfile.Theeasiestistoduplicateyour
currentconfig.

Duplicatingyourcurrentconfig
Ifyou'retryingtoseeifabugisfixed,youprobablywanttoduplicatetheconfigurationonyourrunningkernel.Thatconfigfileisstoredsomewherein
/boot/.Theremightbeseveralfilesthatstartwithconfig,soyouwanttheoneassociatedwithyourrunningkernel.Youcanfindthatbyrunninguname -a
andfindingtheconfigfilethatendswithyourkernelversionnumber.Copythatfileintothesourcedirectoryas.config.Orjustrunthiscommand:

cp/boot/config`unamer`*.config

Makingthedefaultconfig
Remember,adefaultconfigmaynothavetheoptionsyouarecurrentlyusing.Run

makedefconfig

Makingaminimalconfig
Compilingakernelfromscratchfromadistributionconfigurationcantake"forever"becausethedistrosturnoneveryhardwareconfigurationpossible.
Forpeoplewantingtodokerneldevelopmentfast,youwanttomakeaminimalconfiguration.SteveRostedtusesktest.plmake_min_configtogetatruely
minimumconfig,butitwilltakeadayortwotobuild.Warning:makesureyouhaveallyourUSBdevicespluggedintothesystem,oryouwon'tgetthe
driversforthem!

Changingyourconfig
Ifyouneedtomakeanychangestoyourconfiguration,youcanrunoneofthefollowingcommands.Theserequirethecursestoolstobeinstalled.

makemenuconfig
or

makenconfig

Buildingthekernel
Run

make
Or,ifyouhaveamulticoreprocessor,run

makejX
WhereXisanumberlike2or4.Ifyouhaveadualcore,2or3mightbegood.Quadcore,4or6.Donotrunwithreallybignumbersunlessyouwant
yourmachinetobedogslow!
Walkaway,getsomecoffee,lunch,orgoreadsomecomics.

Installingthekernel
Toinstallakernel,youwillneedtoeithermanuallyupdateyourGRUBconfigurationfile,orhaveaninstallkernelscript.Thisscriptinstallsthekernelto
/boot/,installsmodulesto/lib/modules/X.Y.Z/(whereX.Y.Zissomethinglike3.1.5),andupdatesfile/boot/grub/grub.conf.Fortunately,Ubuntu
providesaninstallkernelscriptin/sbin/installkernel.ThegrubbyRPMprovidesitforRPMbasedsystems.
Ifyouhaveaninstallkernelscript,youcanjustrun

kernelnewbies.org/KernelBuild

2/4

06/11/13

KernelBuild - Linux Kernel Newbies

Ifyouhaveaninstallkernelscript,youcanjustrun

sudomakemodules_installinstall
Orifyoudon'thavesudoinstalled,run

suc"makemodules_installinstall"

Runningyourkernel
First,makesureyouknowhowtoselectakernelatboottime.Ifyournewkernelisbroken,youwantawaytobootintoyouroldkernel.Thegrub
bootloaderusuallypresentsuserswithachoiceofkernelsandyoucanrebootintoaknowngoodkernelifyournewcompiledoesn'twork.Somedistros
useadefaultgrubconfigthathidesthatmenu.YoucanusuallygetthemenutoappearbymashingtheESCkeyduringbootaftertheBIOSdisplay
disappears.
Ubuntu:TomakethegrubmenualwaysappearonbootunderUbuntu,removetheGRUB_HIDDEN_TIMEOUT_QUIETlinefrom/etc/default/grub.
YoumaywanttoincreasetheGRUB_DEFAULTtimeoutfrom0to15secondsormore.Afteryou'vefinishededitingthegrubfileyoumayneedto
updateyourgrubfile.

sudoupdategrub2
Youwill(usually)needtorebootintoyournewkernel.

Patchingyourkernel
Thereareseveralwaystoapplyapatchtoyourkernel.Usuallythemaintainerwillsendyouapatchasattachment,orinlineinthemail.Youshouldeither
savethefile,orcopyandpastethepatchintoanewfile.
Toapplythepatch,gotothebasekerneldirectoryandrun

gitampatchfile
Wherepatchfileisthefileyousaved.Ifpatchfails,youcanrun:

gitamabort
gitresethardHEAD
gitam3patchfile
Thisappliesthepatch,andattemptstorunathreewaymergeifthepatchapplicationfails.Ifallelsefails,youcanattempttoduplicatethepatchchanges
byhand.
Thenyouneedtorebuildyourkernelandreboot.
(Note:theolderwayofmanuallypatchingthekernelwithpatch -p1 <patchfiledoesnotcreateanygithistory,whichmakesithardtorevertandretry
differentpatches.Youwilloftenhavetogothroughseveralpatcheswithamaintainertofindtherightfixforabug,sohavingthegithistoryisuseful.)

Revertingapatch
Ifamaintainerwantsyoutorevertapatchyouhaveapplied,andtryadifferentpatch,youcanusegittoresetthehistorytothepointbeforethepatchwas
applied.
Ifgit logshowsthepatchtoberemovedisthefirstlogentry,youcanrun

gitresethardHEAD^
Ifyouneedtorevertseveralpatches,youcanusegitlogtofindthecommitIDofthefirstcommitbeforethosepatches.Forinstance,sayyouhaveapplied
twopatchestothestabletree3.4.17,andyouwanttorevertthosepatches.git logwilllooklikethis:

$gitlogpretty=onelineabbrevcommit
8901234Testingpatch2
1234567Testingpatch1
5390967Linux3.4.17
1f94bd4drm/i915:nolvdsquirkforZotacZDBOXSDID12/ID13
0187c24x86,mm:Usememblockmemoryloopinsteadofe820_RAM
a0419castaging:comedi:amplc_pc236:fixinvalidregisteraccessduringdetach

kernelnewbies.org/KernelBuild

3/4

06/11/13

KernelBuild - Linux Kernel Newbies

Toresetyourtreeto3.4.17,youcanrun:

gitresethard5390967
Ifyoulookatthecommitswithgitkyouwillnoticethatthe3.4.17commitisalsotaggedasv3.4.17.Youcanresetbytagaswell:

gitresethardv3.4.17

TipsandTricks
Ifyouhaveadriverinstalledasamodule,youcanrecompilejustthatdriver.Thissavestime,becausethekernelbuildsystemdoesn'thavetolookfor
changesacrosstheentirekerneltreeorcompileanyofthebuiltincode.
Allmodulefilesendwith.ko.Thekernelmakesystemwillcompilejustone.kofileifyougiveitthefullpathtothefile:

makedrivers/usb/host/xhcihcd.ko
Noteyouwillneedtobeinthebasekernelsourcedirectoryforthistowork.Youcan'tmakefromadifferentdirectory.
Youcanalsoreloaddriverswithoutrebootingyourkernel.Forexample,IcanremovethexHCIdriverandreloaditwith

sudormmodxhcihcd&&sudoinsmoddrivers/usb/host/xhcihcd.ko
Makesurethatyouunderstandtheconsequencesofunloadingyourdriver!Forinstance,ifyouunloadtheUSBcoredriverinordertotryoutchanges,
yourUSBmouseandkeyboardaren'tgoingtoworkuntiltheUSBcoredriverisreloaded.
Youmayhavetounloadotherdriversthatdependonyourdriverbeforeyoucanreloadit.Uselsmodtofindwhichdriversthatareloadeddependonyour
driver.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.Iwouldhavetounloadallthosedrivers
inordertoreloadtheusbcoremodule.

More
linux/README
LinuxKernelinaNutshellbyGregKroahHartman,http://www.kroah.com/lkn/,online:html
KernelRebuildGuide,KwanLowe,2004
inDebian
Debianandthekernel,FAQ
TheLinuxkernelunderDebian,Reference
DebianLinuxKernelHandbook
Fedora'sCustomKernelBuildpage
Tellothersaboutthispage:

SERV I DOR

CLOUD

COM PUT I N G

A Melhor e Mais Rpida Nuvem do Brasil. Suporte 24x7. 25 Mbps Link

kernelnewbies.org/KernelBuild

4/4

You might also like