You are on page 1of 30

EmbeddedLinuxFromScratch

EmbeddedLinuxFromScratch
in40minutes!
MichaelOpdenacker
FreeElectrons
http://freeelectrons.com/

nada+40min=
CreatedwithOpenOffice.org2.0

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 1
Rightstocopy

AttributionShareAlike2.0 Copyright20042005
Youarefree MichaelOpdenacker
tocopy,distribute,display,andperformthework michael@freeelectrons.com
tomakederivativeworks
tomakecommercialuseofthework Documentsources,updatesandtranslations:
Underthefollowingconditions http://freeelectrons.com/articles/elfs
Attribution.Youmustgivetheoriginalauthorcredit.
Corrections,suggestions,contributionsand
ShareAlike.Ifyoualter,transform,orbuilduponthiswork,
youmaydistributetheresultingworkonlyunderalicense translationsarewelcome!
identicaltothisone.
Foranyreuseordistribution,youmustmakecleartoothersthe
licensetermsofthiswork.
Anyoftheseconditionscanbewaivedifyougetpermissionfrom
thecopyrightholder.
Yourfairuseandotherrightsareinnowayaffectedbytheabove.
Licensetext:http://creativecommons.org/licenses/bysa/2.0/legalcode

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 2
Tutorialgoals

Buildatinyembeddedsystementirelyfromscratch,in40minutes
Linuxkernelconfiguringandcompiling
Rootfilesystemcreation
Busyboxcompilingandinstallation
Devicefilecreation
Systeminitializationscripts:virtualfilesystems,networking
SetupofasimpleHTTPinterfacetothesystem
Showyouhowsimplethiscanbe!

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 3
Topdownapproach

Topdownapproachtobuildinganembeddedsystem
StartingfromacompletedesktopGNU/Linuxdistribution(Debian,
Fedora...)andremovingunneededstuff.
Verytediousjob:needtogothroughahugenumberoffilesand
packages.Needtounderstandwhateachfileandpackageisabout
beforeremovingit.
Keepingunnecessarilycomplexscriptsandconfigurationfiles.
Theendresultisstillquitebig,asstandarddesktoptoolsetsand
librariesareused.Lotsofsharedlibrariesstillneededtoo.

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 4
Bottomupapproach

Bottomupapproachtobuildingembeddedsystems
Startingwithanemptyorminimalisticrootfilesystem,adding
onlythingsthatyouneed.
Mucheasiertodo!Youjustspendtimeonthingsyouneed.
Mucheasiertocontrolandmaintain:youbuildanunderstanding
aboutthetoolsyouuse.
Youonlyneedverysimpleconfigurationscripts.
Theendresultcanbeextremelysmall,allthemoreasyouuse
lightweighttoolsetsinstead.

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 5
EmbeddedLinuxFromScratch

Toolsusedinthistutorial
Explanatoryslidestoshowwhilecompiling

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 6
qemu

http://qemu.org
Fastprocessoremulator
usingaportabledynamictranslator.
2operatingmodes
Fullsystememulation:processorandvariousperipherals
Supported:x86,x86_64,ppc
Usermodeemulation(Linuxhostonly):canrunapplications
compiledforanotherCPU.
Supported:x86,ppc,arm,sparc

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 7
qemuexamples
Useremulation

EasytorunBusyboxforarmoni386GNU/Linux:
qemuarmL/usr/local/arm/3.3.2\
/home/bart/arm/busybox1.00pre8/busyboxls

L:targetClibrarybinariespath(herecrosscompilertoolchainpath)

Systememulation

Eveneasiertorun:
qemulinux.img

