source: chapter07/udev.xml@ 6b13ebc6

10.0 10.0-rc1 10.1 10.1-rc1 11.0 11.0-rc1 11.0-rc2 11.0-rc3 11.1 11.1-rc1 11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 7.1 7.2 7.3 7.4 7.5 7.5-systemd 7.6 7.6-systemd 7.7 7.7-systemd 7.8 7.8-systemd 7.9 7.9-systemd 8.0 8.1 8.2 8.3 8.4 9.0 9.1 arm bdubbs/gcc13 ml-11.0 multilib renodr/libudev-from-systemd s6-init trunk xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/lfs-next xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since 6b13ebc6 was 2a1c1e0, checked in by Bruce Dubbs <bdubbs@…>, 13 years ago

Fix the udev_retry script, and add an explanation
for how to configure it.

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

  • Property mode set to 100644
File size: 17.2 KB
Line 
1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/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 was marked as deprecated for a long
59 period &ndash; due to a lack of maintenance &ndash; and was finally removed
60 from the kernel in June, 2006.</para>
61
62 <para>With the development of the unstable 2.5 kernel tree, later released
63 as the 2.6 series of stable kernels, a new virtual filesystem called
64 <systemitem class="filesystem">sysfs</systemitem> came to be. The job of
65 <systemitem class="filesystem">sysfs</systemitem> is to export a view of
66 the system's hardware configuration to userspace processes. With this
67 userspace-visible representation, the possibility of seeing a userspace
68 replacement for <systemitem class="filesystem">devfs</systemitem> became
69 much more realistic.</para>
70
71 </sect2>
72
73 <sect2>
74 <title>Udev Implementation</title>
75
76 <sect3>
77 <title>Sysfs</title>
78
79 <para>The <systemitem class="filesystem">sysfs</systemitem> filesystem was
80 mentioned briefly above. One may wonder how <systemitem
81 class="filesystem">sysfs</systemitem> knows about the devices present on
82 a system and what device numbers should be used for them. Drivers that
83 have been compiled into the kernel directly register their objects with
84 <systemitem class="filesystem">sysfs</systemitem> as they are detected by
85 the kernel. For drivers compiled as modules, this registration will happen
86 when the module is loaded. Once the <systemitem
87 class="filesystem">sysfs</systemitem> filesystem is mounted (on <filename
88 class="directory">/sys</filename>), data which the built-in drivers
89 registered with <systemitem class="filesystem">sysfs</systemitem> are
90 available to userspace processes and to <command>udevd</command> for device
91 node creation.</para>
92
93 </sect3>
94
95 <sect3>
96 <title>Udev Bootscripts</title>
97
98 <para>The <command>/etc/rc.d/init.d/udev</command> initscript takes care
99 of creating device nodes when Linux is booted. The script unsets the
100 uevent handler from the default of <command>/sbin/hotplug</command>.
101 This is done because the kernel no longer needs to call out to an
102 external binary. Instead <command>udevd</command> will listen on a
103 netlink socket for uevents that the kernel raises. Next, the bootscript
104 copies any static device nodes that exist in <filename
105 class="directory">/lib/udev/devices</filename> to <filename
106 class="directory">/dev</filename>. This is necessary because some
107 devices, directories, and symlinks are needed before the dynamic device
108 handling processes are available during the early stages of booting a
109 system, or are required by <command>udevd</command> itself. Creating
110 static device nodes in <filename
111 class="directory">/lib/udev/devices</filename> also provides an easy
112 workaround for devices that are not supported by the dynamic device
113 handling infrastructure. The bootscript then starts the Udev daemon,
114 <command>udevd</command>, which will act on any uevents it receives.
115 Finally, the bootscript forces the kernel to replay uevents for any
116 devices that have already been registered and then waits for
117 <command>udevd</command> to handle them.</para>
118
119 <para>The <command>/etc/rc.d/init.d/udev_retry</command> initscript takes
120 care of re-triggering events for subsystems whose rules may rely on
121 filesystems that are not mounted until the <command>mountfs</command>
122 script is run (in particular, /usr and /var may cause this). This script
123 runs after the <command>mountfs</command> script, so those rules (if
124 re-triggered) should succeed the second time around. It is configured
125 from the <filename>/etc/sysconfig/udev_retry</filename> file; any words
126 in this file other than comments are considered subsystem names to
127 trigger at retry time. (To find the subsystem of a device, use
128 <command>udevadm info --attribute-walk</command>.)</para>
129
130 </sect3>
131
132 <sect3>
133 <title>Device Node Creation</title>
134
135 <para>To obtain the right major and minor number for a device, Udev
136 relies on the information provided by <systemitem
137 class="filesystem">sysfs</systemitem> in <filename
138 class="directory">/sys</filename>. For example,
139 <filename>/sys/class/tty/vcs/dev</filename> contains the string
140 <quote>7:0</quote>. This string is used by <command>udevd</command> to
141 create a device node with major number <emphasis>7</emphasis> and minor
142 <emphasis>0</emphasis>. The names and permissions of the nodes created
143 under the <filename class="directory">/dev</filename> directory are
144 determined by rules specified in the files within the <filename
145 class="directory">/etc/udev/rules.d/</filename> directory. These are
146 numbered in a similar fashion to the LFS-Bootscripts package. If
147 <command>udevd</command> can't find a rule for the device it is creating,
148 it will default permissions to <emphasis>660</emphasis> and ownership to
149 <emphasis>root:root</emphasis>. Documentation on the syntax of the Udev
150 rules configuration files are available in
151 <filename>/usr/share/doc/udev-&udev-version;/writing_udev_rules/index.html</filename>
152 </para>
153
154 </sect3>
155
156 <sect3>
157 <title>Module Loading</title>
158
159 <para>Device drivers compiled as modules may have aliases built into them.
160 Aliases are visible in the output of the <command>modinfo</command>
161 program and are usually related to the bus-specific identifiers of devices
162 supported by a module. For example, the <emphasis>snd-fm801</emphasis>
163 driver supports PCI devices with vendor ID 0x1319 and device ID 0x0801,
164 and has an alias of <quote>pci:v00001319d00000801sv*sd*bc04sc01i*</quote>.
165 For most devices, the bus driver exports the alias of the driver that
166 would handle the device via <systemitem
167 class="filesystem">sysfs</systemitem>. E.g., the
168 <filename>/sys/bus/pci/devices/0000:00:0d.0/modalias</filename> file
169 might contain the string
170 <quote>pci:v00001319d00000801sv00001319sd00001319bc04sc01i00</quote>.
171 The default rules provided with Udev will cause <command>udevd</command>
172 to call out to <command>/sbin/modprobe</command> with the contents of the
173 <envar>MODALIAS</envar> uevent environment variable (which should be the
174 same as the contents of the <filename>modalias</filename> file in sysfs),
175 thus loading all modules whose aliases match this string after wildcard
176 expansion.</para>
177
178 <para>In this example, this means that, in addition to
179 <emphasis>snd-fm801</emphasis>, the obsolete (and unwanted)
180 <emphasis>forte</emphasis> driver will be loaded if it is
181 available. See below for ways in which the loading of unwanted drivers can
182 be prevented.</para>
183
184 <para>The kernel itself is also able to load modules for network
185 protocols, filesystems and NLS support on demand.</para>
186
187 </sect3>
188
189 <sect3>
190 <title>Handling Hotpluggable/Dynamic Devices</title>
191
192 <para>When you plug in a device, such as a Universal Serial Bus (USB) MP3
193 player, the kernel recognizes that the device is now connected and
194 generates a uevent. This uevent is then handled by
195 <command>udevd</command> as described above.</para>
196
197 </sect3>
198
199 </sect2>
200
201 <sect2>
202 <title>Problems with Loading Modules and Creating Devices</title>
203
204 <para>There are a few possible problems when it comes to automatically
205 creating device nodes.</para>
206
207 <sect3>
208 <title>A kernel module is not loaded automatically</title>
209
210 <para>Udev will only load a module if it has a bus-specific alias and the
211 bus driver properly exports the necessary aliases to <systemitem
212 class="filesystem">sysfs</systemitem>. In other cases, one should
213 arrange module loading by other means. With Linux-&linux-version;, Udev is
214 known to load properly-written drivers for INPUT, IDE, PCI, USB, SCSI,
215 SERIO, and FireWire devices.</para>
216
217 <para>To determine if the device driver you require has the necessary
218 support for Udev, run <command>modinfo</command> with the module name as
219 the argument. Now try locating the device directory under
220 <filename class="directory">/sys/bus</filename> and check whether there is
221 a <filename>modalias</filename> file there.</para>
222
223 <para>If the <filename>modalias</filename> file exists in <systemitem
224 class="filesystem">sysfs</systemitem>, the driver supports the device and
225 can talk to it directly, but doesn't have the alias, it is a bug in the
226 driver. Load the driver without the help from Udev and expect the issue
227 to be fixed later.</para>
228
229 <para>If there is no <filename>modalias</filename> file in the relevant
230 directory under <filename class="directory">/sys/bus</filename>, this
231 means that the kernel developers have not yet added modalias support to
232 this bus type. With Linux-&linux-version;, this is the case with ISA
233 busses. Expect this issue to be fixed in later kernel versions.</para>
234
235 <para>Udev is not intended to load <quote>wrapper</quote> drivers such as
236 <emphasis>snd-pcm-oss</emphasis> and non-hardware drivers such as
237 <emphasis>loop</emphasis> at all.</para>
238
239 </sect3>
240
241 <sect3>
242 <title>A kernel module is not loaded automatically, and Udev is not
243 intended to load it</title>
244
245 <para>If the <quote>wrapper</quote> module only enhances the functionality
246 provided by some other module (e.g., <emphasis>snd-pcm-oss</emphasis>
247 enhances the functionality of <emphasis>snd-pcm</emphasis> by making the
248 sound cards available to OSS applications), configure
249 <command>modprobe</command> to load the wrapper after Udev loads the
250 wrapped module. To do this, add an <quote>install</quote> line in any
251 <filename>/etc/modprobe.d/<replaceable>&lt;filename&gt;</replaceable>.conf</filename>
252 file. For example:</para>
253
254<screen role="nodump"><literal>install snd-pcm /sbin/modprobe -i snd-pcm ; \
255 /sbin/modprobe snd-pcm-oss ; true</literal></screen>
256
257 <para>If the module in question is not a wrapper and is useful by itself,
258 configure the <command>modules</command> bootscript to load this
259 module on system boot. To do this, add the module name to the
260 <filename>/etc/sysconfig/modules</filename> file on a separate line.
261 This works for wrapper modules too, but is suboptimal in that case.</para>
262
263 </sect3>
264
265 <sect3>
266 <title>Udev loads some unwanted module</title>
267
268 <para>Either don't build the module, or blacklist it in a
269 <filename>/etc/modprobe.d/blacklist.conf</filename> file as done with the
270 <emphasis>forte</emphasis> module in the example below:</para>
271
272<screen role="nodump"><literal>blacklist forte</literal></screen>
273
274 <para>Blacklisted modules can still be loaded manually with the
275 explicit <command>modprobe</command> command.</para>
276
277 </sect3>
278
279 <sect3>
280 <title>Udev creates a device incorrectly, or makes a wrong symlink</title>
281
282 <para>This usually happens if a rule unexpectedly matches a device. For
283 example, a poorly-writen rule can match both a SCSI disk (as desired)
284 and the corresponding SCSI generic device (incorrectly) by vendor.
285 Find the offending rule and make it more specific, with the help of the
286 <command>udevadm info</command> command.</para>
287
288 </sect3>
289
290 <sect3>
291 <title>Udev rule works unreliably</title>
292
293 <para>This may be another manifestation of the previous problem. If not,
294 and your rule uses <systemitem class="filesystem">sysfs</systemitem>
295 attributes, it may be a kernel timing issue, to be fixed in later kernels.
296 For now, you can work around it by creating a rule that waits for the used
297 <systemitem class="filesystem">sysfs</systemitem> attribute and appending
298 it to the <filename>/etc/udev/rules.d/10-wait_for_sysfs.rules</filename>
299 file (create this file if it does not exist). Please notify the LFS
300 Development list if you do so and it helps.</para>
301
302 </sect3>
303
304 <sect3>
305 <title>Udev does not create a device</title>
306
307 <para>Further text assumes that the driver is built statically into the
308 kernel or already loaded as a module, and that you have already checked
309 that Udev doesn't create a misnamed device.</para>
310
311 <para>Udev has no information needed to create a device node if a kernel
312 driver does not export its data to <systemitem
313 class="filesystem">sysfs</systemitem>.
314 This is most common with third party drivers from outside the kernel
315 tree. Create a static device node in
316 <filename>/lib/udev/devices</filename> with the appropriate major/minor
317 numbers (see the file <filename>devices.txt</filename> inside the kernel
318 documentation or the documentation provided by the third party driver
319 vendor). The static device node will be copied to
320 <filename class="directory">/dev</filename> by the
321 <command>udev</command> bootscript.</para>
322
323 </sect3>
324
325 <sect3>
326 <title>Device naming order changes randomly after rebooting</title>
327
328 <para>This is due to the fact that Udev, by design, handles uevents and
329 loads modules in parallel, and thus in an unpredictable order. This will
330 never be <quote>fixed</quote>. You should not rely upon the kernel device
331 names being stable. Instead, create your own rules that make symlinks with
332 stable names based on some stable attributes of the device, such as a
333 serial number or the output of various *_id utilities installed by Udev.
334 See <xref linkend="ch-scripts-symlinks"/> and
335 <xref linkend="ch-scripts-network"/> for examples.</para>
336
337 </sect3>
338
339 </sect2>
340
341 <sect2>
342 <title>Useful Reading</title>
343
344 <para>Additional helpful documentation is available at the following
345 sites:</para>
346
347 <itemizedlist>
348
349 <listitem>
350 <para>A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
351 <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para>
352 </listitem>
353
354 <listitem>
355 <para>The <systemitem class="filesystem">sysfs</systemitem> Filesystem
356 <ulink url="http://www.kernel.org/pub/linux/kernel/people/mochel/doc/papers/ols-2005/mochel.pdf"/></para>
357 </listitem>
358
359 <listitem>
360 <para>Pointers to further reading
361 <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html"/>
362 </para>
363 </listitem>
364
365 </itemizedlist>
366
367 </sect2>
368
369</sect1>
Note: See TracBrowser for help on using the repository browser.