1 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
2 | <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
|
---|
3 | <!ENTITY % general-entities SYSTEM "../general.ent">
|
---|
4 | %general-entities;
|
---|
5 | ]>
|
---|
6 | <sect1 id="ch-scripts-udev">
|
---|
7 | <title>Device and Module Handling on an LFS System</title>
|
---|
8 | <?dbhtml filename="udev.html"?>
|
---|
9 |
|
---|
10 | <indexterm zone="ch-scripts-udev">
|
---|
11 | <primary sortas="a-Udev">Udev</primary>
|
---|
12 | <secondary>usage</secondary></indexterm>
|
---|
13 |
|
---|
14 | <para>In <xref linkend="chapter-building-system"/>, we installed the Udev
|
---|
15 | package. Before we go into the details regarding how this works,
|
---|
16 | a brief history of previous methods of handling devices is in
|
---|
17 | order.</para>
|
---|
18 |
|
---|
19 | <para>Linux systems in general traditionally use a static device
|
---|
20 | creation method, whereby a great many device nodes are created under
|
---|
21 | <filename class="directory">/dev</filename> (sometimes literally
|
---|
22 | thousands of nodes), regardless of whether the corresponding hardware
|
---|
23 | devices actually exist. This is typically done via a
|
---|
24 | <command>MAKEDEV</command> script, which contains a number of
|
---|
25 | calls to the <command>mknod</command> program with the relevant major and minor device
|
---|
26 | numbers for every possible device that might exist in the world. Using
|
---|
27 | the udev method, only those devices which are detected by the kernel
|
---|
28 | get device nodes created for them. Because these device nodes will be
|
---|
29 | created each time the system boots, they will be stored on a
|
---|
30 | <systemitem class="filesystem">tmpfs</systemitem> (a file system that
|
---|
31 | resides entirely in memory and does not take up any disk space).
|
---|
32 | Device nodes do not require much disk space, so the memory that is
|
---|
33 | used is negligible.</para>
|
---|
34 |
|
---|
35 | <sect2>
|
---|
36 | <title>History</title>
|
---|
37 |
|
---|
38 | <para>In February 2000, a new filesystem called <systemitem
|
---|
39 | class="filesystem">devfs</systemitem> was merged into the 2.3.46
|
---|
40 | kernel and was made available during the 2.4 series of
|
---|
41 | stable kernels. Although it was present in the kernel source itself,
|
---|
42 | this method of creating devices dynamically never received
|
---|
43 | overwhelming support from the core kernel developers.</para>
|
---|
44 |
|
---|
45 | <para>The main problem with the approach adopted by <systemitem
|
---|
46 | class="filesystem">devfs</systemitem> was the way it handled
|
---|
47 | device detection, creation, and naming. The latter issue, that of
|
---|
48 | device node naming, was perhaps the most critical. It is generally
|
---|
49 | accepted that if device names are allowed to be configurable, then
|
---|
50 | the device naming policy should be up to a system administrator, not
|
---|
51 | imposed on them by any particular developer(s). The <systemitem
|
---|
52 | class="filesystem">devfs</systemitem> file system also suffers from race
|
---|
53 | conditions that are inherent in its design and cannot be fixed
|
---|
54 | without a substantial revision to the kernel. It has also been marked
|
---|
55 | as deprecated due to a lack of recent maintenance.</para>
|
---|
56 |
|
---|
57 | <para>With the development of the unstable 2.5 kernel tree, later
|
---|
58 | released as the 2.6 series of stable kernels, a new virtual filesystem
|
---|
59 | called <systemitem class="filesystem">sysfs</systemitem> came to be.
|
---|
60 | The job of <systemitem class="filesystem">sysfs</systemitem> is to
|
---|
61 | export a view of the system's structure to userspace processes. With
|
---|
62 | this userspace visible representation, the possibility of seeing a
|
---|
63 | userspace replacement for <systemitem
|
---|
64 | class="filesystem">devfs</systemitem> became much more
|
---|
65 | realistic.</para>
|
---|
66 | </sect2>
|
---|
67 |
|
---|
68 | <sect2>
|
---|
69 | <title>Udev Implementation</title>
|
---|
70 |
|
---|
71 | <para>The <systemitem class="filesystem">sysfs</systemitem> filesystem
|
---|
72 | was mentioned briefly above. One may wonder how <systemitem
|
---|
73 | class="filesystem">sysfs</systemitem> knows about the devices present
|
---|
74 | on a system and what device numbers should be used. Drivers that
|
---|
75 | have been compiled into the kernel directly register their objects
|
---|
76 | with <systemitem class="filesystem">sysfs</systemitem> as they are
|
---|
77 | detected by the kernel. For drivers compiled as modules, this will
|
---|
78 | happen when the module is loaded. Once the <systemitem
|
---|
79 | class="filesystem">sysfs</systemitem> filesystem is mounted (on
|
---|
80 | <filename class="directory">/sys</filename>), data which the
|
---|
81 | built-in drivers registered with <systemitem
|
---|
82 | class="filesystem">sysfs</systemitem> are available to userspace
|
---|
83 | processes and to <command>udev</command> for device node creation.</para>
|
---|
84 |
|
---|
85 | <para>The <command>S10udev</command> initscript takes care of creating these
|
---|
86 | device nodes when Linux is booted. This script starts with registering
|
---|
87 | <command>/sbin/udevsend</command> as a hotplug event handler. Hotplug events
|
---|
88 | (discussed below) should not be generated during this stage, but
|
---|
89 | <command>udev</command> is registered just in case they do occur. The
|
---|
90 | <command>udevstart</command> program then walks through the <systemitem
|
---|
91 | class="filesystem">/sys</systemitem> filesystem and creates devices under
|
---|
92 | <filename class="directory">/dev</filename> that match the descriptions. For
|
---|
93 | example, <filename>/sys/class/tty/vcs/dev</filename> contains the string
|
---|
94 | <quote>7:0</quote> This string is used by <command>udevstart</command> to create
|
---|
95 | <filename>/dev/vcs</filename> with major number <emphasis>7</emphasis> and minor
|
---|
96 | <emphasis>0</emphasis>. The names and permissions of the nodes created under
|
---|
97 | the <filename class="directory">/dev</filename> directory are configured
|
---|
98 | according to the rules specified in the files within the <filename
|
---|
99 | class="directory">/etc/udev/rules.d/</filename> directory. These are numbered in
|
---|
100 | a similar fashion to the LFS-Bootscripts package. If <command>udev</command>
|
---|
101 | can't find a rule for the device it is creating, it will default permissions to
|
---|
102 | <emphasis>660</emphasis> and ownership to <emphasis>root:root</emphasis>.</para>
|
---|
103 |
|
---|
104 | <para>Once the above stage is complete, all devices that were already
|
---|
105 | present and have compiled-in drivers will be available for use. What
|
---|
106 | about those devices that have modular drivers?</para>
|
---|
107 |
|
---|
108 | <para>Earlier, we mentioned the concept of a <quote>hotplug event
|
---|
109 | handler.</quote> When a new device connection is detected by the
|
---|
110 | kernel, the kernel will generate a hotplug event and look at the file
|
---|
111 | <filename>/proc/sys/kernel/hotplug</filename> to find out the
|
---|
112 | userspace program that handles the device's connection. The
|
---|
113 | <command>udev</command> initscript registered <command>udevsend</command>
|
---|
114 | as this handler. When these hotplug events are generated, the kernel
|
---|
115 | will tell <command>udev</command> to check the <filename
|
---|
116 | class="directory">/sys</filename> filesystem for the information
|
---|
117 | pertaining to this new device and create the <filename
|
---|
118 | class="directory">/dev</filename> entry for it.</para>
|
---|
119 |
|
---|
120 | <para>This brings us to one problem that exists with
|
---|
121 | <command>udev</command>, and likewise with <systemitem
|
---|
122 | class="filesystem">devfs</systemitem> before it. It is commonly
|
---|
123 | referred to as the <quote>chicken and egg</quote> problem. Most Linux
|
---|
124 | distributions handle loading modules via entries in
|
---|
125 | <filename>/etc/modules.conf</filename>. Access to a device node causes
|
---|
126 | the appropriate kernel module to load. With <command>udev</command>,
|
---|
127 | this method will not work because the device node does not exist until
|
---|
128 | the module is loaded. To solve this, the
|
---|
129 | <command>S05modules</command> bootscript was added to the
|
---|
130 | LFS-Bootscripts package, along with the
|
---|
131 | <filename>/etc/sysconfig/modules</filename> file. By
|
---|
132 | adding module
|
---|
133 | names to the <filename>modules</filename> file, these modules will be
|
---|
134 | loaded when the computer is starting up. This allows
|
---|
135 | <command>udev</command> to detect the devices and create the
|
---|
136 | appropriate device nodes.</para>
|
---|
137 |
|
---|
138 | <para>Note that on slower machines or for drivers that create a lot
|
---|
139 | of device nodes, the process of creating devices may take a few
|
---|
140 | seconds to complete. This means that some device nodes may not be
|
---|
141 | immediately accessible.</para>
|
---|
142 | </sect2>
|
---|
143 |
|
---|
144 | <sect2>
|
---|
145 | <title>Handling Hotpluggable/Dynamic Devices</title>
|
---|
146 |
|
---|
147 | <para>When you plug in a device, such as a Universal Serial Bus (USB) MP3 player, the kernel
|
---|
148 | recognizes that the device is now connected and generates a hotplug
|
---|
149 | event. If the driver is already loaded (either because it was compiled
|
---|
150 | into the kernel or because it was loaded via the
|
---|
151 | <command>S05modules</command> bootscript), <command>udev</command> will
|
---|
152 | be called upon to create the relevant device node(s) according to the
|
---|
153 | <systemitem class="filesystem">sysfs</systemitem> data available in
|
---|
154 | <filename class="directory">/sys</filename>.</para>
|
---|
155 |
|
---|
156 | <para>If the driver for the just plugged in device is available as a module but
|
---|
157 | currently unloaded, the Hotplug package will load the appropriate module
|
---|
158 | and make this device available by creating the device node(s) for it.</para>
|
---|
159 | </sect2>
|
---|
160 |
|
---|
161 | <sect2>
|
---|
162 | <title>Problems with Creating Devices</title>
|
---|
163 |
|
---|
164 | <para>There are a few known problems when it comes to automatically creating
|
---|
165 | device nodes:</para>
|
---|
166 |
|
---|
167 | <para>1) A kernel driver may not export its data to <systemitem
|
---|
168 | class="filesystem">sysfs</systemitem>.</para>
|
---|
169 |
|
---|
170 | <para>This is most common with third party drivers from outside the
|
---|
171 | kernel tree. These drivers will not end up having their device nodes
|
---|
172 | created. Use the
|
---|
173 | <filename>/etc/sysconfig/createfiles</filename> configuration file to
|
---|
174 | manually create the devices. Consult the
|
---|
175 | <filename>devices.txt</filename> file inside the kernel documentation
|
---|
176 | or the documentation for that driver to find the proper major/minor
|
---|
177 | numbers.</para>
|
---|
178 |
|
---|
179 | <para>2) A non-hardware device is required. This is most common with
|
---|
180 | the Advanced Linux Sound Architecture (ALSA) project's Open Sound
|
---|
181 | System (OSS) compatibility module. These types of devices can be
|
---|
182 | handled in one of two ways:</para>
|
---|
183 |
|
---|
184 | <itemizedlist>
|
---|
185 |
|
---|
186 | <listitem><para>Adding the module names to
|
---|
187 | <filename>/etc/sysconfig/modules</filename></para><beginpage/></listitem>
|
---|
188 | <listitem><para>Using an
|
---|
189 | <quote>install</quote> line in
|
---|
190 | <filename>/etc/modprobe.conf</filename>. This tells the
|
---|
191 | <command>modprobe</command> command <quote>when loading this module,
|
---|
192 | also load this other module, at the same time.</quote> For example:</para>
|
---|
193 |
|
---|
194 | <screen><userinput>install snd-pcm modprobe -i snd-pcm ; modprobe \
|
---|
195 | snd-pcm-oss ; true</userinput></screen>
|
---|
196 |
|
---|
197 | <para>This will cause the system to load both the
|
---|
198 | <emphasis>snd-pcm</emphasis> and <emphasis>snd-pcm-oss</emphasis>
|
---|
199 | modules when any request is made to load the driver
|
---|
200 | <emphasis>snd-pcm</emphasis>.</para></listitem>
|
---|
201 | </itemizedlist>
|
---|
202 | </sect2>
|
---|
203 |
|
---|
204 | <sect2>
|
---|
205 | <title>Useful Reading</title>
|
---|
206 |
|
---|
207 | <para>Additional helpful documentation is available at the following
|
---|
208 | sites:</para>
|
---|
209 |
|
---|
210 | <itemizedlist>
|
---|
211 | <listitem><para remap="verbatim">A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
|
---|
212 | <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para></listitem>
|
---|
213 |
|
---|
214 | <listitem><para remap="verbatim">udev FAQ
|
---|
215 | <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev-FAQ"/></para></listitem>
|
---|
216 |
|
---|
217 | <listitem><para remap="verbatim">The Linux Kernel Driver Model
|
---|
218 | <ulink url="http://public.planetmirror.com/pub/lca/2003/proceedings/papers/Patrick_Mochel/Patrick_Mochel.pdf"/></para></listitem>
|
---|
219 | </itemizedlist>
|
---|
220 | </sect2>
|
---|
221 |
|
---|
222 | </sect1>
|
---|
223 |
|
---|