udev.xml 11 KB

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