source: BOOK/system-config/common/udev.xml@ 58d3e6ff

clfs-3.0.0-systemd clfs-3.0.0-sysvinit systemd sysvinit
Last change on this file since 58d3e6ff was fc14a7d, checked in by Chris Staub <chris@…>, 11 years ago

Text update

  • Property mode set to 100644
File size: 14.3 KB
RevLine 
[806d0c2]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 a CLFS System</title>
12
13 <indexterm zone="ch-scripts-udev">
[e6e9c33]14 <primary sortas="a-systemd">Systemd</primary>
15 <secondary>udev usage</secondary>
[806d0c2]16 </indexterm>
17
[fc14a7d]18 <para>In <xref linkend="chapter-building-system"/>, we installed systemd,
19 which contains systemd-udevd, previously known as Udev. Before we go into
20 the details regarding how this works, a brief history of previous methods of
21 handling devices is in order.</para>
[806d0c2]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
92 </sect3>
93
94 <sect3>
95 <title>Device Node Creation</title>
96
97 <para>To obtain the right major and minor number for a device, Udev relies
98 on the information provided by <systemitem
99 class="filesystem">sysfs</systemitem> in <filename
100 class="directory">/sys</filename>. For example,
101 <filename>/sys/class/tty/vcs/dev</filename> contains the string
102 <quote>7:0</quote>. This string is used by <command>udevd</command>
103 to create a device node with major number <emphasis>7</emphasis> and minor
104 <emphasis>0</emphasis>. The names and permissions of the nodes created
105 under the <filename class="directory">/dev</filename> directory are
[416772a]106 determined by rules specified in the files within the
107 <filename class="directory">/lib/udev/rules.d</filename> and <filename
108 class="directory">/etc/udev/rules.d/</filename> directories. These files
109 have names that start with numbers, and are evaluated in numerical order.
110 If <command>udevd</command> can't find a rule for the device it is
111 creating, it will default permissions to <emphasis>660</emphasis> and
112 ownership to <emphasis>root:root</emphasis>. </para>
[806d0c2]113
114 </sect3>
115
116 <sect3>
117 <title>Module Loading</title>
118
119 <para>Device drivers compiled as modules may have aliases built into them.
120 Aliases are visible in the output of the <command>modinfo</command>
121 program and are usually related to the bus-specific identifiers of devices
122 supported by a module. For example, the <emphasis>snd-fm801</emphasis>
123 driver supports PCI devices with vendor ID 0x1319 and device ID 0x0801,
124 and has an alias of <quote>pci:v00001319d00000801sv*sd*bc04sc01i*</quote>.
125 For most devices, the bus driver exports the alias of the driver that
126 would handle the device via <systemitem
127 class="filesystem">sysfs</systemitem>. E.g., the
128 <filename>/sys/bus/pci/devices/0000:00:0d.0/modalias</filename> file
129 might contain the string
130 <quote>pci:v00001319d00000801sv00001319sd00001319bc04sc01i00</quote>.
131 The default rules provided by Udev will cause <command>udevd</command>
132 to call out to <command>/sbin/modprobe</command> with the contents of the
133 <envar>MODALIAS</envar> uevent environment variable (that should be the
134 same as the contents of the <filename>modalias</filename> file in sysfs),
135 thus loading all modules whose aliases match this string after wildcard
136 expansion.</para>
137
138 <para>In this example, this means that, in addition to
139 <emphasis>snd-fm801</emphasis>, the obsolete (and unwanted)
140 <emphasis>forte</emphasis> driver will be loaded if it is
141 available. See below for ways in which the loading of unwanted drivers can
142 be prevented.</para>
143
144 <para>The kernel itself is also able to load modules for network
145 protocols, filesystems and NLS support on demand.</para>
146
147 </sect3>
148
149 <sect3>
150 <title>Handling Hotpluggable/Dynamic Devices</title>
151
152 <para>When you plug in a device, such as a Universal Serial Bus (USB) MP3
153 player, the kernel recognizes that the device is now connected and
154 generates a uevent. This uevent is then handled by
155 <command>udevd</command> as described above.</para>
156
157 </sect3>
158
159 </sect2>
160
161 <sect2>
162 <title>Problems with Loading Modules and Creating Devices</title>
163
164 <para>There are a few possible problems when it comes to automatically
165 creating device nodes.</para>
166
167 <sect3>
168 <title>A kernel module is not loaded automatically</title>
169
170 <para>Udev will only load a module if it has a bus-specific alias and the
171 bus driver properly exports the necessary aliases to <systemitem
172 class="filesystem">sysfs</systemitem>. In other cases, one should
173 arrange module loading by other means. With Linux-&linux-version;, Udev is
174 known to load properly-written drivers for INPUT, IDE, PCI, USB, SCSI,
175 SERIO and FireWire devices.</para>
176
177 <para>To determine if the device driver you require has the necessary
178 support for Udev, run <command>modinfo</command> with the module name as
179 the argument. Now try locating the device directory under
180 <filename class="directory">/sys/bus</filename> and check whether there is
181 a <filename>modalias</filename> file there.</para>
182
183 <para>If the <filename>modalias</filename> file exists in <systemitem
184 class="filesystem">sysfs</systemitem>, the driver supports the device and
185 can talk to it directly, but doesn't have the alias, it is a bug in the
186 driver. Load the driver without the help from Udev and expect the issue
187 to be fixed later.</para>
188
189 <para>If there is no <filename>modalias</filename> file in the relevant
190 directory under <filename class="directory">/sys/bus</filename>, this
191 means that the kernel developers have not yet added modalias support to
192 this bus type. With Linux-&linux-version;, this is the case with ISA
193 busses. Expect this issue to be fixed in later kernel versions.</para>
194
195 <para>Udev is not intended to load <quote>wrapper</quote> drivers such as
196 <emphasis>snd-pcm-oss</emphasis> and non-hardware drivers such as
197 <emphasis>loop</emphasis> at all.</para>
198
199 </sect3>
200
201 <sect3>
202 <title>A kernel module is not loaded automatically, and Udev is not
203 intended to load it</title>
204
205 <para>If the <quote>wrapper</quote> module only enhances the functionality
206 provided by some other module (e.g., <emphasis>snd-pcm-oss</emphasis>
207 enhances the functionality of <emphasis>snd-pcm</emphasis> by making the
208 sound cards available to OSS applications), configure
209 <command>modprobe</command> to load the wrapper after Udev loads the
[416772a]210 wrapped module. To do this, add an <quote>install</quote> line to a file
211 in <filename>/etc/modprobe.d</filename>. For example:</para>
[806d0c2]212
213<screen role="nodump"><literal>install snd-pcm /sbin/modprobe -i snd-pcm ; \
214 /sbin/modprobe snd-pcm-oss ; true</literal></screen>
215
216 </sect3>
217
218 <sect3>
219 <title>Udev loads some unwanted module</title>
220
221 <para>Either don't build the module, or blacklist it in
[416772a]222 <filename>/etc/modprobe.d</filename> file as done with the
[806d0c2]223 <emphasis>forte</emphasis> module in the example below:</para>
224
225<screen role="nodump"><literal>blacklist forte</literal></screen>
226
227 <para>Blacklisted modules can still be loaded manually with the
228 explicit <command>modprobe</command> command.</para>
229
230 </sect3>
231
232 <sect3>
233 <title>Udev creates a device incorrectly, or makes a wrong symlink</title>
234
235 <para>This usually happens if a rule unexpectedly matches a device. For
236 example, a poorly-writen rule can match both a SCSI disk (as desired)
237 and the corresponding SCSI generic device (incorrectly) by vendor.
238 Find the offending rule and make it more specific, with the help of
239 <command>udevadm info</command>.</para>
240
241 </sect3>
242
243 <sect3>
244 <title>Udev rule works unreliably</title>
245
246 <para>This may be another manifestation of the previous problem. If not,
247 and your rule uses <systemitem class="filesystem">sysfs</systemitem>
248 attributes, it may be a kernel timing issue, to be fixed in later kernels.
249 For now, you can work around it by creating a rule that waits for the used
250 <systemitem class="filesystem">sysfs</systemitem> attribute and appending
251 it to the <filename>/etc/udev/rules.d/10-wait_for_sysfs.rules</filename>
252 file. Please notify the CLFS Development list if you do so and it
253 helps.</para>
254
255 </sect3>
256
257 <sect3>
258 <title>Udev does not create a device</title>
259
260 <para>Further text assumes that the driver is built statically into the
261 kernel or already loaded as a module, and that you have already checked
262 that Udev doesn't create a misnamed device.</para>
263
264 <para>Udev has no information needed to create a device node if a kernel
265 driver does not export its data to <systemitem
266 class="filesystem">sysfs</systemitem>.
267 This is most common with third party drivers from outside the kernel
268 tree. Create a static device node in
269 <filename>/lib/udev/devices</filename> with the appropriate major/minor
270 numbers (see the file <filename>devices.txt</filename> inside the kernel
271 documentation or the documentation provided by the third party driver
272 vendor). The static device node will be copied to
273 <filename class="directory">/dev</filename> by the
274 <command>S10udev</command> bootscript.</para>
275
276 </sect3>
277
278 <sect3>
279 <title>Device naming order changes randomly after rebooting</title>
280
281 <para>This is due to the fact that Udev, by design, handles uevents and
282 loads modules in parallel, and thus in an unpredictable order. This will
283 never be <quote>fixed</quote>. You should not rely upon the kernel device
284 names being stable. Instead, create your own rules that make symlinks with
285 stable names based on some stable attributes of the device, such as a
286 serial number or the output of various *_id utilities installed by Udev.
287 See <xref linkend="ch-scripts-symlinks"/> and
288 <xref linkend="chapter-network"/> for examples.</para>
289
290 </sect3>
291
292 </sect2>
293
294 <sect2>
295 <title>Useful Reading</title>
296
297 <para>Additional helpful documentation is available at the following
298 sites:</para>
299
300 <itemizedlist>
301
302 <listitem>
303 <para remap="verbatim">A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
304 <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para>
305 </listitem>
306
307 <listitem>
308 <para remap="verbatim">The <systemitem class="filesystem">sysfs</systemitem> Filesystem
309 <ulink url="http://www.kernel.org/pub/linux/kernel/people/mochel/doc/papers/ols-2005/mochel.pdf"/></para>
310 </listitem>
311
312 </itemizedlist>
313
314 </sect2>
315
316</sect1>
Note: See TracBrowser for help on using the repository browser.