Sunteți pe pagina 1din 28

Hotpluggingwithudev

Hotplugging
withudev
MichaelOpdenacker
FreeElectrons

Copyright20042009,FreeElectrons.
CreativeCommonsBYSA3.0license
Latestupdate:Sep28,2010,
Documentsources,updatesandtranslations:
http://freeelectrons.com/docs/udev
Corrections,suggestions,contributionsandtranslationsarewelcome!

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

/devissuesandlimitations
OnRedHat9,18000entriesin/dev!
Allentriesforallpossibledevices
hadtobecreatedatsysteminstallation.
Neededanauthoritytoassignmajornumbers
http://lanana.org/:LinuxAssignedNamesandNumbers
Authority
Notenoughnumbersin2.4,limitsextendedin2.6.
Userspaceneitherknewwhatdeviceswerepresentinthe
system,norwhichrealdevicecorrespondedtoeach/dev
entry.

2
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Theudevsolution
TakesadvantageofsysfsintroducedbyLinux2.6.
CreatedbyGregKroahHartman,ahugecontributor.
Otherkeycontributors:KaySievers,DanStekloff.
Entirelyinuserspace.
Automaticallycreates/removesdeviceentries
in/dev/accordingtoinserted/removeddevices.
Majorandminordevicetransmittedbythekernel.
Requiresnochangetodrivercode.
Fast:writteninC
Smallsize:udevdversion108:61KBinUbuntu7.04

3
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Startingudev(1)
Attheverybeginningofuserspacestartup,
mountthe/dev/directoryasatmpfsfilesystem:
sudomountttmpfsudev/dev
/dev/ispopulatedwithstaticdevicesavailablein
/lib/udev/devices/:
Ubuntu6.10example:
crw1rootroot5,12007013104:18console
lrwxrwxrwx1rootroot112007013104:18core>/proc/kcore
lrwxrwxrwx1rootroot132007013104:18fd>/proc/self/fd
crwr1rootkmem1,22007013104:18kmem
brw1rootroot7,02007013104:18loop0
lrwxrwxrwx1rootroot132007013104:18MAKEDEV>/sbin/MAKEDEV
drwxrxrx2rootroot40962007013104:18net
crw1rootroot1,32007013104:18null
crw1rootroot108,02007013104:18ppp
drwxrxrx2rootroot40962006101614:39pts
drwxrxrx2rootroot40962006101614:39shm
lrwxrwxrwx1rootroot242007013104:18sndstat>/proc/asound/oss/sndstat
lrwxrwxrwx1rootroot152007013104:18stderr>/proc/self/fd/2
lrwxrwxrwx1rootroot152007013104:18stdin>/proc/self/fd/0
lrwxrwxrwx1rootroot152007013104:18stdout>/proc/self/fd/1

4
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Startingudev(2)
Theudevddaemonisstarted.
Itlistenstoueventsfromthedrivercore,
whicharesentwheneverdevicesareinsertedorremoved.
Theudevddaemonreadsandparsesalltherulesfoundin
/etc/udev/rules.d/
andkeepstheminmemory.
Wheneverrulesareadded,removedormodified,
udevdreceivesaninotifyeventandupdatesits
rulesetinmemory.
Whenaneventisreceived,udevdstartsaprocessto:

Theinotifymechanismlets
userspaceprogramssubscribe
tonotificationsoffilesystem
changes.Possibilitytowatch
individualfilesordirectories.

trytomatchtheeventagainstudevrules,
create/removedevicefiles,
andrunprograms(toload/removeadriver,tonotifyuserspace...)

5
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

ueventmessageexample
ExampleinsertingaUSBmouse
recv(4,
//socketid
"add@/class/input/input9/mouse2\0
//message
ACTION=add\0
//actiontype
DEVPATH=/class/input/input9/mouse2\0
//pathin/sys
SUBSYSTEM=input\0
//subsystem(class)
SEQNUM=1064\0
//sequencenumber
PHYSDEVPATH=/devices/pci0000:00/0000:00:1d.1/usb2/22/22:1.0\0

//devicepathin/sys
PHYSDEVBUS=usb\0
//bus
PHYSDEVDRIVER=usbhid\0
//driver
MAJOR=13\0
//majornumber
MINOR=34\0",
//minornumber
2048,
//messagebuffersize
0)
//flags
=221
//actualmessagesize

6
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

