udev.xml 11 KB

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