[bce08ef] | 1 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
[d781ffb] | 2 | <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
|
---|
| 3 | "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
|
---|
[bce08ef] | 4 | <!ENTITY % general-entities SYSTEM "../general.ent">
|
---|
| 5 | %general-entities;
|
---|
| 6 | ]>
|
---|
[d781ffb] | 7 |
|
---|
[bce08ef] | 8 | <sect1 id="ch-scripts-udev">
|
---|
[d781ffb] | 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),
|
---|
[ae20451] | 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
|
---|
[d781ffb] | 35 | class="filesystem">tmpfs</systemitem> file system (a virtual file system that
|
---|
| 36 | resides entirely in system memory). Device nodes do not require much space, so
|
---|
| 37 | 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
|
---|
[ae20451] | 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 has also been marked as deprecated
|
---|
| 59 | due to a lack of recent maintenance.</para>
|
---|
| 60 |
|
---|
| 61 | <para>With the development of the unstable 2.5 kernel tree, later released
|
---|
| 62 | as the 2.6 series of stable kernels, a new virtual filesystem called
|
---|
| 63 | <systemitem class="filesystem">sysfs</systemitem> came to be. The job of
|
---|
| 64 | <systemitem class="filesystem">sysfs</systemitem> is to export a view of
|
---|
[e8eddacc] | 65 | the system's hardware configuration to userspace processes. With this
|
---|
[ae20451] | 66 | userspace-visible representation, the possibility of seeing a userspace
|
---|
| 67 | replacement for <systemitem class="filesystem">devfs</systemitem> became
|
---|
| 68 | much more realistic.</para>
|
---|
[d781ffb] | 69 |
|
---|
| 70 | </sect2>
|
---|
| 71 |
|
---|
| 72 | <sect2>
|
---|
| 73 | <title>Udev Implementation</title>
|
---|
| 74 |
|
---|
[ae20451] | 75 | <sect3>
|
---|
| 76 | <title>Sysfs</title>
|
---|
| 77 |
|
---|
| 78 | <para>The <systemitem class="filesystem">sysfs</systemitem> filesystem was
|
---|
| 79 | mentioned briefly above. One may wonder how <systemitem
|
---|
| 80 | class="filesystem">sysfs</systemitem> knows about the devices present on
|
---|
| 81 | a system and what device numbers should be used for them. Drivers that
|
---|
| 82 | have been compiled into the kernel directly register their objects with
|
---|
| 83 | <systemitem class="filesystem">sysfs</systemitem> as they are detected by
|
---|
| 84 | the kernel. For drivers compiled as modules, this registration will happen
|
---|
| 85 | when the module is loaded. Once the <systemitem
|
---|
| 86 | class="filesystem">sysfs</systemitem> filesystem is mounted (on <filename
|
---|
| 87 | class="directory">/sys</filename>), data which the built-in drivers
|
---|
| 88 | registered with <systemitem class="filesystem">sysfs</systemitem> are
|
---|
| 89 | available to userspace processes and to <command>udev</command> for device
|
---|
| 90 | node creation.</para>
|
---|
| 91 | </sect3>
|
---|
| 92 |
|
---|
| 93 | <sect3>
|
---|
| 94 | <title>Udev Bootscript</title>
|
---|
| 95 |
|
---|
| 96 | <para>The <command>S10udev</command> initscript takes care of creating
|
---|
| 97 | device nodes when Linux is booted. The script starts by unsetting the
|
---|
| 98 | hotplug event handler from the default of <command>/sbin/hotplug</command>
|
---|
| 99 | This is done because, instead of the kernel calling out to an external
|
---|
| 100 | binary, <command>udev</command> will listen on a netlink socket for
|
---|
| 101 | hotplug events that the kernel raises. The bootscript copies any static
|
---|
| 102 | device nodes that exist in <filename
|
---|
| 103 | class="directory">/lib/udev/devices</filename> to <filename
|
---|
| 104 | class="directory">/dev</filename>. This is necessary because some devices
|
---|
| 105 | are needed before the dynamic device handling processes are available
|
---|
| 106 | during the early stages of booting a system. Creating static device nodes
|
---|
| 107 | in <filename class="directory">/lib/udev/devices</filename> also provides
|
---|
| 108 | an easy workaround for devices that are not supported by the dynamic
|
---|
| 109 | device handling infrastructure. The bootscript then starts the Udev
|
---|
| 110 | daemon, <command>udevd</command>, which will act on any hotplug events it
|
---|
| 111 | receives. Finally, the bootscript "coldplugs" any devices that
|
---|
| 112 | have already been registered with the kernel by forcing them to raise
|
---|
| 113 | hotplug events which <command>udevd</command> will then handle.</para>
|
---|
| 114 | </sect3>
|
---|
| 115 |
|
---|
| 116 | <sect3>
|
---|
| 117 | <title>Device Node Creation</title>
|
---|
| 118 |
|
---|
| 119 | <para>To obtain the right major and minor number for a device, Udev relies
|
---|
| 120 | on the information provided by <systemitem
|
---|
| 121 | class="filesystem">sysfs</systemitem> in <filename
|
---|
| 122 | class="directory">/sys</filename>. For example,
|
---|
| 123 | <filename>/sys/class/tty/vcs/dev</filename> contains the string
|
---|
| 124 | <quote>7:0</quote>. This string is used by <command>udevd</command>
|
---|
| 125 | to create a device node with major number <emphasis>7</emphasis> and minor
|
---|
| 126 | <emphasis>0</emphasis>. The names and permissions of the nodes created
|
---|
| 127 | under the <filename class="directory">/dev</filename> directory are
|
---|
| 128 | determined by rules specified in the files within the <filename
|
---|
| 129 | class="directory">/etc/udev/rules.d/</filename> directory. These are
|
---|
| 130 | numbered in a similar fashion to the LFS-Bootscripts package. If
|
---|
| 131 | <command>udevd</command> can't find a rule for the device it is creating,
|
---|
| 132 | it will default permissions to <emphasis>660</emphasis> and ownership to
|
---|
| 133 | <emphasis>root:root</emphasis>. Documentation on the syntax of the Udev
|
---|
| 134 | rules configuration files are available in
|
---|
| 135 | <filename>/usr/share/doc/udev-084/index.html</filename></para>
|
---|
| 136 | </sect3>
|
---|
| 137 |
|
---|
| 138 | <sect3>
|
---|
| 139 | <title>Module Loading</title>
|
---|
| 140 |
|
---|
| 141 | <para>If a device driver has been compiled as a module, the rules that
|
---|
| 142 | LFS installs will cause <command>udevd</command> to call out to
|
---|
| 143 | <command>/sbin/modprobe</command> with the name of the corresponding
|
---|
| 144 | module, thereby loading the driver.</para>
|
---|
| 145 | </sect3>
|
---|
| 146 |
|
---|
| 147 | <sect3>
|
---|
| 148 | <title>Handling Hotpluggable/Dynamic Devices</title>
|
---|
| 149 |
|
---|
| 150 | <para>When you plug in a device, such as a Universal Serial Bus (USB) MP3
|
---|
| 151 | player, the kernel recognizes that the device is now connected and
|
---|
| 152 | generates a hotplug event. This hotplug event is then handled by
|
---|
| 153 | <command>udevd</command> as described above.</para>
|
---|
| 154 | </sect3>
|
---|
| 155 |
|
---|
| 156 | <!-- FIXME: These are questions Matt thought of while rewriting this page
|
---|
| 157 | to reflect the hotplug-less setup but didn't have time to investigate
|
---|
| 158 | straight away.
|
---|
| 159 | <sect3>
|
---|
| 160 | <title>Questions?</title>
|
---|
| 161 |
|
---|
| 162 | <para>7.4.2.3: Are default ownership/permissions still 0660 root:root? I
|
---|
| 163 | thought they'd changed, but can't be sure. Running without a config file
|
---|
| 164 | will prove this pretty quickly.</para>
|
---|
| 165 |
|
---|
| 166 | <para>7.4.2.4: How does <command>udevd</command> know which driver to
|
---|
| 167 | load, i.e. the correct module name? Is it in the hotplug event? I don't
|
---|
| 168 | think it can be in /sys as that won't be populated yet (it's the driver
|
---|
| 169 | itself that populates /sys, after all).</para>
|
---|
| 170 |
|
---|
| 171 | <para>Is the S05modules script still required? If so, what are the use
|
---|
| 172 | cases for it?</para>
|
---|
| 173 |
|
---|
| 174 | </sect3> -->
|
---|
[d781ffb] | 175 |
|
---|
| 176 | </sect2>
|
---|
| 177 |
|
---|
| 178 | <sect2>
|
---|
| 179 | <title>Problems with Creating Devices</title>
|
---|
| 180 |
|
---|
| 181 | <para>There are a few known problems when it comes to automatically creating
|
---|
| 182 | device nodes:</para>
|
---|
| 183 |
|
---|
[5e9d88c] | 184 | <para>1) A kernel driver might not export its data to <systemitem
|
---|
[d781ffb] | 185 | class="filesystem">sysfs</systemitem>.</para>
|
---|
| 186 |
|
---|
| 187 | <para>This is most common with third party drivers from outside the kernel
|
---|
| 188 | tree. Udev will be unable to automatically create device nodes for such
|
---|
[ae20451] | 189 | drivers. Create a static device node in
|
---|
| 190 | <filename>/lib/udev/devices</filename> with the appropriate major/minor
|
---|
| 191 | numbers (see the file <filename>devices.txt</filename> inside the kernel
|
---|
| 192 | documentation or the documentation provided by the third party driver
|
---|
| 193 | vendor). The static device node will be copied to
|
---|
| 194 | <filename class="directory">/dev</filename> by the
|
---|
| 195 | <command>S10udev</command> bootscript.</para>
|
---|
[d781ffb] | 196 |
|
---|
| 197 | <para>2) A non-hardware device is required. This is most common with
|
---|
| 198 | the Advanced Linux Sound Architecture (ALSA) project's Open Sound
|
---|
| 199 | System (OSS) compatibility module. These types of devices can be
|
---|
| 200 | handled in one of two ways:</para>
|
---|
| 201 |
|
---|
| 202 | <itemizedlist>
|
---|
| 203 |
|
---|
| 204 | <listitem>
|
---|
| 205 | <para>Adding the module names to
|
---|
| 206 | <filename>/etc/sysconfig/modules</filename></para>
|
---|
| 207 | </listitem>
|
---|
| 208 |
|
---|
| 209 | <listitem>
|
---|
| 210 | <para>Using an <quote>install</quote> line in
|
---|
| 211 | <filename>/etc/modprobe.conf</filename>. This tells the
|
---|
| 212 | <command>modprobe</command> command <quote>when loading this module,
|
---|
| 213 | also load this other module, at the same time.</quote>
|
---|
| 214 | For example:</para>
|
---|
[81fd230] | 215 |
|
---|
[0ebe9fa4] | 216 | <screen role="nodump"><userinput>install snd-pcm modprobe -i snd-pcm ; modprobe \
|
---|
[81fd230] | 217 | snd-pcm-oss ; true</userinput></screen>
|
---|
| 218 |
|
---|
[d781ffb] | 219 | <para>This will cause the system to load both the
|
---|
| 220 | <emphasis>snd-pcm</emphasis> and <emphasis>snd-pcm-oss</emphasis>
|
---|
| 221 | modules when any request is made to load the driver
|
---|
| 222 | <emphasis>snd-pcm</emphasis>.</para>
|
---|
| 223 | </listitem>
|
---|
[81fd230] | 224 |
|
---|
[d781ffb] | 225 | </itemizedlist>
|
---|
[81fd230] | 226 |
|
---|
[d781ffb] | 227 | </sect2>
|
---|
[81fd230] | 228 |
|
---|
[d781ffb] | 229 | <sect2>
|
---|
| 230 | <title>Useful Reading</title>
|
---|
[81fd230] | 231 |
|
---|
[d781ffb] | 232 | <para>Additional helpful documentation is available at the following
|
---|
| 233 | sites:</para>
|
---|
[81fd230] | 234 |
|
---|
[d781ffb] | 235 | <itemizedlist>
|
---|
[bce08ef] | 236 |
|
---|
[d781ffb] | 237 | <listitem>
|
---|
| 238 | <para>A Userspace Implementation of <systemitem class="filesystem">devfs</systemitem>
|
---|
| 239 | <ulink url="http://www.kroah.com/linux/talks/ols_2003_udev_paper/Reprint-Kroah-Hartman-OLS2003.pdf"/></para>
|
---|
| 240 | </listitem>
|
---|
| 241 |
|
---|
| 242 | <listitem>
|
---|
| 243 | <para>udev FAQ
|
---|
| 244 | <ulink url="http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev-FAQ"/></para>
|
---|
| 245 | </listitem>
|
---|
| 246 |
|
---|
| 247 | <listitem>
|
---|
| 248 | <para>The Linux Kernel Driver Model
|
---|
| 249 | <ulink url="http://public.planetmirror.com/pub/lca/2003/proceedings/papers/Patrick_Mochel/Patrick_Mochel.pdf"/></para>
|
---|
| 250 | </listitem>
|
---|
[bce08ef] | 251 |
|
---|
[d781ffb] | 252 | </itemizedlist>
|
---|
| 253 |
|
---|
| 254 | </sect2>
|
---|
| 255 |
|
---|
| 256 | </sect1>
|
---|