udevrules
Whenaudevrulematchingeventinformationisfound,
itcanbeused:
Todefinethenameandpathofadevicefile.
Todefinetheowner,groupandpermissionsofadevicefile.
Toexecuteaspecifiedprogram.
Rulefilesareprocessedinlexicalorder.

7
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

udevnamingcapabilities
Devicenamescanbedefined
fromalabelorserialnumber,
fromabusdevicenumber,
fromalocationonthebustopology,
fromakernelname,
fromtheoutputofaprogram.
Seehttp://www.reactivated.net/writing_udev_rules.html
foraverycompletedescription.Seealsomanudev.

8
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

udevnamingruleexamples
#Namingtestingtheoutputofaprogram
BUS=="scsi",PROGRAM="/sbin/scsi_id",RESULT=="OEM0815",NAME="disk1"
#USBprintertobecalledlp_color
BUS=="usb",SYSFS{serial}=="W09090207101241330",NAME="lp_color"
#SCSIdiskwithaspecificvendorandmodelnumberwillbecalledboot
BUS=="scsi",SYSFS{vendor}=="IBM",SYSFS{model}=="ST336",NAME="boot%n"
#soundcardwithPCIbusid00:0b.0tobecalleddsp
BUS=="pci",ID=="00:0b.0",NAME="dsp"
#USBmouseatthirdportofthesecondhubtobecalledmouse1
BUS=="usb",PLACE=="2.3",NAME="mouse1"
#ttyUSB1shouldalwaysbecalledpdawithtwoadditionalsymlinks
KERNEL=="ttyUSB1",NAME="pda",SYMLINK="palmtophandheld"
#multipleUSBwebcamswithsymlinkstobecalledwebcam0,webcam1,...
BUS=="usb",SYSFS{model}=="XV3",NAME="video%n",SYMLINK="webcam%n"

9
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

udevpermissionruleexamples
Excerptsfrom/etc/udev/rules.d/40permissions.rules
#Blockdevices
SUBSYSTEM!="block",GOTO="block_end"
SYSFS{removable}!="1",GROUP="disk"
SYSFS{removable}=="1",GROUP="floppy"
BUS=="usb",GROUP="plugdev"
BUS=="ieee1394",GROUP="plugdev"
LABEL="block_end"
#Otherdevices,byname
KERNEL=="null",MODE="0666"
KERNEL=="zero",MODE="0666"
KERNEL=="full",MODE="0666"

10
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Identifyingdevicedrivermodules
Kernel/modulecompiling

Systemeverydaylife

Eachdriverannounceswhichdeviceandvendor
idsitsupports.Informationstoredinmodulefiles.

Thedrivercore(usb,pci...)readsthedeviceid,
vendoridandotherdeviceattributes.

Thedepmodacommandprocesses
modulefilesandgenerates
/lib/modules/<version>/modules.alias

Thekernelsendsaneventtoudevd,settingthe
MODALIASenvironmentvariable,encodingthesedata.

Audeveventprocessruns
modprobe$MODALIAS

modprobefindsthemoduletoload
inthemodules.aliasfile.

11
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Modulealiases
MODALIASenvironmentvariableexample(USBmouse):
MODALIAS=usb:v046DpC03Ed2000dc00dsc00dp00ic03isc01ip02
Matchinglinein/lib/modules/<version>/modules.alias:
aliasusb:v*p*d*dc*dsc*dp*ic03isc01ip02*usbmouse

12
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

udevmodproberuleexamples
Evenmoduleloadingisdonewithudev!
Excerptsfrom/etc/udev/rules.d/90modprobe.rules
ACTION!="add",GOTO="modprobe_end"
SUBSYSTEM!="ide",GOTO="ide_end"
IMPORT{program}="ide_mediaexport$devpath"
ENV{IDE_MEDIA}=="cdrom", RUN+="/sbin/modprobeQbaidecd"
ENV{IDE_MEDIA}=="disk",
RUN+="/sbin/modprobeQbaidedisk"
ENV{IDE_MEDIA}=="floppy",RUN+="/sbin/modprobeQbaidefloppy"
ENV{IDE_MEDIA}=="tape",RUN+="/sbin/modprobeQbaidetape"
LABEL="ide_end"
SUBSYSTEM=="input",PROGRAM="/sbin/grepmapudev",\
RUN+="/sbin/modprobeQba$result"
#Loaddriversthatmatchkernelsuppliedalias
ENV{MODALIAS}=="?*",RUN+="/sbin/modprobeQ$env{MODALIAS}"

