source: chapter09/udev.xml@ 384d643f

12.0 12.0-rc1 12.1 12.1-rc1 multilib renodr/libudev-from-systemd trunk xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/update-glibc
Last change on this file since 384d643f was 6beb8ce, checked in by David Bryant <david@…>, 18 months ago

Add a missing XML tag (<filename class="directory">).

  • Property mode set to 100644
File size: 15.8 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-config-udev">
9 <?dbhtml filename="udev.html"?>
10
11 <title>Overview of Device and Module Handling</title>
12
13 <indexterm zone="ch-config-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 daemon when <phrase revision="sysv">eudev</phrase>
20 <phrase revision="systemd">systemd</phrase> was built. Before we go into the
21 details regarding how udev works, a brief history of previous methods of
22 handling devices is in order.</para>
23
24 <para>Linux systems in general traditionally used a static device creation
25 method, whereby a great many device nodes were created under <filename
26 class="directory">/dev</filename> (sometimes literally thousands of nodes),
27 regardless of whether the corresponding hardware devices actually existed. This
28 was typically done via a <command>MAKEDEV</command> script, which contained a
29 number of calls to the <command>mknod</command> program with the relevant
30 major and minor device numbers for every possible device that might exist in
31 the world.</para>
32
33 <para>Using the udev method, device nodes are only created for those devices
34 which are detected by the kernel. These device nodes are
35 created each time the system boots; they are stored in a <systemitem
36 class="filesystem">devtmpfs</systemitem> file system (a virtual file system
37 that resides entirely in system memory). Device nodes do not require much
38 space, so the memory that is used is negligible.</para>
39
40 <sect2>
41 <title>History</title>
42
43 <para>In February 2000, a new filesystem called <systemitem
44 class="filesystem">devfs</systemitem> was merged into the 2.3.46 kernel
45 and was made available during the 2.4 series of stable kernels. Although
46 it was present in the kernel source itself, this method of creating devices
47 dynamically never received overwhelming support from the core kernel
48 developers.</para>
49
50 <para>The main problem with the approach adopted by <systemitem
51 class="filesystem">devfs</systemitem> was the way it handled device
52 detection, creation, and naming. The latter issue, that of device node
53 naming, was perhaps the most critical. It is generally accepted that if
54 device names are configurable, the device naming policy
55 should be chosen by system administrators, and not imposed on them by the
56 developer(s). The <systemitem
57 class="filesystem">devfs</systemitem> file system also suffered from race
58 conditions that were inherent in its design; these could not be fixed without a
59 substantial revision of the kernel. <systemitem class="filesystem">devfs</systemitem>
60 was marked as deprecated for a long
61 time, and was finally removed
62 from the kernel in June, 2006.</para>
63
64 <para>With the development of the unstable 2.5 kernel tree, later released
65 as the 2.6 series of stable kernels, a new virtual filesystem called
66 <systemitem class="filesystem">sysfs</systemitem> came to be. The job of
67 <systemitem class="filesystem">sysfs</systemitem> is to provide information about
68 the system's hardware configuration to userspace processes. With this
69 userspace-visible representation, it became possible to develop a userspace
70 replacement for <systemitem class="filesystem">devfs</systemitem>.</para>
71
72 </sect2>
73
74 <sect2>
75 <title>Udev Implementation</title>
76
77 <sect3>
78 <title>Sysfs</title>
79
80 <para>The <systemitem class="filesystem">sysfs</systemitem> filesystem
81 was mentioned briefly above. One may wonder how <systemitem
82 class="filesystem">sysfs</systemitem> knows about the devices present on
83 a system and what device numbers should be used for them. Drivers that
84 have been compiled into the kernel register their objects in
85 <systemitem class="filesystem">sysfs</systemitem> (devtmpfs internally)
86 as they are detected by the kernel. For drivers compiled as modules,
87 registration happens when the module is loaded. Once the <systemitem
88 class="filesystem">sysfs</systemitem> filesystem is mounted (on
89 <filename class="directory">/sys</filename>),
90 data which the drivers have registered with <systemitem
91 class="filesystem">sysfs</systemitem> are available to userspace
92 processes and to udevd for processing (including modifications to device
93 nodes).</para>
94
95 </sect3>
96
97 <sect3 id='ch-config-udev-device-node-creation'>
98 <title>Device Node Creation</title>
99
100 <para>Device files are created by the kernel in the <systemitem
101 class="filesystem">devtmpfs</systemitem> file system. Any driver that
102 wishes to register a device node will use the <systemitem
103 class="filesystem">devtmpfs</systemitem> (via the driver core) to do it.
104 When a <systemitem class="filesystem">devtmpfs</systemitem> instance is
105 mounted on <filename class="directory">/dev</filename>, the device node
106 will initially be exposed to userspace with a fixed name, permissions, and
107 owner.</para>
108
109 <para>A short time later, the kernel will send a uevent to <command>
110 udevd</command>. Based on the rules specified in the files within the
111 <filename class="directory">/etc/udev/rules.d</filename>, <filename
112 class="directory">/usr/lib/udev/rules.d</filename>, and <filename
113 class="directory">/run/udev/rules.d</filename> directories, <command>
114 udevd</command> will create additional symlinks to the device node, or
115 change its permissions, owner, or group, or modify the internal
116 <command>udevd</command> database entry (name) for that object.</para>
117
118 <para>The rules in these three directories are numbered and all three
119 directories are merged together. If <command>udevd</command> can't find a
120 rule for the device it is creating, it will leave the permissions and
121 ownership at whatever <systemitem
122 class="filesystem">devtmpfs</systemitem> used initially.</para> </sect3>
123
124 <sect3 id="module-loading">
125 <title>Module Loading</title>
126
127 <para>Device drivers compiled as modules may have aliases built into them.
128 Aliases are visible in the output of the <command>modinfo</command>
129 program and are usually related to the bus-specific identifiers of devices
130 supported by a module. For example, the <emphasis>snd-fm801</emphasis>
131 driver supports PCI devices with vendor ID 0x1319 and device ID 0x0801,
132 and has an alias of <quote>pci:v00001319d00000801sv*sd*bc04sc01i*</quote>.
133 For most devices, the bus driver exports the alias of the driver that
134 would handle the device via <systemitem
135 class="filesystem">sysfs</systemitem>. E.g., the
136 <filename>/sys/bus/pci/devices/0000:00:0d.0/modalias</filename> file
137 might contain the string
138 <quote>pci:v00001319d00000801sv00001319sd00001319bc04sc01i00</quote>.
139 The default rules provided with udev will cause <command>udevd</command>
140 to call out to <command>/sbin/modprobe</command> with the contents of the
141 <envar>MODALIAS</envar> uevent environment variable (which should be the
142 same as the contents of the <filename>modalias</filename> file in sysfs),
143 thus loading all modules whose aliases match this string after wildcard
144 expansion.</para>
145
146 <para>In this example, this means that, in addition to
147 <emphasis>snd-fm801</emphasis>, the obsolete (and unwanted)
148 <emphasis>forte</emphasis> driver will be loaded if it is
149 available. See below for ways in which the loading of unwanted drivers can
150 be prevented.</para>
151
152 <para>The kernel itself is also able to load modules for network
153 protocols, filesystems, and NLS support on demand.</para>
154
155 </sect3>
156
157 <sect3>
158 <title>Handling Hotpluggable/Dynamic Devices</title>
159
160 <para>When you plug in a device, such as a Universal Serial Bus (USB) MP3
161 player, the kernel recognizes that the device is now connected and
162 generates a uevent. This uevent is then handled by
163 <command>udevd</command> as described above.</para>
164
165 </sect3>
166
167 </sect2>
168
169 <sect2>
170 <title>Problems with Loading Modules and Creating Devices</title>
171
172 <para>There are a few possible problems when it comes to automatically
173 creating device nodes.</para>
174
175 <sect3>
176 <title>A Kernel Module Is Not Loaded Automatically</title>
177
178 <para>Udev will only load a module if it has a bus-specific alias and the
179 bus driver properly exports the necessary aliases to <systemitem
180 class="filesystem">sysfs</systemitem>. In other cases, one should
181 arrange module loading by other means. With Linux-&linux-version;, udev is
182 known to load properly-written drivers for INPUT, IDE, PCI, USB, SCSI,
183 SERIO, and FireWire devices.</para>
184
185 <para>To determine if the device driver you require has the necessary
186 support for udev, run <command>modinfo</command> with the module name as
187 the argument. Now try locating the device directory under
188 <filename class="directory">/sys/bus</filename> and check whether there is
189 a <filename>modalias</filename> file there.</para>
190
191 <para>If the <filename>modalias</filename> file exists in <systemitem
192 class="filesystem">sysfs</systemitem>, the driver supports the device and
193 can talk to it directly, but doesn't have the alias, it is a bug in the
194 driver. Load the driver without the help from udev and expect the issue
195 to be fixed later.</para>
196
197 <para>If there is no <filename>modalias</filename> file in the relevant
198 directory under <filename class="directory">/sys/bus</filename>, this
199 means that the kernel developers have not yet added modalias support to
200 this bus type. With Linux-&linux-version;, this is the case with ISA
201 busses. Expect this issue to be fixed in later kernel versions.</para>
202
203 <para>Udev is not intended to load <quote>wrapper</quote> drivers such as
204 <emphasis>snd-pcm-oss</emphasis> and non-hardware drivers such as
205 <emphasis>loop</emphasis> at all.</para>
206
207 </sect3>
208
209 <sect3>
210 <title>A Kernel Module Is Not Loaded Automatically, and Udev Is Not
211 Intended to Load It</title>
212
213 <para>If the <quote>wrapper</quote> module only enhances the
214 functionality provided by some other module (e.g.,
215 <emphasis>snd-pcm-oss</emphasis> enhances the functionality of
216 <emphasis>snd-pcm</emphasis> by making the sound cards available to OSS
217 applications), configure <command>modprobe</command> to load the wrapper
218 after udev loads the wrapped module. To do this, add a
219 <quote>softdep</quote> line to the corresponding
220 <filename>/etc/modprobe.d/<replaceable>&lt;filename&gt;</replaceable>.conf</filename>
221 file. For example:</para>
222
223<screen role="nodump"><literal>softdep snd-pcm post: snd-pcm-oss</literal></screen>
224
225 <para>Note that the <quote>softdep</quote> command also allows
226 <literal>pre:</literal> dependencies, or a mixture of both
227 <literal>pre:</literal> and <literal>post:</literal> dependencies. See
228 the <filename>modprobe.d(5)</filename> manual page for more information
229 on <quote>softdep</quote> syntax and capabilities.</para>
230
231 <para revision="sysv">If the module in question is not a wrapper and is
232 useful by itself, configure the <command>modules</command> bootscript to
233 load this module on system boot. To do this, add the module name to the
234 <filename>/etc/sysconfig/modules</filename> file on a separate line.
235 This works for wrapper modules too, but is suboptimal in that case.</para>
236
237 </sect3>
238
239 <sect3>
240 <title>Udev Loads Some Unwanted Module</title>
241
242 <para>Either don't build the module, or blacklist it in a
243 <filename>/etc/modprobe.d/blacklist.conf</filename> file as done with the
244 <emphasis>forte</emphasis> module in the example below:</para>
245
246<screen role="nodump"><literal>blacklist forte</literal></screen>
247
248 <para>Blacklisted modules can still be loaded manually with the
249 explicit <command>modprobe</command> command.</para>
250
251 </sect3>
252
253 <sect3>
254 <title>Udev Creates a Device Incorrectly, or Makes the Wrong Symlink</title>
255
256 <para>This usually happens if a rule unexpectedly matches a device. For
257 example, a poorly-written rule can match both a SCSI disk (as desired)
258 and the corresponding SCSI generic device (incorrectly) by vendor.
259 Find the offending rule and make it more specific, with the help of the
260 <command>udevadm info</command> command.</para>
261
262 </sect3>
263
264 <sect3>
265 <title>Udev Rule Works Unreliably</title>
266
267 <para>This may be another manifestation of the previous problem. If not,
268 and your rule uses <systemitem class="filesystem">sysfs</systemitem>
269 attributes, it may be a kernel timing issue, to be fixed in later kernels.
270 For now, you can work around it by creating a rule that waits for the used
271 <systemitem class="filesystem">sysfs</systemitem> attribute and appending
272 it to the <filename>/etc/udev/rules.d/10-wait_for_sysfs.rules</filename>
273 file (create this file if it does not exist). Please notify the LFS
274 Development list if you do so and it helps.</para>
275
276 </sect3>
277
278 <sect3>
279 <title>Udev Does Not Create a Device</title>
280
281 <para>First, be certain that the driver is built into the
282 kernel or already loaded as a module, and that
283 udev isn't creating a misnamed device.</para>
284
285 <para>If a kernel driver does not export its data to
286 <systemitem class="filesystem">sysfs</systemitem>, udev lacks the
287 information needed to create a device node. This is most likely to happen
288 with third party drivers from outside the kernel tree. Create a static
289 device node in <filename>/usr/lib/udev/devices</filename> with the
290 appropriate major/minor numbers (see the file
291 <filename>devices.txt</filename> inside the kernel documentation or the
292 documentation provided by the third party driver vendor). The static
293 device node will be copied to <filename class="directory">/dev</filename>
294 by <command>udev</command>.</para>
295
296 </sect3>
297
298 <sect3>
299 <title>Device Naming Order Changes Randomly After Rebooting</title>
300
301 <para>This is due to the fact that udev, by design, handles uevents and
302 loads modules in parallel, and thus in an unpredictable order. This will
303 never be <quote>fixed</quote>. You should not rely upon the kernel device
304 names being stable. Instead, create your own rules that make symlinks with
305 stable names based on some stable attributes of the device, such as a
306 serial number or the output of various *_id utilities installed by udev.
307 See <xref linkend="ch-config-symlinks"/> and
308 <xref linkend="ch-config-network"/> for examples.</para>
309
310 </sect3>
311
312 </sect2>
313
314 <sect2>
315 <title>Useful Reading</title>
316
317 <para>Additional helpful documentation is available at the following
318 sites:</para>
319
320 <itemizedlist>
321
322 <listitem>
323 <para>A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
324 <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para>
325 </listitem>
326
327 <listitem>
328 <para>The <systemitem class="filesystem">sysfs</systemitem> Filesystem
329 <ulink url="https://www.kernel.org/pub/linux/kernel/people/mochel/doc/papers/ols-2005/mochel.pdf"/></para>
330 </listitem>
331
332<!-- No longer available
333 <listitem>
334 <para>Pointers to further reading
335 <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html"/>
336 </para>
337 </listitem>
338-->
339 </itemizedlist>
340
341 </sect2>
342
343</sect1>
Note: See TracBrowser for help on using the repository browser.