source: chapter07/udev.xml@ 6249eea

Last change on this file since 6249eea was 1775dfc3, checked in by Matthew Burgess <matthew@…>, 19 years ago

Remove an irrelevant example of poor Udev support

git-svn-id: http://svn.linuxfromscratch.org/LFS/branches/udev_update/BOOK@7417 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

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