13
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Coldplugging
Issue:loosingalldeviceeventshappeningduringkernel
initialization,becauseudevisnotreadyyet.
Solution:afterstartingudevd,havethekernelemitueventsfor
alldevicespresentin/sys.
Thiscanbedonebytheudevtriggerutility.
Strongbenefit:completelytransparentforuserspace.
Legacyandremovabledeviceshandledandnamedinexactlythe
sameway.

14
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Debuggingeventsudevmonitor(1)
udevadmmonitorvisualizesthedrivercoreevents
andtheudeveventprocesses.
ExampleeventsequenceconnectingaUSBmouse:
UEVENT[1170452995.094476]add@/devices/pci0000:00/0000:00:1d.7/usb4/43/43.2
UEVENT[1170452995.094569]add@/devices/pci0000:00/0000:00:1d.7/usb4/43/43.2/43.2:1.0
UEVENT[1170452995.098337]add@/class/input/input28
UEVENT[1170452995.098618]add@/class/input/input28/mouse2
UEVENT[1170452995.098868]add@/class/input/input28/event4
UEVENT[1170452995.099110]add@/class/input/input28/ts2
UEVENT[1170452995.099353]add@/class/usb_device/usbdev4.30
UDEV[1170452995.165185]add@/devices/pci0000:00/0000:00:1d.7/usb4/43/43.2
UDEV[1170452995.274128]add@/devices/pci0000:00/0000:00:1d.7/usb4/43/43.2/43.2:1.0
UDEV[1170452995.375726]add@/class/usb_device/usbdev4.30
UDEV[1170452995.415638]add@/class/input/input28
UDEV[1170452995.504164]add@/class/input/input28/mouse2
UDEV[1170452995.525087]add@/class/input/input28/event4
UDEV[1170452995.568758]add@/class/input/input28/ts2

Itgivestimeinformationmeasuredinmicroseconds.
Youcanmeasuretimeelapsedbetweentheuevent(UEVENTline),andthe
completionofthecorrespondingudevprocess(matchingUDEVline).
15
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Debuggingeventsudevmonitor(2)
udevadmmonitorenv
showsthecompleteeventenvironmentforeachline.
UDEV[1170453642.595297]add@/devices/pci0000:00/0000:00:1d.7/usb4/43/43.2/43.2:1.0
UDEV_LOG=3
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb4/43/43.2/43.2:1.0
SUBSYSTEM=usb
SEQNUM=3417
PHYSDEVBUS=usb
DEVICE=/proc/bus/usb/004/031
PRODUCT=46d/c03d/2000
TYPE=0/0/0
INTERFACE=3/1/2
MODALIAS=usb:v046DpC03Dd2000dc00dsc00dp00ic03isc01ip02
UDEVD_EVENT=1

16
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Miscudevutilities
udevinfo
Letsusersquerytheudevdatabase.
udevtest<sysfs_device_path>
Simulatesaudevruntotesttheconfiguredrules.

17
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Firmwarehotplugging
Alsoimplementedwithudev!
Firmwaredataarekeptoutsidedevicedrivers
Maynotbelegalorfreeenoughtodistribute
Firmwareinkernelcodewouldoccupymemory
permanently,evenifjustusedonce.
Kernelconfiguration:needstobesetin
CONFIG_FW_LOADER
(DeviceDrivers>GenericDriverOptions>hotplug
firmwareloadingsupport)

18
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Firmwarehotpluggingimplementation
Kernelspace

Userspace

Driver

/sys/class/firmware/xxx/{loading,data}
appear

callsrequest_firmware()
Sleeps

firmwaresubsystemeventsenttoudev
Calling/lib/udev/firmware_helper
Kernel
Getreadytoloadfirmwaredata
Growsabuffertoaccommodateincomingdata

/lib/udev/firmware_helper
echo1>/sys/class/firmware/xxx/loading
catfw_image>/sys/class/firmware/xxx/data
echo0>/sys/class/firmware/xxx/loading

Driver
wakesupafterrequest_firmware()
Copiesthebuffertothehardware
Callsrelease_firmware()

SeeDocumentation/firmware_class/foraniceoverview

19
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

