LinuxDevices.com-theembeddedLinuxportal:Articles>View>PrinterFriendly Page1of8

LinuxDevices.comprinter-friendlystorydisplayof: http://www.linuxdevices.com/articles/AT5974781081.html... ELJonline: BOEL,Part2:KernelConfigurationand

BrianElliottFinley Brian'sOwnEmbedded,orBOELasitisaffectionatelyknown,wascreatedaspartofthe SystemImagerProject.SystemImagerisatoolusedfor,well,imagingsystemsanddistributinglive updates.Partoftheimagingprocessinvolvesbootingaclientmachineinsuchawaythatitshard disksarenotinuseandarethereforeavailableformanipulation.TheSystemImagerautoinstallclient doesthisbyusingarootfilesystemthatexistsentirelyinmemoryoraninitrd(initialRAMdisk).This initrdandthekernelthatgoeswithitaretheembeddedLinuxsystemwecallBOEL. Thisisthesecondarticleofatwo-partseries.Inthefirstarticle,wewentthroughtheprocessof creatingourinitrdanddiscussedissuessuchaslibraries,shells,filesystemsandinodes.Muchofour focusisoncreatingasystemassmallaspossible--thecompleteBOELsystemhastofitonasingle 1.44MBfloppy.Inthisarticlewegothroughtheprocessoftyingakerneltoourinitrdandmakingthe systembootablefromavarietyofmedia.

Figure1.UncompressedRAMDiskComponents(inKilobytes) CompilingtheKernel Onenicethingaboutcompilingakernelforanembeddedsystemisthatyoutypicallyhaveavery limitedsetofhardwaretosupport.InthecaseofBOEL,theonlytypesofhardwarewehaveto supportareNICs(NetworkInterfaceCards),diskdrivesanddiskdrivecontrollers.Thismakesit mucheasiertoparedownthedriversneededforourkernel.Italsoallowsustouseanon-modular kernel,whichinadditiontosimplifyingthebootprocess,alsosimplifiesthedevelopmentprocess. Evenassuch,itcanstilltakealotoftimetowhittledownakerneltoexactlywhatisneededand nothingmore.Butit'sworthmakingthateffort.Everyadditionaldrivertakesupvaluablespaceon oursystem,soweonlywanttoincludethedriversthatweneed. ThismeanswecanleaveoutthingslikeUSB,sound,Hamradiosupport,etc.However,wedohaveto besuretoincludesupportforthingslikeaninitrd(wehavetobeabletomountthethingforourroot filesystem),loopbackdevices(theinitrdgetsputonaloopbackdevicebeforethekernelmountsit), NICs,TCP/IP,IDE,SCSIandthefilesystemsthatwesupport.Ifyouwanttohavealookatexactly whatweincludeinourstandardkernel,youcantakealookatthiskernel.configfile.

http://www.linuxdevices.com/cgi-bin/printerfriendly.cgi?id=AT5974781081 5/10/2002 LinuxDevices.com-theembeddedLinuxportal:Articles>View>PrinterFriendly Page2of8

Onceyou'vemadeyourdriverselections,youmustcompileyourkernel.Funnythingis,eachtime youcompileit,yourresultantkernelmayhaveaslightlydifferentsize.Iusuallywilldoseveral compilesuntilI'mconvincedI'vegottenthesmallestonepossible.

Figure2.KernelSizes Nowthatwehaveourkernelreadyandourinitrdaspreparedinthefirstarticle,weneedtoconfigure thesystemforbooting.InthecaseofBOEL,weneedtobeabletobootfromfloppy,CD-ROM,a harddriveorthenetwork.I'lldescribehowwemakeeachofthesehappenusingthesamekerneland initrd. BootingtheSystem First,abitaboutbootingingeneral.Whenbootingasystemthatusesaninitrd,theactualstepsthe computergoesthrougharenotnecessarilyintuitive.OnaLinuxsystemthathasitsrootfilesystemon aharddiskpartition(withnoinitrd),thebootloadersimplyloadsthekernelandtellsitwheretofind itsrootfilesystem.Whenthekernelfinishesinitializingthehardware,ithappilymountstheroot filesystem,andyou'reoffandrunning. WithaninitialRAMdisk,theinitrdmustbedecompressedandplacedintomemorybeforethekernel isloaded.Otherwise,therewouldbenorootfilesystemavailableforthekerneltomountwhenit finishesitsinitialization.Here'sasummaryofthestepstakenbyabootloaderwhenusinganinitrd: 1. DecompresstheinitialRAMdisk(usuallystoredasinitrd.gz). 2. Asitisbeingdecompressed,tossitupintomemory. 3. Tossthekernelupintomemory(withoutoverwritingtheRAMdisk),tellingitthememory addresswheretheRAMdiskstarts,thedeviceit'ssupposedtousetomounttheRAMdisk

