udev.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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">ramfs</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
  76. these device nodes when Linux is booted. This script starts with
  77. registering <command>/sbin/udevsend</command> as a hotplug event handler.
  78. Hotplug events (discussed below) should not be generated during this
  79. stage, but <command>udev</command> is registered just in case they do
  80. occur. The <command>udevstart</command> program then walks through
  81. the <systemitem class="filesystem">/sys</systemitem> filesystem and
  82. creates devices under <filename class="directory">/dev</filename> that
  83. match the descriptions. For example,
  84. <filename>/sys/class/tty/vcs/dev</filename> contains the string
  85. <quote>7:0</quote> This string is used by <command>udevstart</command>
  86. to create <filename>/dev/vcs</filename> with major number
  87. <emphasis>7</emphasis> and minor <emphasis>0</emphasis>. The names and
  88. permissions of the nodes created under the
  89. <filename class="directory">/dev</filename> directory are configured according
  90. to the rules specified in the files within the
  91. <filename class="directory">/etc/udev/rules.d/</filename> directory. These are
  92. numbered in a similar fashion to the LFS bootscripts. If
  93. <command>udev</command> can't find a rule for the device it is creating, it will
  94. default permissions to <emphasis>660</emphasis> and ownership to
  95. <emphasis>root:root</emphasis>.</para>
  96. <para>Once the above stage is complete, all devices that were already
  97. present and have compiled-in drivers will be available for use. What
  98. about those devices that have modular drivers?</para>
  99. <para>Earlier, we mentioned the concept of a <quote>hotplug event
  100. handler.</quote> When a new device connection is detected by the
  101. kernel, the kernel will generate a hotplug event and look at the file
  102. <filename>/proc/sys/kernel/hotplug</filename> to find out the
  103. userspace program that handles the device's connection. The
  104. <command>udev</command> initscript registered <command>udevsend</command>
  105. as this handler. When these hotplug events are generated, the kernel
  106. will tell <command>udev</command> to check the <filename
  107. class="directory">/sys</filename> filesystem for the information
  108. pertaining to this new device and create the <filename
  109. class="directory">/dev</filename> entry for it.</para>
  110. <para>This brings us to one problem that exists with
  111. <command>udev</command>, and likewise with <systemitem
  112. class="filesystem">devfs</systemitem> before it. It is commonly
  113. referred to as the <quote>chicken and egg</quote> problem. Most Linux
  114. distributions handle loading modules via entries in
  115. <filename>/etc/modules.conf</filename>. Access to a device node causes
  116. the appropriate kernel module to load. With <command>udev</command>,
  117. this method will not work because the device node does not exist until
  118. the module is loaded. To solve this, the
  119. <command>S05modules</command> bootscript was added to the
  120. LFS-Bootscripts package, along with the
  121. <filename>/etc/sysconfig/modules</filename> file. By
  122. adding module
  123. names to the <filename>modules</filename> file, these modules will be
  124. loaded when the computer is starting up. This allows
  125. <command>udev</command> to detect the devices and create the
  126. appropriate device nodes.</para>
  127. <para>Note that on slower machines or for drivers that create a lot
  128. of device nodes, the process of creating devices may take a few
  129. seconds to complete. This means that some device nodes may not be
  130. immediately accessible.</para>
  131. </sect2>
  132. <sect2>
  133. <title>Handling Hotpluggable/Dynamic Devices</title>
  134. <para>When you plug in a device, such as a Universal Serial Bus (USB) MP3 player, the kernel
  135. recognizes that the device is now connected and generates a hotplug
  136. event. If the driver is already loaded (either because it was compiled
  137. into the kernel or because it was loaded via the
  138. <command>S05modules</command> bootscript), <command>udev</command> will
  139. be called upon to create the relevant device node(s) according to the
  140. <systemitem class="filesystem">sysfs</systemitem> data available in
  141. <filename class="directory">/sys</filename>.</para>
  142. <para>If the driver for the just plugged in device is available as a module but
  143. currently unloaded, the Hotplug package will load the appropriate module
  144. and make this device available by creating the device node(s) for it.</para>
  145. </sect2>
  146. <sect2>
  147. <title>Problems with Creating Devices</title>
  148. <para>There are a few known problems when it comes to automatically creating
  149. devices nodes:</para>
  150. <para>1) A kernel driver may not export its data to <systemitem
  151. class="filesystem">sysfs</systemitem>.</para>
  152. <para>This is most common with third party drivers from outside the
  153. kernel tree. These drivers will not end up having their device nodes
  154. created. Use the
  155. <filename>/etc/sysconfig/createfiles</filename> configuration file to
  156. manually create the devices. Consult the
  157. <filename>devices.txt</filename> file inside the kernel documentation
  158. or the documentation for that driver to find the proper major/minor
  159. numbers.</para>
  160. <para>2) A non-hardware device is required. This is most common with
  161. the Advanced Linux Sound Architecture (ALSA) project's Open Sound
  162. System (OSS) compatibility module. These types of devices can be
  163. handled in one of two ways:</para>
  164. <itemizedlist>
  165. <listitem><para>Adding the module names to
  166. <filename>/etc/sysconfig/modules</filename></para></listitem>
  167. <listitem><para>Using an
  168. <quote>install</quote> line in
  169. <filename>/etc/modprobe.conf</filename>. This tells the
  170. <command>modprobe</command> command <quote>when loading this module,
  171. also load this other module, at the same time.</quote> For example:</para>
  172. <screen><userinput>install snd-pcm modprobe -i snd-pcm ; modprobe \
  173. snd-pcm-oss ; true</userinput></screen>
  174. <para>This will cause the system to load both the
  175. <emphasis>snd-pcm</emphasis> and <emphasis>snd-pcm-oss</emphasis>
  176. modules when any request is made to load the driver
  177. <emphasis>snd-pcm</emphasis>.</para></listitem>
  178. </itemizedlist>
  179. </sect2>
  180. <sect2>
  181. <title>Useful Reading</title>
  182. <para>Additional helpful documentation is available at the following
  183. sites:</para>
  184. <itemizedlist>
  185. <listitem><para>A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
  186. <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para></listitem>
  187. <listitem><para>udev FAQ
  188. <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev-FAQ"/></para></listitem>
  189. <listitem><para>The Linux Kernel Driver Model
  190. <ulink url="http://public.planetmirror.com/pub/lca/2003/proceedings/papers/Patrick_Mochel/Patrick_Mochel.pdf"/></para></listitem>
  191. </itemizedlist>
  192. </sect2>
  193. </sect1>