udev.xml 11 KB

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