linux.img:fullpartitionimageincludingthekernel
Plentyofimagesforfreeoperatingsystemsonhttp://freeoszoo.org!

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 8
Generalpurposetoolbox:busybox
http://www.busybox.net/fromCodepoetConsulting
MostUnixcommandlineutilitieswithinasingleexecutable!
Evenincludesawebserver!
Sizeslessthan1MB(staticallycompiledwithglibc)
lessthan500MB(staticallycompiledwithuClibc)
Easytoconfigurewhichfeaturestoinclude
Thebestchoicefor
Initrdswithcomplexscripts
Anyembeddedsystem!

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 9
Busyboxcommands!
addgroup,adduser,adjtimex,ar,arping,ash,awk,basename,bunzip2,
bzcat,cal,cat,chgrp,chmod,chown,chroot,chvt,clear,cmp,cp,cpio,
crond,crontab,cut,date,dc,dd,deallocvt,delgroup,deluser,devfsd,
df,dirname,dmesg,dos2unix,dpkg,dpkgdeb,du,dumpkmap,dumpleases,
echo,egrep,env,expr,false,fbset,fdflush,fdformat,fdisk,fgrep,
find,fold,free,freeramdisk,fsck.minix,ftpget,ftpput,getopt,getty,
grep,gunzip,gzip,halt,hdparm,head,hexdump,hostid,hostname,httpd,
hush,hwclock,id,ifconfig,ifdown,ifup,inetd,init,insmod,install,
ip,ipaddr,ipcalc,iplink,iproute,iptunnel,kill,killall,klogd,
lash,last,length,linuxrc,ln,loadfont,loadkmap,logger,login,
logname,logread,losetup,ls,lsmod,makedevs,md5sum,mesg,mkdir,
mkfifo,mkfs.minix,mknod,mkswap,mktemp,modprobe,more,mount,msh,
mt,mv,nameif,nc,netstat,nslookup,od,openvt,passwd,patch,pidof,
ping,ping6,pipe_progress,pivot_root,poweroff,printf,ps,pwd,rdate,
readlink,realpath,reboot,renice,reset,rm,rmdir,rmmod,route,rpm,
rpm2cpio,runparts,rx,sed,seq,setkeycodes,sha1sum,sleep,sort,
startstopdaemon,strings,stty,su,sulogin,swapoff,swapon,sync,
sysctl,syslogd,tail,tar,tee,telnet,telnetd,test,tftp,time,top,
touch,tr,traceroute,true,tty,udhcpc,udhcpd,umount,uname,
uncompress,uniq,unix2dos,unzip,uptime,usleep,uudecode,uuencode,
vconfig,vi,vlock,watch,watchdog,wc,wget,which,who,whoami,xargs,
yes,zcat

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 10
glibc

http://www.gnu.org/software/libc/
ClibraryfromtheGNUproject
Designedforperformance,standardscomplianceandportability
FoundonallGNU/Linuxhostsystems
Quitebigforsmallembeddedsystems:about~1.7MBonFamiliar
LinuxiPAQs(libc:1.2MB,libm:500KB)
Examplehelloworldprogramsize:12KB(dynamicallylinked),
350KB(staticallylinked)

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 11
uClibc

http://www.uclibc.org/forCodePoetConsulting
LightweightClibraryforsmallembeddedsystems,withmost
featuresthough.
ThewholeDebianWoodywasrecentlyportedtoit...Youcan
assumeitsatisfiedmostneeds!
Examplesize(arm):approx.400KB(libuClibc:300KB,libm:
55KB)
Examplehelloworldprogramsize:2KB(dynamicallylinked),18
KB(staticallylinked).

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 12
Kerneluserspaceinterface
Afewexamples:
/proc/cpuinfo:processorinformation
/proc/meminfo:memorystatus
/proc/version:versionandbuildinformation
/proc/cmdline:kernelcommandline
/proc/<pid>/environ:callingenvironment
/proc/<pid>/cmdline:processcommandline
...andmanymore!Completedetailsinthekernelsources:
Documentation/filesystems/proc.txt

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 13
EmbeddedLinuxFromScratch

Whatwedid

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 14
CompilingtheLinuxkernel

GettingtheLinuxsourcesfromhttp://kernel.org
SettingEXTRA_VERSIONinMakefile
Settingaminimalistickernelconfiguration:
makeallnoconfig
Addingsettingsspecifictotheembeddedsystem:
makexconfigormakemenuconfig
Compiling:
make
Result:compressedkernelimage
arch/i386/boot/bzImage
EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 15
Creatingarootfilesystem

Creatinganemptyfilewitha320Ksize:
ddif=/dev/zeroof=rootfs.imgbs=320kcount=1
Formatingthisfilefortheext2filesystem:
mkfs.ext2i1024Frootfs.ext2
1inodeevery1024bytes>320files
insteadof1inodeevery4096bytes>only80files!

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 16
Compilingbusybox

Gettingthesourcesfromhttp://busybox.net
Configuringbusybox:
makemenuconfig
Choosingtobuildastatically,nativelycompiledexecutable.
Compilingbusybox:
make
Preinstallingbusybox(inthe_install/subdirectory):
makeinstall
Result:a500Kexecutableimplementing
allthecommandsthatweneed!

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 17
Recompilingbusybox
500Kwasalreadywaytoobigforaperfectembeddedsystem!
Configuringbusyboxagain
makemenuconfig
Choosingtobuildastatically,crosscompiledexecutable,using
auClibctoolchaininsteadofthestandardglibcone.
Compilingbusybox:
make
Preinstallingbusybox(inthe_install/subdirectory):
makeinstall
Result:a250Kexecutableimplementing
allthecommandsthatweneed!
EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 18
Populatingtherootfilesystem

Loggedasroot:
Creatingamountpoint:
mkdir/mnt/rootfs
Mountingtherootfilesystemimage:
mountolooprootfs.img/mnt/rootfs
Copyingthebusyboxfilestructureintothemountedimage:
rsyncabusybox/_install//mnt/rootfs/
chownRroot:root/mnt/rootfs/
Flushingthechangesintothemountedfilesystemimage:
sync
EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 19
Bootingthevirtualsystem

