1 | <?xml version="1.0" encoding="UTF-8"?>
|
---|
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">udev</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 <literal>pci:v00001319d00000801sv*sd*bc04sc01i*</literal>.
|
---|
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 | <literal>pci:v00001319d00000801sv00001319sd00001319bc04sc01i00</literal>.
|
---|
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><filename></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 <ulink role='man' url='&man;modprobe.d.5'>modprobe.d(5)</ulink>
|
---|
229 | manual page for more information on <quote>softdep</quote> syntax and
|
---|
230 | capabilities.</para>
|
---|
231 |
|
---|
232 | <para revision="sysv">If the module in question is not a wrapper and is
|
---|
233 | useful by itself, configure the <command>modules</command> bootscript to
|
---|
234 | load this module on system boot. To do this, add the module name to the
|
---|
235 | <filename>/etc/sysconfig/modules</filename> file on a separate line.
|
---|
236 | This works for wrapper modules too, but is suboptimal in that case.</para>
|
---|
237 |
|
---|
238 | </sect3>
|
---|
239 |
|
---|
240 | <sect3>
|
---|
241 | <title>Udev Loads Some Unwanted Module</title>
|
---|
242 |
|
---|
243 | <para>Either don't build the module, or blacklist it in a
|
---|
244 | <filename>/etc/modprobe.d/blacklist.conf</filename> file as done with the
|
---|
245 | <emphasis>forte</emphasis> module in the example below:</para>
|
---|
246 |
|
---|
247 | <screen role="nodump"><literal>blacklist forte</literal></screen>
|
---|
248 |
|
---|
249 | <para>Blacklisted modules can still be loaded manually with the
|
---|
250 | explicit <command>modprobe</command> command.</para>
|
---|
251 |
|
---|
252 | </sect3>
|
---|
253 |
|
---|
254 | <sect3>
|
---|
255 | <title>Udev Creates a Device Incorrectly, or Makes the Wrong Symlink</title>
|
---|
256 |
|
---|
257 | <para>This usually happens if a rule unexpectedly matches a device. For
|
---|
258 | example, a poorly-written rule can match both a SCSI disk (as desired)
|
---|
259 | and the corresponding SCSI generic device (incorrectly) by vendor.
|
---|
260 | Find the offending rule and make it more specific, with the help of the
|
---|
261 | <command>udevadm info</command> command.</para>
|
---|
262 |
|
---|
263 | </sect3>
|
---|
264 |
|
---|
265 | <sect3>
|
---|
266 | <title>Udev Rule Works Unreliably</title>
|
---|
267 |
|
---|
268 | <para>This may be another manifestation of the previous problem. If not,
|
---|
269 | and your rule uses <systemitem class="filesystem">sysfs</systemitem>
|
---|
270 | attributes, it may be a kernel timing issue, to be fixed in later kernels.
|
---|
271 | For now, you can work around it by creating a rule that waits for the used
|
---|
272 | <systemitem class="filesystem">sysfs</systemitem> attribute and appending
|
---|
273 | it to the <filename>/etc/udev/rules.d/10-wait_for_sysfs.rules</filename>
|
---|
274 | file (create this file if it does not exist). Please notify the LFS
|
---|
275 | Development list if you do so and it helps.</para>
|
---|
276 |
|
---|
277 | </sect3>
|
---|
278 |
|
---|
279 | <sect3>
|
---|
280 | <title>Udev Does Not Create a Device</title>
|
---|
281 |
|
---|
282 | <para>First, be certain that the driver is built into the
|
---|
283 | kernel or already loaded as a module, and that
|
---|
284 | udev isn't creating a misnamed device.</para>
|
---|
285 |
|
---|
286 | <para>If a kernel driver does not export its data to
|
---|
287 | <systemitem class="filesystem">sysfs</systemitem>, udev lacks the
|
---|
288 | information needed to create a device node. This is most likely to happen
|
---|
289 | with third party drivers from outside the kernel tree. Create a static
|
---|
290 | device node in <filename>/usr/lib/udev/devices</filename> with the
|
---|
291 | appropriate major/minor numbers (see the file
|
---|
292 | <filename>devices.txt</filename> inside the kernel documentation or the
|
---|
293 | documentation provided by the third party driver vendor). The static
|
---|
294 | device node will be copied to <filename class="directory">/dev</filename>
|
---|
295 | by <command>udev</command>.</para>
|
---|
296 |
|
---|
297 | </sect3>
|
---|
298 |
|
---|
299 | <sect3>
|
---|
300 | <title>Device Naming Order Changes Randomly After Rebooting</title>
|
---|
301 |
|
---|
302 | <para>This is due to the fact that udev, by design, handles uevents and
|
---|
303 | loads modules in parallel, and thus in an unpredictable order. This will
|
---|
304 | never be <quote>fixed.</quote> You should not rely upon the kernel device
|
---|
305 | names being stable. Instead, create your own rules that make symlinks with
|
---|
306 | stable names based on some stable attributes of the device, such as a
|
---|
307 | serial number or the output of various *_id utilities installed by udev.
|
---|
308 | See <xref linkend="ch-config-symlinks"/> and
|
---|
309 | <xref linkend="ch-config-network"/> for examples.</para>
|
---|
310 |
|
---|
311 | </sect3>
|
---|
312 |
|
---|
313 | </sect2>
|
---|
314 |
|
---|
315 | <sect2>
|
---|
316 | <title>Useful Reading</title>
|
---|
317 |
|
---|
318 | <para>Additional helpful documentation is available at the following
|
---|
319 | sites:</para>
|
---|
320 |
|
---|
321 | <itemizedlist>
|
---|
322 |
|
---|
323 | <listitem>
|
---|
324 | <para>A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
|
---|
325 | <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para>
|
---|
326 | </listitem>
|
---|
327 |
|
---|
328 | <listitem>
|
---|
329 | <para>The <systemitem class="filesystem">sysfs</systemitem> Filesystem
|
---|
330 | <ulink url="https://www.kernel.org/pub/linux/kernel/people/mochel/doc/papers/ols-2005/mochel.pdf"/></para>
|
---|
331 | </listitem>
|
---|
332 |
|
---|
333 | <!-- No longer available
|
---|
334 | <listitem>
|
---|
335 | <para>Pointers to further reading
|
---|
336 | <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html"/>
|
---|
337 | </para>
|
---|
338 | </listitem>
|
---|
339 | -->
|
---|
340 | </itemizedlist>
|
---|
341 |
|
---|
342 | </sect2>
|
---|
343 |
|
---|
344 | </sect1>
|
---|