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