Usingtheqemuemulatorasabootloader
(noneedtocopythekerneltothetargetstorage)
qemu\
m32\ Amountofmemory(MB)intheemulatedtarget
hdarootfs.img\ Contentsoftheemulatedharddisk
kernellinux2.6.12/arch/i386/boot/bzImage\
Kernelimage
append"root=/dev/hdaclock=pit"
Kernelcommandline

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 20
Creatingdevicefiles

Creatingdevicefileswhenprogramscomplain:
mkdir/mnt/rootfs/dev
mknod/mnt/rootfs/dev/consolec51
mknod/mnt/rootfs/dev/null13
TakingtheGNU/Linuxhostasanexampletofindcorrect
majorandminornumbers:
lsl/dev/console
lsl/dev/null

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 21
Mountingvirtualfilesystems

Making/procand/sysavailable
(requiredbyseveralcommandlinetoolssuchasps)

Mounting/proc:
mounttprocnone/proc
Mounting/sys:
mounttsysfsnone/sys

Filesystemtype Rawdevice Mountpoint


orfilesystemimage
Inthecaseofvirtual
filesystems,anystringisfine

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 22
/etc/inittabfileforbusyboxinit

Creatingthe/etc/inittabfilerequiredbybusyboxinit
Gettinganexamplefrombusyboxdocumentation
(notfromtheGNU/Linuxhost...missingfeatures!)
#Thisisrunfirstscript
::sysinit:/etc/init.d/rcS
#Startan"askfirst"shellontheconsole
::askfirst:/bin/sh
#Stufftodowhenrestartingtheinitprocess
::restart:/sbin/init
#Stufftodobeforerebooting
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umountar

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 23
Settingupnetworking

AddingTCP/IPandnetworkcarddrivertothekernel
Bringingupthenetworkinterface:
ifconfigeth0172.20.0.2
UsingtheGNU/Linuxhostasagateway:
routeadddefaultgw172.20.0.1
Testingnetworking:
pingc3172.20.0.1
c3:usefulwhen[Ctrl][C]doesn'twork
(missingttysettings)
Testingrouting:
pingc3<externaladdress>
EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 24
Startingupahttpserver

CopyingHTMLpageson/www(forexample)
CreatingCGIscriptsin/www/cgibin/
Startingthebusyboxhttpserver:
/usr/sbin/httpdh/www/&

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 25
/etc/init.d/rcSstartupscript

#!/bin/sh
mounttprocnone/proc
mounttsysfsnone/sys
ifconfigeth0172.20.0.2
routeadddefaultgw172.20.0.1
/usr/sbin/httpdh/www/&
/bin/sh
Seehowsimplethiscanbe!

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 26
AsimplisticCGIscript

/www/cgibin/uptime:
#!/bin/sh
echo"Contenttype:text/html"
echo""
echo"<html><header></header><body>"
echo"<h1>Uptimeinformation</h1>"
echo"Yourembeddeddevicehasbeen
runningfor:<pre><fontcolor=Blue>"
echo`uptime`
echo"</font></pre></u>"
echo"</body></html>"

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 27
Limitations

Afewminorlimitations
CGIscripts:can'timplementnontrivialscripts
NeedtocodeinCtosupportpostingandURLparsing.
Systemspecificsoftware:can'tbepartofbusybox.
NeedmoreCexecutables.Asaconsequence,needtoinclude
theuClibclibraryandcompiletheexecutableswithshared
librarysupport.
Theyareeasyandcheaptoovercome!

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 28
Realembeddedsystems

Thistutorialhasalreadybeendoneonrealdevelopmentboards!
Needtoinstallandconfigureabootloader(ifmissing)
Needtotransferkernelandrootfilesystemimagestothetarget.
AnefficientwayistomakethetargetbootonaNFSexported
directoryontheGNU/Linuxhost.

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 29
Relateddocuments
ThisdocumentbelongstothematerialsofanEmbeddedLinuxtrainingfromFree
Electrons,availableunderafreedocumentationlicense(morethan900pages!)
http://freeelectrons.com/training http://freeelectrons.com/articles

IntroductiontoUnixandGNU/Linux JavainembeddedLinuxsystems

EmbeddedLinuxkernelanddriver What'snewinLinux2.6?
development
IntroductiontouClinux
ToolsforembeddedLinuxsystems
Linuxrealtimeextensions
AudioinembeddedLinuxsystems
LinuxonTIOMAPprocessors
MultimediainembeddedLinux
Introductiontosoftware
systems
developmenttools

EmbeddedLinuxFromScratch...in40minutes!
Copyright20042005,MichaelOpdenacker
CreativeCommonsAttributionShareAlike2.0license
http://freeelectrons.com Jul17,2005 30

You might also like