source: chapter07/udev.xml@ 5ba3d1d

6.0
Last change on this file since 5ba3d1d was 5ba3d1d, checked in by Gerard Beekmans <gerard@…>, 20 years ago

Completed global edits for upcoming 6.0 release

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