udev.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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
  22. is typically done via a <command>MAKEDEV</command> script, which contains a
  23. number of calls to the <command>mknod</command> program with the relevant
  24. major and minor device numbers for every possible device that might exist in
  25. the world.</para>
  26. <para>Using the Udev method, only those devices which are detected by the
  27. kernel get device nodes created for them. Because these device nodes will be
  28. created each time the system boots, they will be stored on a <systemitem
  29. class="filesystem">tmpfs</systemitem> file system (a virtual file system that
  30. resides entirely in system memory). Device nodes do not require much space, so
  31. the memory that is used is negligible.</para>
  32. <sect2>
  33. <title>History</title>
  34. <para>In February 2000, a new filesystem called <systemitem
  35. class="filesystem">devfs</systemitem> was merged into the 2.3.46 kernel
  36. and was made available during the 2.4 series of stable kernels. Although
  37. it was present in the kernel source itself, this method of creating devices
  38. dynamically never received overwhelming support from the core kernel
  39. developers.</para>
  40. <para>The main problem with the approach adopted by <systemitem
  41. class="filesystem">devfs</systemitem> was the way it handled device
  42. detection, creation, and naming. The latter issue, that of device node
  43. naming, was perhaps the most critical. It is generally accepted that if
  44. device names are allowed to be configurable, then the device naming policy
  45. should be up to a system administrator, not imposed on them by any
  46. particular developer(s). The <systemitem
  47. class="filesystem">devfs</systemitem> file system also suffers from race
  48. conditions that are inherent in its design and cannot be fixed without a
  49. substantial revision to the kernel. It has also been marked as deprecated
  50. due to a lack of recent maintenance.</para>
  51. <para>With the development of the unstable 2.5 kernel tree, later released
  52. as the 2.6 series of stable kernels, a new virtual filesystem called
  53. <systemitem class="filesystem">sysfs</systemitem> came to be. The job of
  54. <systemitem class="filesystem">sysfs</systemitem> is to export a view of
  55. the system's hardware configuration to userspace processes. With this
  56. userspace-visible representation, the possibility of seeing a userspace
  57. replacement for <systemitem class="filesystem">devfs</systemitem> became
  58. much more realistic.</para>
  59. </sect2>
  60. <sect2>
  61. <title>Udev Implementation</title>
  62. <sect3>
  63. <title>Sysfs</title>
  64. <para>The <systemitem class="filesystem">sysfs</systemitem> filesystem was
  65. mentioned briefly above. One may wonder how <systemitem
  66. class="filesystem">sysfs</systemitem> knows about the devices present on
  67. a system and what device numbers should be used for them. Drivers that
  68. have been compiled into the kernel directly register their objects with
  69. <systemitem class="filesystem">sysfs</systemitem> as they are detected by
  70. the kernel. For drivers compiled as modules, this registration will happen
  71. when the module is loaded. Once the <systemitem
  72. class="filesystem">sysfs</systemitem> filesystem is mounted (on <filename
  73. class="directory">/sys</filename>), data which the built-in drivers
  74. registered with <systemitem class="filesystem">sysfs</systemitem> are
  75. available to userspace processes and to <command>udevd</command> for device
  76. node creation.</para>
  77. </sect3>
  78. <sect3>
  79. <title>Udev Bootscript</title>
  80. <para>The <command>S10udev</command> initscript takes care of creating
  81. device nodes when Linux is booted. The script unsets the uevent handler
  82. from the default of <command>/sbin/hotplug</command>. This is done
  83. because the kernel no longer needs to call out to an external binary.
  84. Instead <command>udevd</command> will listen on a netlink socket for
  85. uevents that the kernel raises. Next, the bootscript copies any static
  86. device nodes that exist in <filename
  87. class="directory">/lib/udev/devices</filename> to <filename
  88. class="directory">/dev</filename>. This is necessary because some devices,
  89. directories, and symlinks are needed before the dynamic device handling
  90. processes are available during the early stages of booting a system.
  91. Creating static device nodes in <filename
  92. class="directory">/lib/udev/devices</filename> also provides an easy
  93. workaround for devices that are not supported by the dynamic device
  94. handling infrastructure. The bootscript then starts the Udev daemon,
  95. <command>udevd</command>, which will act on any uevents it receives.
  96. Finally, the bootscript forces the kernel to replay uevents for any
  97. devices that have already been registered and then waits for
  98. <command>udevd</command> to handle them.</para>
  99. </sect3>
  100. <sect3>
  101. <title>Device Node Creation</title>
  102. <para>To obtain the right major and minor number for a device, Udev relies
  103. on the information provided by <systemitem
  104. class="filesystem">sysfs</systemitem> in <filename
  105. class="directory">/sys</filename>. For example,
  106. <filename>/sys/class/tty/vcs/dev</filename> contains the string
  107. <quote>7:0</quote>. This string is used by <command>udevd</command>
  108. to create a device node with major number <emphasis>7</emphasis> and minor
  109. <emphasis>0</emphasis>. The names and permissions of the nodes created
  110. under the <filename class="directory">/dev</filename> directory are
  111. determined by rules specified in the files within the <filename
  112. class="directory">/etc/udev/rules.d/</filename> directory. These are
  113. numbered in a similar fashion to the LFS-Bootscripts package. If
  114. <command>udevd</command> can't find a rule for the device it is creating,
  115. it will default permissions to <emphasis>660</emphasis> and ownership to
  116. <emphasis>root:root</emphasis>. Documentation on the syntax of the Udev
  117. rules configuration files are available in
  118. <filename>/usr/share/doc/udev-&udev-version;/index.html</filename></para>
  119. </sect3>
  120. <sect3>
  121. <title>Module Loading</title>
  122. <para>Device drivers compiled as modules may have aliases built into them.
  123. Aliases are visible in the output of the <command>modinfo</command>
  124. program and are usually related to the bus-specific identifiers of devices
  125. supported by a module. For example, the <emphasis>snd-fm801</emphasis>
  126. driver supports PCI devices with vendor ID 0x1319 and device ID 0x0801,
  127. and has an alias of <quote>pci:v00001319d00000801sv*sd*bc04sc01i*</quote>.
  128. For most devices, the bus driver exports the alias of the driver that
  129. would handle the device via <systemitem
  130. class="filesystem">sysfs</systemitem>. E.g., the
  131. <filename>/sys/bus/pci/devices/0000:00:0d.0/modalias</filename> file
  132. might contain the string
  133. <quote>pci:v00001319d00000801sv00001319sd00001319bc04sc01i00</quote>.
  134. The rules that LFS installs will cause <command>udevd</command> to call
  135. out to <command>/sbin/modprobe</command> with the contents of the
  136. <envar>MODALIAS</envar> uevent environment variable (that should be the
  137. same as the contents of the <filename>modalias</filename> file in sysfs),
  138. thus loading all modules whose aliases match this string after wildcard
  139. expansion.</para>
  140. <para>In this example, this means that, in addition to
  141. <emphasis>snd-fm801</emphasis>, the obsolete (and unwanted)
  142. <emphasis>forte</emphasis> driver will be loaded if it is
  143. available. See below for ways in which the loading of unwanted drivers can
  144. be prevented.</para>
  145. <para>The kernel itself is also able to load modules for network
  146. protocols, filesystems and NLS support on demand.</para>
  147. </sect3>
  148. <sect3>
  149. <title>Handling Hotpluggable/Dynamic Devices</title>
  150. <para>When you plug in a device, such as a Universal Serial Bus (USB) MP3
  151. player, the kernel recognizes that the device is now connected and
  152. generates a uevent. This uevent is then handled by
  153. <command>udevd</command> as described above.</para>
  154. </sect3>
  155. </sect2>
  156. <sect2>
  157. <title>Problems with Loading Modules and Creating Devices</title>
  158. <para>There are a few possible problems when it comes to automatically
  159. creating device nodes.</para>
  160. <sect3>
  161. <title>A kernel module is not loaded automatically</title>
  162. <para>Udev will only load a module if it has a bus-specific alias and the
  163. bus driver properly exports the necessary aliases to <systemitem
  164. class="filesystem">sysfs</systemitem>. In other cases, one should
  165. arrange module loading by other means. With Linux-&linux-version;, Udev is
  166. known to load properly-written drivers for INPUT, IDE, PCI, USB, SCSI,
  167. SERIO and FireWire devices.</para>
  168. <para>To determine if the device driver you require has the necessary
  169. support for Udev, run <command>modinfo</command> with the module name as
  170. the argument. Now try locating the device directory under
  171. <filename class="directory">/sys/bus</filename> and check whether there is
  172. a <filename>modalias</filename> file there.</para>
  173. <para>If the <filename>modalias</filename> file exists in <systemitem
  174. class="filesystem">sysfs</systemitem>, the driver supports the device and
  175. can talk to it directly, but doesn't have the alias, it is a bug in the
  176. driver. Load the driver without the help from Udev and expect the issue
  177. to be fixed later.</para>
  178. <para>If there is no <filename>modalias</filename> file in the relevant
  179. directory under <filename class="directory">/sys/bus</filename>, this
  180. means that the kernel developers have not yet added modalias support to
  181. this bus type. With Linux-&linux-version;, this is the case with ISA
  182. busses. Expect this issue to be fixed in later kernel versions.</para>
  183. <para>Udev is not intended to load <quote>wrapper</quote> drivers such as
  184. <emphasis>snd-pcm-oss</emphasis> and non-hardware drivers such as
  185. <emphasis>loop</emphasis> at all.</para>
  186. </sect3>
  187. <sect3>
  188. <title>A kernel module is not loaded automatically, and Udev is not
  189. intended to load it</title>
  190. <para>If the <quote>wrapper</quote> module only enhances the functionality
  191. provided by some other module (e.g., <emphasis>snd-pcm-oss</emphasis>
  192. enhances the functionality of <emphasis>snd-pcm</emphasis> by making the
  193. sound cards available to OSS applications), configure
  194. <command>modprobe</command> to load the wrapper after Udev loads the
  195. wrapped module. To do this, add an <quote>install</quote> line in
  196. <filename>/etc/modprobe.conf</filename>. For example:</para>
  197. <screen role="nodump"><literal>install snd-pcm /sbin/modprobe -i snd-pcm ; \
  198. /sbin/modprobe snd-pcm-oss ; true</literal></screen>
  199. <para>If the module in question is not a wrapper and is useful by itself,
  200. configure the <command>S05modules</command> bootscript to load this
  201. module on system boot. To do this, add the module name to the
  202. <filename>/etc/sysconfig/modules</filename> file on a separate line.
  203. This works for wrapper modules too, but is suboptimal in that case.</para>
  204. </sect3>
  205. <sect3>
  206. <title>Udev loads some unwanted module</title>
  207. <para>Either don't build the module, or blacklist it in
  208. <filename>/etc/modprobe.conf</filename> file as done with the
  209. <emphasis>forte</emphasis> module in the example below:</para>
  210. <screen role="nodump"><literal>blacklist forte</literal></screen>
  211. <para>Blacklisted modules can still be loaded manually with the
  212. explicit <command>modprobe</command> command.</para>
  213. </sect3>
  214. <sect3>
  215. <title>Udev creates a device incorrectly, or makes a wrong symlink</title>
  216. <para>This usually happens if a rule unexpectedly matches a device. For
  217. example, a poorly-writen rule can match both a SCSI disk (as desired)
  218. and the corresponding SCSI generic device (incorrectly) by vendor.
  219. Find the offending rule and make it more specific.</para>
  220. </sect3>
  221. <sect3>
  222. <title>Udev rule works unreliably</title>
  223. <para>This may be another manifestation of the previous problem. If not,
  224. and your rule uses <systemitem class="filesystem">sysfs</systemitem>
  225. attributes, it may be a kernel timing issue, to be fixed in later kernels.
  226. For now, you can work around it by creating a rule that waits for the used
  227. <systemitem class="filesystem">sysfs</systemitem> attribute and appending
  228. it to the <filename>/etc/udev/rules.d/10-wait_for_sysfs.rules</filename>
  229. file. Please notify the LFS Development list if you do so and it
  230. helps.</para>
  231. </sect3>
  232. <sect3>
  233. <title>Udev does not create a device</title>
  234. <para>Further text assumes that the driver is built statically into the
  235. kernel or already loaded as a module, and that you have already checked
  236. that Udev doesn't create a misnamed device.</para>
  237. <para>Udev has no information needed to create a device node if a kernel
  238. driver does not export its data to <systemitem
  239. class="filesystem">sysfs</systemitem>.
  240. This is most common with third party drivers from outside the kernel
  241. tree. Create a static device node in
  242. <filename>/lib/udev/devices</filename> with the appropriate major/minor
  243. numbers (see the file <filename>devices.txt</filename> inside the kernel
  244. documentation or the documentation provided by the third party driver
  245. vendor). The static device node will be copied to
  246. <filename class="directory">/dev</filename> by the
  247. <command>S10udev</command> bootscript.</para>
  248. </sect3>
  249. <sect3>
  250. <title>Device naming order changes randomly after rebooting</title>
  251. <para>This is due to the fact that Udev, by design, handles uevents and
  252. loads modules in parallel, and thus in an unpredictable order. This will
  253. never be <quote>fixed</quote>. You should not rely upon the kernel device
  254. names being stable. Instead, create your own rules that make symlinks with
  255. stable names based on some stable attributes of the device, such as a
  256. serial number or the output of various *_id utilities installed by Udev.
  257. See <xref linkend="ch-scripts-symlinks"/> and
  258. <xref linkend="ch-scripts-network"/> for examples.</para>
  259. </sect3>
  260. </sect2>
  261. <sect2>
  262. <title>Useful Reading</title>
  263. <para>Additional helpful documentation is available at the following
  264. sites:</para>
  265. <itemizedlist>
  266. <listitem>
  267. <para>A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
  268. <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para>
  269. </listitem>
  270. <listitem>
  271. <para>udev FAQ
  272. <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev-FAQ"/></para>
  273. </listitem>
  274. <listitem>
  275. <para>The <systemitem class="filesystem">sysfs</systemitem> Filesystem
  276. <ulink url="http://www.kernel.org/pub/linux/kernel/people/mochel/doc/papers/ols-2005/mochel.pdf"/></para>
  277. </listitem>
  278. </itemizedlist>
  279. </sect2>
  280. </sect1>