[5ba3d1d] | 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">
|
---|
[69993f4] | 7 | <title>Device and Module Handling on an LFS System</title>
|
---|
[5ba3d1d] | 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 |
|
---|
[9876d9e] | 14 | <para>In <xref linkend="chapter-building-system"/>, we installed the Udev
|
---|
[69993f4] | 15 | package. Before we go into the details regarding how this works,
|
---|
| 16 | a brief history of previous methods of handling devices is in
|
---|
[5ba3d1d] | 17 | order.</para>
|
---|
| 18 |
|
---|
[0cb7f8b] | 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
|
---|
[69993f4] | 24 | <command>MAKEDEV</command> script, which contains a number of
|
---|
[9876d9e] | 25 | calls to the <command>mknod</command> program with the relevant major and minor device
|
---|
[0cb7f8b] | 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
|
---|
[69993f4] | 28 | get device nodes created for them. Because these device nodes will be
|
---|
[0cb7f8b] | 29 | created each time the system boots, they will be stored on a
|
---|
| 30 | <systemitem class="filesystem">ramfs</systemitem> (a file system that
|
---|
[5ba3d1d] | 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 in negligable.</para>
|
---|
| 34 |
|
---|
| 35 | <sect2>
|
---|
| 36 | <title>History</title>
|
---|
| 37 |
|
---|
[853917c] | 38 | <para>In February 2000, a new filesystem called <systemitem
|
---|
[5ba3d1d] | 39 | class="filesystem">devfs</systemitem> was merged into the 2.3.46
|
---|
[69993f4] | 40 | kernel and was made available during the 2.4 series of
|
---|
[5ba3d1d] | 41 | stable kernels. Although it was present in the kernel source itself,
|
---|
[0cb7f8b] | 42 | this method of creating devices dynamically never received
|
---|
[0aa61a48] | 43 | overwhelming support from the core kernel developers.</para>
|
---|
[0cb7f8b] | 44 |
|
---|
| 45 | <para>The main problem with the approach adopted by <systemitem
|
---|
[69993f4] | 46 | class="filesystem">devfs</systemitem> was the way it handled
|
---|
[0cb7f8b] | 47 | device detection, creation, and naming. The latter issue, that of
|
---|
| 48 | device node naming, was perhaps the most critical. It is generally
|
---|
[69993f4] | 49 | accepted that if device names are allowed to be configurable, then
|
---|
[0cb7f8b] | 50 | the device naming policy should be up to a system administrator, not
|
---|
[69993f4] | 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
|
---|
[0cb7f8b] | 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
|
---|
[69993f4] | 58 | released as the 2.6 series of stable kernels, a new virtual filesystem
|
---|
| 59 | called <systemitem class="filesystem">sysfs</systemitem> came to be.
|
---|
[0cb7f8b] | 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
|
---|
[0aa61a48] | 65 | realistic.</para>
|
---|
[8eeb6bc] | 66 | <beginpage/>
|
---|
[0ffc1a4] | 67 | </sect2>
|
---|
[8eeb6bc] | 68 |
|
---|
[5ba3d1d] | 69 | <sect2>
|
---|
| 70 | <title>Udev Implementation</title>
|
---|
| 71 |
|
---|
| 72 | <para>The <systemitem class="filesystem">sysfs</systemitem> filesystem
|
---|
| 73 | was mentioned briefly above. One may wonder how <systemitem
|
---|
| 74 | class="filesystem">sysfs</systemitem> knows about the devices present
|
---|
[69993f4] | 75 | on a system and what device numbers should be used. Drivers that
|
---|
[0cb7f8b] | 76 | have been compiled into the kernel directly register their objects
|
---|
[5ba3d1d] | 77 | with <systemitem class="filesystem">sysfs</systemitem> as they are
|
---|
| 78 | detected by the kernel. For drivers compiled as modules, this will
|
---|
| 79 | happen when the module is loaded. Once the <systemitem
|
---|
| 80 | class="filesystem">sysfs</systemitem> filesystem is mounted (on
|
---|
| 81 | <filename class="directory">/sys</filename>), the data which the
|
---|
| 82 | built-in drivers registered with <systemitem
|
---|
[69993f4] | 83 | class="filesystem">sysfs</systemitem> are available to userspace
|
---|
[0cb7f8b] | 84 | processes and to <command>udev</command> for device node creation.</para>
|
---|
[5ba3d1d] | 85 |
|
---|
| 86 | <para>The <command>S10udev</command> initscript takes care of creating
|
---|
[0cb7f8b] | 87 | these device nodes when Linux is booted. This script starts with
|
---|
| 88 | registering <command>/sbin/udev</command> as a hotplug event handler.
|
---|
| 89 | Hotplug events (discussed below) should not be generated during this
|
---|
| 90 | stage, but <command>udev</command> is registered just in case they do
|
---|
| 91 | occur. The <command>udevstart</command> program then walks through
|
---|
| 92 | the <systemitem class="filesystem">/sys</systemitem> filesystem and
|
---|
[5ba3d1d] | 93 | creates devices under <filename class="directory">/dev</filename> that
|
---|
| 94 | match the descriptions. For example,
|
---|
| 95 | <filename>/sys/class/tty/vcs/dev</filename> contains the string
|
---|
[69993f4] | 96 | <quote>7:0</quote> This string is used by <command>udevstart</command>
|
---|
| 97 | to create <filename>/dev/vcs</filename> with major number
|
---|
| 98 | <emphasis>7</emphasis> and minor <emphasis>0</emphasis>. The
|
---|
| 99 | permissions of each and every device that <command>udevstart</command>
|
---|
| 100 | creates are set using files from the <filename
|
---|
[5ba3d1d] | 101 | class="directory">/etc/udev.d/permissions.d/</filename> directory.
|
---|
[0cb7f8b] | 102 | These are numbered in a similar fashion to the LFS bootscripts. If
|
---|
| 103 | <command>udev</command> cannot find a permissions file for the device
|
---|
| 104 | it is creating, it will default permissions to
|
---|
| 105 | <emphasis>600</emphasis> and ownership to
|
---|
[5ba3d1d] | 106 | <emphasis>root:root</emphasis>. The names of the nodes created under
|
---|
| 107 | the <filename class="directory">/dev</filename> directory are
|
---|
| 108 | configured according to the rules specified in the files within the
|
---|
| 109 | <filename class="directory">/etc/udev/rules.d/</filename>
|
---|
| 110 | directory.</para>
|
---|
| 111 |
|
---|
| 112 | <para>Once the above stage is complete, all devices that were already
|
---|
[69993f4] | 113 | present and have compiled-in drivers will be available for use. What
|
---|
[5ba3d1d] | 114 | about those devices that have modular drivers?</para>
|
---|
| 115 |
|
---|
[69993f4] | 116 | <para>Earlier, we mentioned the concept of a <quote>hotplug event
|
---|
| 117 | handler.</quote> When a new device connection is detected by the
|
---|
[0cb7f8b] | 118 | kernel, the kernel will generate a hotplug event and look at the file
|
---|
[5ba3d1d] | 119 | <filename>/proc/sys/kernel/hotplug</filename> to find out the
|
---|
[69993f4] | 120 | userspace program that handles the device's connection. The
|
---|
[5ba3d1d] | 121 | <command>udev</command> initscript registered <command>udev</command>
|
---|
| 122 | as this handler. When these hotplug events are generated, the kernel
|
---|
| 123 | will tell <command>udev</command> to check the <filename
|
---|
| 124 | class="directory">/sys</filename> filesystem for the information
|
---|
[69993f4] | 125 | pertaining to this new device and create the <filename
|
---|
[5ba3d1d] | 126 | class="directory">/dev</filename> entry for it.</para>
|
---|
| 127 |
|
---|
| 128 | <para>This brings us to one problem that exists with
|
---|
| 129 | <command>udev</command>, and likewise with <systemitem
|
---|
| 130 | class="filesystem">devfs</systemitem> before it. It is commonly
|
---|
| 131 | referred to as the <quote>chicken and egg</quote> problem. Most Linux
|
---|
| 132 | distrubtions handle loading modules via entries in
|
---|
| 133 | <filename>/etc/modules.conf</filename>. Access to a device node causes
|
---|
| 134 | the appropriate kernel module to load. With <command>udev</command>,
|
---|
| 135 | this method will not work because the device node does not exist until
|
---|
[0cb7f8b] | 136 | the module is loaded. To solve this, the
|
---|
| 137 | <command>S05modules</command> bootscript was added to the
|
---|
[69993f4] | 138 | lfs-bootscripts package, along with the
|
---|
[2256873] | 139 | <filename>/etc/sysconfig/modules</filename> file. By
|
---|
| 140 | <beginpage/> adding module
|
---|
[0cb7f8b] | 141 | names to the <filename>modules</filename> file, these modules will be
|
---|
| 142 | loaded when the computer is starting up. This allows
|
---|
| 143 | <command>udev</command> to detect the devices and create the
|
---|
| 144 | appropriate device nodes.</para>
|
---|
| 145 |
|
---|
[69993f4] | 146 | <para>Note that on slower machines or for drivers that create a lot
|
---|
[0cb7f8b] | 147 | of device nodes, the process of creating devices may take a few
|
---|
| 148 | seconds to complete. This means that some device nodes may not be
|
---|
[5ba3d1d] | 149 | immediately accessible.</para>
|
---|
| 150 | </sect2>
|
---|
| 151 |
|
---|
| 152 | <sect2>
|
---|
[69993f4] | 153 | <title>Handling Hotpluggable/Dynamic Devices</title>
|
---|
[5ba3d1d] | 154 |
|
---|
[69993f4] | 155 | <para>When you plug in a device, such a Universal Serial Bus (USB) MP3 player, the kernel
|
---|
[0cb7f8b] | 156 | recognizes that the device is now connected and generates a hotplug
|
---|
[24a08b9] | 157 | event. If the driver is already loaded (either because it was compiled
|
---|
[69993f4] | 158 | into the kernel or because it was loaded via the
|
---|
| 159 | <command>S05modules</command> bootscript), <command>udev</command> will
|
---|
[24a08b9] | 160 | be called upon to create the relevant device node(s) according to the
|
---|
| 161 | <systemitem class="filesystem">sysfs</systemitem> data available in
|
---|
| 162 | <filename class="directory">/sys</filename>. If the driver for the
|
---|
| 163 | just plugged in device is available as a module but currently unloaded,
|
---|
| 164 | then attaching the device to the system will only cause the kernel's
|
---|
| 165 | bus driver to generate a hotplug event that notifies userspace of the
|
---|
| 166 | new device connection and it not being attached to a driver. In
|
---|
| 167 | effect, nothing happens and the device itself is not usable
|
---|
| 168 | yet.</para>
|
---|
| 169 |
|
---|
[69993f4] | 170 | <para>If building a system that has a lot of drivers compiled as
|
---|
| 171 | modules rather than directly built into the kernel, using the
|
---|
| 172 | <command>S05modules</command> may not be practical. The Hotplug
|
---|
[24a08b9] | 173 | package (see <ulink url="http://linux-hotplug.sourceforge.net/"/>) can
|
---|
[69993f4] | 174 | be beneficial in these cases. When the Hotplug package is installed,
|
---|
| 175 | it will respond to the aforementioned kernel's bus driver hotplug
|
---|
| 176 | events. The Hotplug package will load the appropriate module and make
|
---|
| 177 | this device available by creating the device node(s) for it.</para>
|
---|
[5ba3d1d] | 178 | </sect2>
|
---|
| 179 |
|
---|
| 180 | <sect2>
|
---|
[69993f4] | 181 | <title>Problems with Creating Devices</title>
|
---|
[5ba3d1d] | 182 |
|
---|
[69993f4] | 183 | <para>There are a few known problems when it comes to automatically creating
|
---|
[5ba3d1d] | 184 | devices nodes:</para>
|
---|
| 185 |
|
---|
[9876d9e] | 186 | <para>1) A kernel driver may not export its data to <systemitem
|
---|
| 187 | class="filesystem">sysfs</systemitem>.</para>
|
---|
[5ba3d1d] | 188 |
|
---|
[69993f4] | 189 | <para>This is most common with third party drivers from outside the
|
---|
[5ba3d1d] | 190 | kernel tree. These drivers will not end up having their device nodes
|
---|
[69993f4] | 191 | created. Use the
|
---|
[5ba3d1d] | 192 | <filename>/etc/sysconfig/createfiles</filename> configuration file to
|
---|
[0cb7f8b] | 193 | manually create the devices. Consult the
|
---|
[69993f4] | 194 | <filename>devices.txt</filename> file inside the kernel documentation
|
---|
[0cb7f8b] | 195 | or the documentation for that driver to find the proper major/minor
|
---|
| 196 | numbers.</para>
|
---|
[5ba3d1d] | 197 |
|
---|
[2256873] | 198 | <beginpage/>
|
---|
[69993f4] | 199 | <para>2) A non-hardware device is required. This is most common with
|
---|
| 200 | the Advanced Linux Sound Architecture (ALSA) project's Open Sound
|
---|
| 201 | System (OSS) compatibility module. These types of devices can be
|
---|
| 202 | handled in one of two ways:</para>
|
---|
[5ba3d1d] | 203 |
|
---|
| 204 | <itemizedlist>
|
---|
| 205 |
|
---|
| 206 | <listitem><para>Adding the module names to
|
---|
| 207 | <filename>/etc/sysconfig/modules</filename></para></listitem>
|
---|
[69993f4] | 208 | <listitem><para>Using an
|
---|
[5ba3d1d] | 209 | <quote>install</quote> line in
|
---|
[69993f4] | 210 | <filename>/etc/modprobe.conf</filename>. This tells the
|
---|
[9876d9e] | 211 | <command>modprobe</command> command <quote>when loading this module,
|
---|
| 212 | also load this other module, at the same time.</quote> For example:</para>
|
---|
[5ba3d1d] | 213 |
|
---|
[00cc7ce] | 214 | <screen><userinput>install snd-pcm modprobe -i snd-pcm ; modprobe \
|
---|
| 215 | snd-pcm-oss ; true</userinput></screen>
|
---|
[5ba3d1d] | 216 |
|
---|
| 217 | <para>This will cause the system to load both the
|
---|
| 218 | <emphasis>snd-pcm</emphasis> and <emphasis>snd-pcm-oss</emphasis>
|
---|
| 219 | modules when any request is made to load the driver
|
---|
| 220 | <emphasis>snd-pcm</emphasis>.</para></listitem>
|
---|
| 221 | </itemizedlist>
|
---|
| 222 | </sect2>
|
---|
| 223 |
|
---|
| 224 | <sect2>
|
---|
[69993f4] | 225 | <title>Useful Reading</title>
|
---|
[5ba3d1d] | 226 |
|
---|
[69993f4] | 227 | <para>Additional helpful documentation is available at the following
|
---|
| 228 | sites:</para>
|
---|
[5ba3d1d] | 229 |
|
---|
| 230 | <itemizedlist>
|
---|
[2256873] | 231 | <listitem><para remap="verbatim">A Userspace Implementation of devfs
|
---|
[c5143c9] | 232 | <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"><phrase
|
---|
[5a1e6d9] | 233 | condition="pdf">http://www.kroah.com/linux/talks/ols_2003_udev_paper/
|
---|
[c5143c9] | 234 | Reprint-Kroah-Hartman-OLS2003.pdf</phrase></ulink></para></listitem>
|
---|
| 235 |
|
---|
[2256873] | 236 | <listitem><para remap="verbatim">udev FAQ
|
---|
[c5143c9] | 237 | <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev-FAQ"/></para></listitem>
|
---|
| 238 |
|
---|
[2256873] | 239 | <listitem><para remap="verbatim">The Linux Kernel Driver Model
|
---|
[c5143c9] | 240 | <ulink url="http://public.planetmirror.com/pub/lca/2003/proceedings/papers/Patrick_Mochel/Patrick_Mochel.pdf"><phrase
|
---|
| 241 | condition="pdf">http://public.planetmirror.com/pub/lca/2003/proceedings/papers/
|
---|
| 242 | Patrick_Mochel/Patrick_Mochel.pdf</phrase></ulink></para></listitem>
|
---|
[5ba3d1d] | 243 | </itemizedlist>
|
---|
| 244 | </sect2>
|
---|
| 245 |
|
---|
| 246 | </sect1>
|
---|
| 247 |
|
---|