http://www.linuxdevices.com/cgi-bin/printerfriendly.cgi?id=AT5974781081 5/10/2002 LinuxDevices.com-theembeddedLinuxportal:Articles>View>PrinterFriendly Page3of8

(/dev/ram0),andkickthekerneltostartitspinningthroughtheprocessor. 4. Oncethekernelfinishesinitializing,itmountstheRAMdiskasitsrootfilesystemasifnothing specialhappened,usingthedevicelocationthatitwastoldbythebootloader.

H.PeterAnvinhascreatedapackage,calledSYSLINUX,thatcontainssomeutilitiesthatmakeit reallyquiteeasytogetasystemwithaninitrdtobootfromafloppy,aCD-ROMorfromthenetwork. Inrecentreleases,therearethreeseparatetoolsforthethreedifferentbootmethodsmentionedabove: SYSLINUX,ISOLINUXandPXELINUX.Thesetoolscanuseaconfigurationfile(syslinux.cfg, isolinux.cfgorpxelinux.cfg)tospecifybootoptionsandamessagesfile(messages.txt)togivethe personbootingthesystemimportantmessages,like"ThisistheSystemImagerautoinstallsystem.If youdonotwantyourdisksrepartitionedandfreshlyformatted,poweroffnow!" BootingfromaFloppy Tomakeourfloppybootable,wefirstmustformatthefloppywiththeFAT(FileAllocationTable) filesystem.FATistheonlyfilesystemthatisreadablebySYSLINUX.Afterformattingthefloppy, wecopyoverthefollowingfiles:initrd.gz,kernel,syslinux.cfgandmessages.txt.Inourcase,the syslinux.cfgfilecontainsthefollowingentries: DEFAULTkernel APPENDvga=extendedload_ramdisk=1prompt_ramdisk=0 initrd=initrd.gzroot=/dev/ramrw DISPLAYmessage.txt PROMPT1 TIMEOUT50

Thecontentsofthemessages.txtfiledonotaffectthebootingofthesystem,soIwon'tlistthemhere. Oncethesefileshavebeencopiedtothefloppy,weunmountthefloppyandrunthecommand syslinux-s/dev/fd0.That'sit!OurembeddedLinuxsystemcannowbootfromfloppy! BootingfromaCD-ROM WhenBOELwasfirstcreated,ISOLINUXwasn'taround,soweusedSYSLINUXforourCD-ROM bootingaswellasforfloppies.Therefore,makingaCD-ROMbootableisverysimilartomakinga floppybootable.Asamatteroffact,themethodwecurrentlyuseistocreateabootablefloppy,then usethatbootablefloppyasthebasisforanISOimage.Buttomakethingsfaster,weactuallyemploy afilethatwemountusingtheloopbackdeviceinsteadofafloppydiskette: ddif=/dev/zeroof=/tmp/diskette.imgbs=1kcount=2880 mkdosfs/tmp/diskette.img syslinux-s/tmp/diskette.img mkdir-p/tmp/mount_dir/ mount-tmsdos-oloop/tmp/diskette.img /tmp/mount_dir/ cpinitrd.gzkernelsyslinux.cfgmessage.txt /tmp/mount_dir/ umount/tmp/mount_dir/

Andfinally,weissuethecommandstocreateourElTorito-stylebootableCD-ROM:

http://www.linuxdevices.com/cgi-bin/printerfriendly.cgi?id=AT5974781081 5/10/2002 LinuxDevices.com-theembeddedLinuxportal:Articles>View>PrinterFriendly Page4of8

