source: chapter07/udev.xml@ 68450d5

6.0
Last change on this file since 68450d5 was 0cb7f8b, checked in by Gerard Beekmans <gerard@…>, 20 years ago

Updated acknowledgements. Added notes regarding test suites not available for some packages. Edited the udev explanation page. Fixed some misc. typos

git-svn-id: http://svn.linuxfromscratch.org/LFS/branches/testing/BOOK@4011 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

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