udev.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!ENTITY % general-entities SYSTEM "../general.ent">
  5. %general-entities;
  6. ]>
  7. <sect1 id="ch-scripts-udev">
  8. <?dbhtml filename="udev.html"?>
  9. <title>Device and Module Handling on an LFS System</title>
  10. <indexterm zone="ch-scripts-udev">
  11. <primary sortas="a-Udev">Udev</primary>
  12. <secondary>usage</secondary>
  13. </indexterm>
  14. <para>In <xref linkend="chapter-building-system"/>, we installed the Udev
  15. package. Before we go into the details regarding how this works,
  16. a brief history of previous methods of handling devices is in
  17. order.</para>
  18. <para>Linux systems in general traditionally use a static device creation
  19. method, whereby a great many device nodes are created under <filename
  20. class="directory">/dev</filename> (sometimes literally thousands of nodes),
  21. regardless of whether the corresponding hardware devices actually exist. This is
  22. typically done via a <command>MAKEDEV</command> script, which contains a number
  23. of calls to the <command>mknod</command> program with the relevant major and
  24. minor device numbers for every possible device that might exist in the world.
  25. Using the Udev method, only those devices which are detected by the kernel get
  26. device nodes created for them. Because these device nodes will be created each
  27. time the system boots, they will be stored on a <systemitem
  28. class="filesystem">tmpfs</systemitem> file system (a virtual file system that
  29. resides entirely in system memory). Device nodes do not require much space, so
  30. the memory that is used is negligible.</para>
  31. <sect2>
  32. <title>History</title>
  33. <para>In February 2000, a new filesystem called <systemitem
  34. class="filesystem">devfs</systemitem> was merged into the 2.3.46 kernel
  35. and was made available during the 2.4 series of stable kernels. Although
  36. it was present in the kernel source itself, this method of creating devices
  37. dynamically never received overwhelming support from the core kernel
  38. developers.</para>
  39. <para>The main problem with the approach adopted by <systemitem
  40. class="filesystem">devfs</systemitem> was the way it handled device
  41. detection, creation, and naming. The latter issue, that of device node
  42. naming, was perhaps the most critical. It is generally accepted that if
  43. device names are allowed to be configurable, then the device naming policy
  44. should be up to a system administrator, not imposed on them by any
  45. particular developer(s). The <systemitem class="filesystem">devfs</systemitem>
  46. file system also suffers from race conditions that are inherent in its design
  47. and cannot be fixed without a substantial revision to the kernel. It has also
  48. been marked as deprecated due to a lack of recent maintenance.</para>
  49. <para>With the development of the unstable 2.5 kernel tree, later released as
  50. the 2.6 series of stable kernels, a new virtual filesystem called <systemitem
  51. class="filesystem">sysfs</systemitem> came to be. The job of <systemitem
  52. class="filesystem">sysfs</systemitem> is to export a view of the system's
  53. hardrware configuration to userspace processes. With this userspace-visible
  54. representation, the possibility of seeing a userspace replacement for
  55. <systemitem class="filesystem">devfs</systemitem> became much more
  56. realistic.</para>
  57. </sect2>
  58. <sect2>
  59. <title>Udev Implementation</title>
  60. <para>The <systemitem class="filesystem">sysfs</systemitem> filesystem was
  61. mentioned briefly above. One may wonder how <systemitem
  62. class="filesystem">sysfs</systemitem> knows about the devices present on
  63. a system and what device numbers should be used for them. Drivers that have
  64. been compiled into the kernel directly register their objects with
  65. <systemitem class="filesystem">sysfs</systemitem> as they are detected by
  66. the kernel. For drivers compiled as modules, this registration will happen
  67. when the module is loaded. Once the <systemitem
  68. class="filesystem">sysfs</systemitem> filesystem is mounted (on <filename
  69. class="directory">/sys</filename>), data which the built-in drivers
  70. registered with <systemitem class="filesystem">sysfs</systemitem> are
  71. available to userspace processes and to <command>udev</command> for device
  72. node creation.</para>
  73. <para>The <command>S10udev</command> initscript takes care of creating
  74. these device nodes when Linux is booted. This script starts by registering
  75. <command>/sbin/udevsend</command> as a hotplug event handler. Hotplug events
  76. (discussed below) are not usually generated during this stage, but
  77. <command>udev</command> is registered just in case they do occur. The
  78. <command>udevstart</command> program then walks through the <systemitem
  79. class="filesystem">/sys</systemitem> filesystem and creates devices under
  80. <filename class="directory">/dev</filename> that match the descriptions.
  81. For example, <filename>/sys/class/tty/vcs/dev</filename> contains the
  82. string <quote>7:0</quote> This string is used by <command>udevstart</command>
  83. to create <filename>/dev/vcs</filename> with major number
  84. <emphasis>7</emphasis> and minor <emphasis>0</emphasis>. The names and
  85. permissions of the nodes created under the <filename
  86. class="directory">/dev</filename> directory are configured according to the
  87. rules specified in the files within the <filename
  88. class="directory">/etc/udev/rules.d/</filename> directory. These are
  89. numbered in a similar fashion to the LFS-Bootscripts package. If
  90. <command>udev</command> can't find a rule for the device it is creating,
  91. it will default permissions to <emphasis>660</emphasis> and ownership to
  92. <emphasis>root:root</emphasis>.</para>
  93. <para>Once the above stage is complete, all devices that were already present
  94. and have compiled-in drivers will be available for use. This leads us to the
  95. devices that have modular drivers.</para>
  96. <para>Earlier, we mentioned the concept of a <quote>hotplug event
  97. handler.</quote> When a new device connection is detected by the kernel,
  98. the kernel will generate a hotplug event and look at the file
  99. <filename>/proc/sys/kernel/hotplug</filename> to determine the userspace
  100. program that handles the device's connection. The <command>udev</command>
  101. bootscript registered <command>udevsend</command> as this handler. When
  102. these hotplug events are generated, the kernel will tell
  103. <command>udev</command> to check the <filename
  104. class="directory">/sys</filename> filesystem for the information pertaining
  105. to this new device and create the <filename class="directory">/dev</filename>
  106. entry for it.</para>
  107. <para>This brings us to one problem that exists with <command>udev</command>,
  108. and likewise with <systemitem class="filesystem">devfs</systemitem> before it.
  109. It is commonly referred to as the <quote>chicken and egg</quote> problem. Most
  110. Linux distributions handle loading modules via entries in
  111. <filename>/etc/modules.conf</filename>. Access to a device node causes the
  112. appropriate kernel module to load. With <command>udev</command>, this method
  113. will not work because the device node does not exist until the module is loaded.
  114. To solve this, the <command>S05modules</command> bootscript was added to the
  115. LFS-Bootscripts package, along with the
  116. <filename>/etc/sysconfig/modules</filename> file. By adding module names to the
  117. <filename>modules</filename> file, these modules will be loaded when the
  118. computer starts up. This allows <command>udev</command> to detect the devices
  119. and create the appropriate device nodes.</para>
  120. <para>Note that on slower machines or for drivers that create a lot of device
  121. nodes, the process of creating devices may take a few seconds to complete.
  122. This means that some device nodes may not be immediately accessible.</para>
  123. </sect2>
  124. <sect2>
  125. <title>Handling Hotpluggable/Dynamic Devices</title>
  126. <para>When you plug in a device, such as a Universal Serial Bus (USB) MP3
  127. player, the kernel recognizes that the device is now connected and generates
  128. a hotplug event. If the driver is already loaded (either because it was
  129. compiled into the kernel or because it was loaded via the
  130. <command>S05modules</command> bootscript), <command>udev</command> will be
  131. called upon to create the relevant device node(s) according to the
  132. <systemitem class="filesystem">sysfs</systemitem> data available in
  133. <filename class="directory">/sys</filename>.</para>
  134. <para>If the driver for the just plugged in device is available as a module but
  135. currently unloaded, the Hotplug package will load the appropriate module
  136. and make this device available by creating the device node(s) for it.</para>
  137. </sect2>
  138. <sect2>
  139. <title>Problems with Creating Devices</title>
  140. <para>There are a few known problems when it comes to automatically creating
  141. device nodes:</para>
  142. <para>1) A kernel driver may not export its data to <systemitem
  143. class="filesystem">sysfs</systemitem>.</para>
  144. <para>This is most common with third party drivers from outside the kernel
  145. tree. Udev will be unable to automatically create device nodes for such
  146. drivers. Use the <filename>/etc/sysconfig/createfiles</filename>
  147. configuration file to manually create the devices. Consult the
  148. <filename>devices.txt</filename> file inside the kernel documentation or
  149. the documentation for that driver to find the proper major/minor
  150. numbers.</para>
  151. <para>2) A non-hardware device is required. This is most common with
  152. the Advanced Linux Sound Architecture (ALSA) project's Open Sound
  153. System (OSS) compatibility module. These types of devices can be
  154. handled in one of two ways:</para>
  155. <itemizedlist>
  156. <listitem>
  157. <para>Adding the module names to
  158. <filename>/etc/sysconfig/modules</filename></para>
  159. </listitem>
  160. <listitem>
  161. <para>Using an <quote>install</quote> line in
  162. <filename>/etc/modprobe.conf</filename>. This tells the
  163. <command>modprobe</command> command <quote>when loading this module,
  164. also load this other module, at the same time.</quote>
  165. For example:</para>
  166. <screen role="nodump"><userinput>install snd-pcm modprobe -i snd-pcm ; modprobe \
  167. snd-pcm-oss ; true</userinput></screen>
  168. <para>This will cause the system to load both the
  169. <emphasis>snd-pcm</emphasis> and <emphasis>snd-pcm-oss</emphasis>
  170. modules when any request is made to load the driver
  171. <emphasis>snd-pcm</emphasis>.</para>
  172. </listitem>
  173. </itemizedlist>
  174. </sect2>
  175. <sect2>
  176. <title>Useful Reading</title>
  177. <para>Additional helpful documentation is available at the following
  178. sites:</para>
  179. <itemizedlist>
  180. <listitem>
  181. <para>A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
  182. <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para>
  183. </listitem>
  184. <listitem>
  185. <para>udev FAQ
  186. <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev-FAQ"/></para>
  187. </listitem>
  188. <listitem>
  189. <para>The Linux Kernel Driver Model
  190. <ulink url="http://public.planetmirror.com/pub/lca/2003/proceedings/papers/Patrick_Mochel/Patrick_Mochel.pdf"/></para>
  191. </listitem>
  192. </itemizedlist>
  193. </sect2>
  194. </sect1>