udevfiles
/etc/udev/udev.conf
udevconfigurationfile.
Mainlyusedtoconfiguresyslogreportingpriorities.
Examplesetting:udev_log="err"
/lib/udev/rules.d/
Standardudeveventmatchingrules,installedbythedistribution.
/etc/udev/rules.d/*.rules
Local(custom)udeveventmatchingrules.Besttomodifythese.
/lib/udev/devices/*
static/devcontent(suchas/dev/console,/dev/null...).
/lib/udev/*
helperprogramscalledfromudevrules.
/dev/*
Createddevicefiles.
20
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Kernelconfigurationforudev
Createdfor2.6.19
Caution:nodocumentationfound,andnottestedyetonaminimalisticsystem.
Somesettingsmaystillbemissing.
Subsystemsanddevicedrivers(USB,PCI,PCMCIA...)shouldbeaddedtoo!
#Generalsetup
CONFIG_HOTPLUG=y
#Networking,networkingoptions
CONFIG_NET=y
CONFIG_UNIX=y
CONFIG_NETFILTER_NETLINK=y
CONFIG_NETFILTER_NETLINK_QUEUE=y
#Pseudofilesystems
CONFIG_PROC_FS=y
CONFIG_SYSFS=y
CONFIG_TMPFS=y
CONFIG_RAMFS=y

Unixdomainsockets

Neededtomanage/dev

21
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

udevsummarytypicaloperation
Kerneldrivercore
(usb,pci...)

uevent
udevd

udeveventprocess
Matcheseventtorules
Creates/removes
devicefiles
/lib/udev/programsorothers
Loadtherightmodule
Notifyuserspace
programs(GUI...)

22
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

udevresources
Homepage
http://kernel.org/pub/linux/utils/kernel/hotplug/udev.html
Sources
http://kernel.org/pub/linux/utils/kernel/hotplug/
Theudevmanualpage:
manudev

23
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

mdev,theudevforembeddedsystems
udevmightbetooheavyweightforsomeembeddedsystems,
theudevddaemonstayinginthebackgroundwaitingforevents.
BusyBoxprovidesasimpleralternativecalledmdev,availableby
enablingtheMDEVconfigurationoption.
mdev'susageisdocumentedindoc/mdev.txtintheBusyBox
sourcecode.
mdevisalsoabletoloadfirmwaretothekernellikeudev

24
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

mdevusage
Tousemdev,theprocandsysfsfilesystemsmustbemounted
mdevmustbeenabledasthehotplugeventmanager
echo/sbin/mdev>/proc/sys/kernel/hotplug

Needtomount/devasatmpfs:
mountttmpfsmdev/dev
Tellmdevtocreatethe/deventriescorrespondingtothe
devicesdetectedduringbootwhenmdevwasnotrunning:
mdevs

Thebehaviorisspecifiedbythe/etc/mdev.conf
configurationfile,withthefollowingformat
<deviceregex><uid>:<gid><octalpermissions>
[=path][@|$|*<command>]

Example
hd[az][09]*0:3660
25
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Relateddocuments

Allourtechnicalpresentations
onhttp://freeelectrons.com/docs
Linuxkernel
Devicedrivers
Architecturespecifics
EmbeddedLinuxsystemdevelopment
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Howtohelp
Youcanhelpustoimproveandmaintainthisdocument...
Bysendingcorrections,suggestions,contributionsand
translations
Byaskingyourorganizationtoorderdevelopment,consulting
andtrainingservicesperformedbytheauthorsofthese
documents(seehttp://freeelectrons.com/).
Bysharingthisdocumentwithyourfriends,colleagues
andwiththelocalFreeSoftwarecommunity.
Byaddinglinksonyourwebsitetoouronlinematerials,
toincreasetheirvisibilityinsearchengineresults.

FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com

Linuxkernel
Linuxdevicedrivers
Boardsupportcode
Mainstreamingkernelcode
Kerneldebugging
EmbeddedLinuxTraining
Allmaterialsreleasedwithafreelicense!
UnixandGNU/Linuxbasics
Linuxkernelanddriversdevelopment
RealtimeLinux,uClinux
Developmentandprofilingtools
Lightweighttoolsforembeddedsystems
Rootfilesystemcreation
Audioandmultimedia
Systemoptimization

FreeElectrons
Ourservices
CustomDevelopment
Systemintegration
EmbeddedLinuxdemosandprototypes
Systemoptimization
Applicationandinterfacedevelopment
Consultingandtechnicalsupport
Helpindecisionmaking
Systemarchitecture
Systemdesignandperformancereview
Developmenttoolandapplicationsupport
Investigatingissuesandfixingtoolbugs

S-ar putea să vă placă și