mkdir-p/tmp/iso_tmp_dir/boot/ cp/tmp/diskette.img/tmp/iso_tmp_dir/boot/ cd/tmp/iso_tmp_dir/ mkisofs-bboot/diskette.img-boot/boot.catalog -o/tmp/CD_Image.iso.

Nowletmecommentonacoupleofthingsyoumayhavenoticedinthecommandsabove.Weusea 2.88MBfloppyimageinsteadofthe1.44MBfloppysizeyoumightexpect;2.88MBisavalidfloppy size,anditgivesusalittlemorebreathingroom.Sincethiswillonlytakeupafractionofthecapacity oftheCD,whynot?(Honestly,Ican'trememberwhywedecidedtogowith2.88insteadof1.44.) YoualsomayhavenoticedthatSYSLINUXwasrunpriortoplacingthekernelonthefloppyimage. SYSLINUX,likeGRUBbutunlikeLILO,isactuallyabletoreadthefilesystemwherethekernel resides.Thismeansthatitcanfindandreadtheconfigurationfile,whichalwayshasthesamename. Fromtheconfigurationfileitcandeterminethefilenamesofthekernelandinitrd.Itcanthenaccess thekernelandinitrdasnormalfilesthatitneedstoworkwith.ThisisincontrasttoLILO,whichmust pre-recordthephysicallocationofyourkernelonthedisk. TheentirebootableCD-ROMandfloppycreationprocessisautomatedinSystemImagerwiththe mkautoinstallcdandmkautoinstalldiskettecommands. BootingfromtheNetwork Forbootingfromthenetwork,weusePXELINUX.PXE(pronounced"pixie")standsforpre- executionenvironment.WithPXELINUX,theprocessisverysimilartousingSYSLINUX.Wecan evenusethesameconfigurationandmessagefiles,buttheconfigurationfilemustbenamed differently.I'llexplainwhyinjustabit. TobootamachineusingPXELINUX,youmustfirstconfigureanothermachineasaDHCP (DynamicHostConfigurationProtocol)andTFTP(TrivialFileTransferProtocol)server. SystemImagercomeswithtwoutilitiesthatmakethiseasybywalkingyouthroughtheprocess: mkdhcpserverandmkbootserver.InadditiontothebasicentriesneededforhandingoutIPaddresses, mkdhcpserveralsoaddsinformationthatwillbeusedbyclientsbootingwithPXE.Mostnotably,it specifiesthelocationofthePXELINUXbinaryfromtheperspectiveofaPXE-enablednetworkcard. Inourcasethatmeans/pxelinux.bin. ThemkbootservercommandwillmakesurethatyourTFTPdæmonisinstalledandenabled.Italso willcopythePXELINUXfilestotherightlocationsotheycanbeservedupbytheTFTPdæmon. OnceyouhaveamachinesetupasaDHCPandTFTPserver,youwillneedtotellyourclienttoboot fromitsnetworkcard.Thisisusuallydonebychangingyourclient'sBIOSsettings,butalsois availableasa"pressthiskeynowtoactivate"duringboottimeoptiononsomesystems.Ifyouenter yourBIOStochangethissetting,youwillneedtolookforabitabout"BootOrder".Thisshouldlist allofthebootabledevicesinyourcomputerandwillincludeanyPXE-enabledNICsthatyoumay have. Nowwhenyoupoweronyourcomputer,insteadofaskingtheharddriveforbootinformation,itwill asktheNIC.TheNICwillmakeaDHCPrequestthatwillbeansweredbyyourDHCPserver.The DHCPserverwillprovidetheNICwiththeIPaddresssettingsitshoulduseandthelocationofthe

http://www.linuxdevices.com/cgi-bin/printerfriendly.cgi?id=AT5974781081 5/10/2002 LinuxDevices.com-theembeddedLinuxportal:Articles>View>PrinterFriendly Page5of8

PXELINUXbinary.TheNICwillthenuseTFTPtoretrievethePXELINUXbinaryfromtheTFTP server.ThePXELINUXbinaryisthenrunbytheclientsystemasabootloader.Ifwewereusing SYSLINUX,thisisthepointwhereitwouldreaditsconfigurationfilefromaFATpartition. PXELINUX,ontheotherhand,retrievesitfromacrossthenetworkandreadsitfromthedatastream. ImentionedearlierthattheconfigurationfilecouldbethesameaswithSYSLINUXbutmustbe nameddifferently.Asamatteroffact,inourcase,we'vewrittentheconfigurationfile,configuredthe TFTPserverandlocatedthekernelandinitrd.gzfilessothatwecanusethesameexactconfiguration fileforbootingfromfloppy,CDandnetwork. Andwithregardstoconfigurationfiles,PXELINUXgivesyousomeniceflexibilitybyallowingyou tospecifyadifferentconfigurationfileforeachmachineorasinglefileforagroupofmachines.It doesn'tknowenoughtoaskforafilebyhostname,butitdoesknowitsIPaddress.Soitfirstrequests aconfigurationfilenamedwiththeIPaddressthatitisusing.Thecatchisthatitusesthehexadecimal representationofthisIPaddress.Ifitcan'tfindaconfigurationfilewiththatname,itlopsoffone characterfromtheendandtriesagainuntilitrunsoutofcharactersinthefilename.Atthatpointit makesonelastdesperatetryandasksforafilenamed"default".Italsocopiesdownthemessages.txt file,butthere'snothingexcitingtosayaboutthat. Whytryallthosevariations?Becauseitallowsyoutospecifyaconfigurationfilebasedonan individualcomputerorbasedonsubnetsofincreasingsizes.Wejustusethedefaultfilebydefault. OncePXELINUXgetsitshandsontheconfigurationfile,itretrievesthekernelandRAMdiskas specifiedinthefile,alsoviaTFTP.ThekernelandRAMdiskfilenamesarerelativetotherootof whatispresentedbytheTFTPserver.Atthispoint,thebootprocessproceedsinexactlythesame wayaswithSYSLINUX--thekernelandinitialRAMdiskaretossedupintomemory,andoffwe go! ThereisonenetworkbootingtrickIshouldmention.It'skindofahackandIconsideritdangerous forproductionuse,butinspecialcircumstancesitcanbeuseful.MostmodernBIOSesaresmart enoughtotryeachbootdeviceinsuccessionifthepreviousonefails.Whatthismeansisthatyoucan configureyoursystemtobootfromthenetworkfirst,andthenfromtheharddriveifthatfails. Assumingthatyouusuallywantyoursystemstobootfromtheharddrive,youjustcripplepartof yournetworkbootconfigurationbywayoftheDHCPorTFTPserversuntilyouneedittowork.The advantageisthatyourmachinesarealreadyconfiguredinawaythatyoucanremotelydealwitha failedharddrive(dependingonhowitfails).Thedisadvantageisthatanyonewhocanpluga DHCP/TFTPserverontoyournetworkcandomaliciousthingswithyoursystems. BootingfromaHardDriveorAnythingthatLooksLikeaHardDrive SystemImager'supdateclientutilityprovidesanoptionthatallowsyoutoconnecttoaremotemachine thatneedstobere-installedandremotelypreparesittobe"autoinstalled".Thoseofyoufamiliarwith SystemImagerwillrecognizethisas"thatwonderful-aoption".Whenyourunupdateclient- autoinstall-server,itcontactstheimageserverspecified,pullsdownBOEL's kernelandinitrdandconfiguresthesystemtoloadtheseatboottime. Afterthekernelandinitrd.gzfileshavebeencopiedto/bootontheharddrive,updateclientaddsan entrytoLILO'sconfigurationfile/etc/.conf.Thatentrywilllooksomethinglikethis: image=/boot/kernel

http://www.linuxdevices.com/cgi-bin/printerfriendly.cgi?id=AT5974781081 5/10/2002 LinuxDevices.com-theembeddedLinuxportal:Articles>View>PrinterFriendly Page6of8

label=systemimager initrd=/boot/initrd.gz read-write #appendingroot=/dev/ram0ensuresthatthekernel #willnottrytorunthenonexistent/linuxrc #script append="LAST_ROOT=/dev/sda3root=/dev/ram0 load_ramdisk=1prompt_ramdisk=0"

Afteraddingthisentry,updateclientwillrunthecommandlilo-d50-Dsystemimager.Thistells LILOtoloadthesystemimagerentrywithafive-seconddelayontheverynextboot,butafterthatone deviantbootsession,toreturntobootingthedefaultkernel.Thisallowsyoutobringyoursystem backtoitsprevioususeablestateifyourautoinstallclientfailstoinitiatetheinstallforsomereason. Youalsomaynoticethreeinterestingthingsaboutthelilo.confentryabove.Oneisthatitactuallyhas commentstotellyouwhat'sgoingon.Thisisathemethatwehavetriedtocarryonthroughout BOELandtheSystemImagerdistributionasawhole.Whenyouaremakingsomanysmalldecisions whereeachaffectsthebehaviorofanotherpieceofthepuzzleseveralstepsdowntheline,itis incrediblyimportanttobeabletorecallwhyyoudidsomethingthatmaylookalittleoddsixmonths fromnow. Skippingtheseconditemforaminutetoprovidecontinuity(noncontinuityforthesakeofcontinuity), thethirditemofinterestistheLAST_ROOT=appendparameter.Appendparametersareawayto givethekernelconfigurationdetails.Youcanthinkofitassettingvariablesforthekernel.Andyou hard-corekernelhackerswillbenotingthatLAST_ROOTisnotavalidappendparameter--that's becauseImadeitup!It'savariablethatisusedbySystemImagertoidentifytherootfilesystemthat wasinusepriortostartinganautoinstallbywayofbootingfromtheharddrive.ThisallowsBOELto lookforaSystemImagerconfigurationfile,local.cfg,thatcanbeusedtocontrolsomeofthe autoinstallbehavior,suchasyourclient'sIPaddress,yourimageserver'sIPaddressandyourdesired imagename,amongothers. No,Ididn'thavetohackthekerneltousethisvariable.Asamatteroffact,thekerneldoesn'tuseitat all!But,itisvisibleinuserspacebymeansofthe/proc/cmdlinekerneltable.Takealooktoseewhat yourshasinit:cat/proc/cmdline.Notmuchthere,huh?Well,evenifyoupassedyourkernel"a wholemessofoptions",asBillCosbywouldsay,onlythefirstfewattheverybeginningshowup here.Andthat'swhyLAST_ROOTistheveryfirstappendparameter.Lestyoubeconcernedthatyou havestuffin/proc/cmdlinebuthavenoappendparametersspecified,it'sprobablybecauseyourboot loaderautomaticallypassesyourkernelsomeofthethingsitneedsinordertobootproperly.Youcan doamanbootparamtogetthelowdownonalltheofficialappendparameters. Finally,theseconditemofinterest,whichleadsintothenextsection,isthecommentsthemselves. Whywouldappendingroot=/dev/ram0ensurethatthekernelwouldnottrytorunthe nonexistent/linuxrcscript?Well,atthetimeIaddedthatcomment,therewasabug/featureinthe Linuxkernelthatcaused/linuxrcalwaystobeexecutedwhenusinganinitialRAMdisk--except whenyouused/dev/ram0astherootdevice.Youcanactuallychoosetouseanyoftheavailable RAMdevicesasyourrootdevice,butIdidn'twanttorun/linuxrc.Iwantedtogostraight to/sbin/init.Nowadaysthekernelhasbeenfixed,andyoucansimplyusethebootparameter init=/linuxrcifyoustillwanttouseascriptorothercustomexecutableinsteadofgoingstraight to/sbin/init.

http://www.linuxdevices.com/cgi-bin/printerfriendly.cgi?id=AT5974781081 5/10/2002 LinuxDevices.com-theembeddedLinuxportal:Articles>View>PrinterFriendly Page7of8

HackingDHCPtoSendandReceiveNonstandardInformation SowhybotherwithdhclientontheRAMdiskifspaceissoimportant?Whynotjustusethe"IP: kernel-levelconfigurationsupport"optioninthekernel?Because,wewanttodomischievousthings withDHCP,thingslikecreatingourownoptionstosendnonstandarddata.Andwewanttheabilityto usetheseoptionstocontrolthebehaviorofourembeddedLinuxsystem,notjustaffectboot-related parameters. Now,ifyou'veevertakenalookatthedhcp-optionsmanpage,youknowthatthere'sawholemessof optionsthatyoucanspecifyinyourdhcpd.conffile.Andifyouwerelookingclosely,youmayhave noticedthatyoucanalsomakeupyourown.That'sright,youcanpassanyinformationyouliketo yourDHCPclientsbyaddingalinetoyourDHCPserverlikeso: optionoption-133 "stuff-what-I-want-to-send-to-my-clients";

Nowtheproblemis,whatcanmyclientdowithit?Ifyouareusingeitherdhcpcd(DHCPclient dæmon)orpump(notanacronym,canyoubelieveit?),andtheyreceiveanoptiontheydon't recognize,theyjustdropitonthefloorandstomponitafewtimes.Bothhaveaverylimitedsetof fixedoptionsthattheyunderstand.Ofcourse,wecouldhackthecodetomakethemrecognizethe nonstandardoptionswewanttosend,butdowereallywanttodothateverytimeanewversionofthe codecomesout? AlthoughdhcpcdandpumpseemtobethetwomostpopularDHCPclients,thereisathirdcalled dhclient.dhclientseemstoberarelyused,eventhoughitispartofthesamepackageastheDHCP serversoftwaremadebyISCthatisthedefactostandard.Incontrasttodhcpcdandpump,dhclientis abletoacceptanyandalloftheoptionsthatitssiblingserversoftwareisabletosend--eventheones youmakeup. dhclientusesaconfigurationfile(/etc/dhclient.conf)andascript(/etc/dhclient-script).Thisgivesusa lotofroomforcustomizationsthatwillhopefullysurvivecodechanges.Anditturnsoutthatthe optionsspecifiedattheserversimplyshowupasvariableswhiledhclientscriptisrunning.Soifwe wantedtocapturetheinformationpassedfromtheserverasoption-133,wecanaccessthatdatainthe variable$new_option_133.ForBOEL,weaddcommandstothedhclient-scriptthatstoresthese valuessothattheycanbeeasilyretrievedbyourembeddedsystem--asasourceabledot-stylefile. Hereisanexamplelinethatweaddtothisscript: echo"SSH_DOWNLOAD_URL=$new_option_208" >>/tmp/dhcp_variables.txt

BOELisdistributedaspartofSystemImagerandisavailableinsourceformhereandmakeinitrd willproducethesetwocomponentsofBOEL.ThankstoDannFrazierandtheentireSystemImager developmentteam.

Abouttheauthor:BrianElliottFinleyhasspecializedinoperatingsystems,system architectureandsystemintegrationsince1990.In1993hefellinlovewithLinux.

http://www.linuxdevices.com/cgi-bin/printerfriendly.cgi?id=AT5974781081 5/10/2002 LinuxDevices.com-theembeddedLinuxportal:Articles>View>PrinterFriendly Page8of8

SystemImagerisaprojectbegunin1998asawaytoupdatemultipleSolarismachinesacrosstheUS.

Copyright©2002SpecializedSystemsConsultants,Inc.Allrightsreserved.EmbeddedLinuxJournal OnlineisacooperativeprojectofEmbeddedLinuxJournalandLinuxDevices.com.

|aboutus|contactus|termsofuse|home|search|directory|news|articles|polls|forum| events|jobs|products|links|sponsors| Exceptwhereotherwisespecified,thecontentsofthissitearecopyright©1999-2001DeviceForgeLLC.Allrights reserved.DeviceForge,LinuxDevices,andLinuxDevices.comaretrademarksofDeviceForgeLLC.The LinuxDevices.comlogoisaservicemarkofDeviceForgeLLC.Allothertrademarksarethepropertyoftheir respectiveowners.SitecodingbyWebgurus.com

http://www.linuxdevices.com/cgi-bin/printerfriendly.cgi?id=AT5974781081 5/10/2002