source: chapter09/udev.xml@ b1774158

12.0 12.0-rc1 12.1 12.1-rc1 multilib 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 b1774158 was b1774158, checked in by Xi Ruoyao <xry111@…>, 12 months ago

Remove references to eudev

  • Property mode set to 100644
File size: 15.8 KB
RevLine 
[bce08ef]1<?xml version="1.0" encoding="ISO-8859-1"?>
[b06ca36]2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
[bce08ef]4 <!ENTITY % general-entities SYSTEM "../general.ent">
5 %general-entities;
6]>
[d781ffb]7
[afcfd74]8<sect1 id="ch-config-udev">
[d781ffb]9 <?dbhtml filename="udev.html"?>
10
[bf58c1e]11 <title>Overview of Device and Module Handling</title>
[d781ffb]12
[afcfd74]13 <indexterm zone="ch-config-udev">
[d781ffb]14 <primary sortas="a-Udev">Udev</primary>
15 <secondary>usage</secondary>
16 </indexterm>
17
[a3d0817]18 <para>In <xref linkend="chapter-building-system"/>, we installed the udev
[b1774158]19 daemon when <phrase revision="sysv">udev</phrase>
[9a0d741]20 <phrase revision="systemd">systemd</phrase> was built. Before we go into the
[6ef5053]21 details regarding how udev works, a brief history of previous methods of
[9a0d741]22 handling devices is in order.</para>
[d781ffb]23
[bf58c1e]24 <para>Linux systems in general traditionally used a static device creation
25 method, whereby a great many device nodes were created under <filename
[d781ffb]26 class="directory">/dev</filename> (sometimes literally thousands of nodes),
[bf58c1e]27 regardless of whether the corresponding hardware devices actually existed. This
[6ef5053]28 was typically done via a <command>MAKEDEV</command> script, which contained a
[d2c332bc]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
[6ef5053]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
[841b723]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>
[d781ffb]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
[6ef5053]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
[bf58c1e]57 class="filesystem">devfs</systemitem> file system also suffered from race
[6ef5053]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
[8d2b5a3]62 from the kernel in June, 2006.</para>
[d2c332bc]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
[6ef5053]67 <systemitem class="filesystem">sysfs</systemitem> is to provide information about
[d2c332bc]68 the system's hardware configuration to userspace processes. With this
[6ef5053]69 userspace-visible representation, it became possible to develop a userspace
70 replacement for <systemitem class="filesystem">devfs</systemitem>.</para>
[d781ffb]71
72 </sect2>
73
74 <sect2>
75 <title>Udev Implementation</title>
76
[d2c332bc]77 <sect3>
78 <title>Sysfs</title>
79
[5cef314]80 <para>The <systemitem class="filesystem">sysfs</systemitem> filesystem
81 was mentioned briefly above. One may wonder how <systemitem
[d2c332bc]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
[6ef5053]84 have been compiled into the kernel register their objects in
[5cef314]85 <systemitem class="filesystem">sysfs</systemitem> (devtmpfs internally)
[6ef5053]86 as they are detected by the kernel. For drivers compiled as modules,
87 registration happens when the module is loaded. Once the <systemitem
[6beb8ce]88 class="filesystem">sysfs</systemitem> filesystem is mounted (on
89 <filename class="directory">/sys</filename>),
[6ef5053]90 data which the drivers have registered with <systemitem
[5cef314]91 class="filesystem">sysfs</systemitem> are available to userspace
92 processes and to udevd for processing (including modifications to device
[2ca8941]93 nodes).</para>
[2a1c1e0]94
[d2c332bc]95 </sect3>
96
[0058818]97 <sect3 id='ch-config-udev-device-node-creation'>
[d2c332bc]98 <title>Device Node Creation</title>
99
[6ef5053]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
[841b723]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
[6ef5053]106 will initially be exposed to userspace with a fixed name, permissions, and
[5cef314]107 owner.</para>
[841b723]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
[d7a9421]112 class="directory">/usr/lib/udev/rules.d</filename>, and <filename
[841b723]113 class="directory">/run/udev/rules.d</filename> directories, <command>
[5cef314]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>
[841b723]117
[bf58c1e]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>
[c34b4fb]123
[36d2dcc]124 <sect3 id="module-loading">
[d2c332bc]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>.
[a3d0817]139 The default rules provided with udev will cause <command>udevd</command>
[61e63d3]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
[d2c332bc]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
[a3d0817]153 protocols, filesystems, and NLS support on demand.</para>
[d2c332bc]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
[a0d96d25]162 generates a uevent. This uevent is then handled by
[d2c332bc]163 <command>udevd</command> as described above.</para>
164
165 </sect3>
[d781ffb]166
167 </sect2>
168
169 <sect2>
[d2c332bc]170 <title>Problems with Loading Modules and Creating Devices</title>
[d781ffb]171
[d2c332bc]172 <para>There are a few possible problems when it comes to automatically
173 creating device nodes.</para>
[d781ffb]174
[d2c332bc]175 <sect3>
[6ef5053]176 <title>A Kernel Module Is Not Loaded Automatically</title>
[d781ffb]177
[d2c332bc]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
[a3d0817]181 arrange module loading by other means. With Linux-&linux-version;, udev is
[a0d96d25]182 known to load properly-written drivers for INPUT, IDE, PCI, USB, SCSI,
[0cda898]183 SERIO, and FireWire devices.</para>
[d781ffb]184
[d2c332bc]185 <para>To determine if the device driver you require has the necessary
[a3d0817]186 support for udev, run <command>modinfo</command> with the module name as
[d2c332bc]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
[a3d0817]194 driver. Load the driver without the help from udev and expect the issue
[d2c332bc]195 to be fixed later.</para>
[d781ffb]196
[d2c332bc]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
[a0d96d25]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>
[d2c332bc]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>
[6ef5053]210 <title>A Kernel Module Is Not Loaded Automatically, and Udev Is Not
211 Intended to Load It</title>
[d2c332bc]212
[0d84af1]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
[a3d0817]218 after udev loads the wrapped module. To do this, add a
[0d84af1]219 <quote>softdep</quote> line to the corresponding
[b67f2d6]220 <filename>/etc/modprobe.d/<replaceable>&lt;filename&gt;</replaceable>.conf</filename>
221 file. For example:</para>
[d2c332bc]222
[841b723]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
[c34b4fb]227 <literal>pre:</literal> and <literal>post:</literal> dependencies. See
[9e7475a]228 the <filename>modprobe.d(5)</filename> manual page for more information
[841b723]229 on <quote>softdep</quote> syntax and capabilities.</para>
[d2c332bc]230
[c34b4fb]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
[9e7475a]233 load this module on system boot. To do this, add the module name to the
[d2c332bc]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>
[6ef5053]240 <title>Udev Loads Some Unwanted Module</title>
[d2c332bc]241
[b67f2d6]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
[d2c332bc]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>
[6ef5053]254 <title>Udev Creates a Device Incorrectly, or Makes the Wrong Symlink</title>
[d2c332bc]255
256 <para>This usually happens if a rule unexpectedly matches a device. For
[edbeeb5]257 example, a poorly-written rule can match both a SCSI disk (as desired)
[d2c332bc]258 and the corresponding SCSI generic device (incorrectly) by vendor.
[61e63d3]259 Find the offending rule and make it more specific, with the help of the
260 <command>udevadm info</command> command.</para>
[d2c332bc]261
262 </sect3>
263
264 <sect3>
[6ef5053]265 <title>Udev Rule Works Unreliably</title>
[d2c332bc]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>
[61e63d3]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>
[d2c332bc]275
276 </sect3>
277
[9a0d741]278 <sect3>
[6ef5053]279 <title>Udev Does Not Create a Device</title>
[9a0d741]280
[6ef5053]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>
[9a0d741]284
[6ef5053]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
[9a0d741]288 with third party drivers from outside the kernel tree. Create a static
[d7a9421]289 device node in <filename>/usr/lib/udev/devices</filename> with the
[9a0d741]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
[d2c332bc]298 <sect3>
[6ef5053]299 <title>Device Naming Order Changes Randomly After Rebooting</title>
[d781ffb]300
[a3d0817]301 <para>This is due to the fact that udev, by design, handles uevents and
[d2c332bc]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
[a3d0817]306 serial number or the output of various *_id utilities installed by udev.
[afcfd74]307 See <xref linkend="ch-config-symlinks"/> and
308 <xref linkend="ch-config-network"/> for examples.</para>
[d2c332bc]309
310 </sect3>
[81fd230]311
[d781ffb]312 </sect2>
[81fd230]313
[d781ffb]314 <sect2>
315 <title>Useful Reading</title>
[81fd230]316
[d781ffb]317 <para>Additional helpful documentation is available at the following
318 sites:</para>
[81fd230]319
[d781ffb]320 <itemizedlist>
[bce08ef]321
[d781ffb]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>
[e3bc7f5]328 <para>The <systemitem class="filesystem">sysfs</systemitem> Filesystem
[fb66c4c]329 <ulink url="https://www.kernel.org/pub/linux/kernel/people/mochel/doc/papers/ols-2005/mochel.pdf"/></para>
[d781ffb]330 </listitem>
331
[5cef314]332<!-- No longer available
[d781ffb]333 <listitem>
[e3bc7f5]334 <para>Pointers to further reading
335 <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html"/>
336 </para>
[d781ffb]337 </listitem>
[5cef314]338-->
[d781ffb]339 </itemizedlist>
340
341 </sect2>
342
343</sect1>
Note: See TracBrowser for help on using the repository browser.