symlinks.xml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!ENTITY % general-entities SYSTEM "../general.ent">
  5. %general-entities;
  6. ]>
  7. <sect1 id="ch-scripts-symlinks">
  8. <?dbhtml filename="symlinks.html"?>
  9. <title>Creating Custom Symlinks to Devices</title>
  10. <sect2>
  11. <title>CD-ROM symlinks</title>
  12. <para>Some software that you may want to install later (e.g., various
  13. media players) expect the <filename class="symlink">/dev/cdrom</filename>
  14. and <filename class="symlink">/dev/dvd</filename> symlinks to exist, and
  15. to point to a CD-ROM or DVD-ROM device. Also, it may be convenient to put
  16. references to those symlinks into <filename>/etc/fstab</filename>. Udev
  17. comes with a script that will generate rules files to create these symlinks
  18. for you, depending on the capabilities of each device, but you need to
  19. decide which of two modes of operation you wish to have the script use.</para>
  20. <para>First, the script can operate in <quote>by-path</quote> mode, where
  21. the rules it creates depend on the physical path to the CD or DVD device.
  22. Second, it can operate in <quote>by-id</quote> mode, where the rules it
  23. creates depend on identification strings stored in the CD or DVD device
  24. itself. The path is determined by Udev's <command>path_id</command> script,
  25. and the identification strings are read from the hardware by its
  26. <command>ata_id</command> or <command>scsi_id</command> programs, depending
  27. on which type of device you have.</para>
  28. <para>There are advantages to each approach; the correct approach to use
  29. will depend on what kinds of device changes may happen. If you expect the
  30. physical path to the device (that is, the ports and/or slots that it plugs
  31. into) to change, for example because you plan on moving the drive to a
  32. different IDE port or a different USB connector, then you should use the
  33. <quote>by-id</quote> mode. On the other hand, if you expect the device's
  34. identification to change, for example because it may die, and you would
  35. replace it with a different device with the same capabilities and which
  36. is plugged into the same connectors, then you should use the
  37. <quote>by-path</quote> mode.</para>
  38. <para>If either type of change is possible with your drive, then choose a
  39. mode based on the type of change you expect to happen more often.</para>
  40. <!-- If you use by-id mode, the symlinks will survive even the transition
  41. to libata for IDE drives, but that is not for the book. -->
  42. <important><para>External devices (for example, a USB-connected CD drive)
  43. should not use by-path persistence, because each time the device is plugged
  44. into a new external port, its physical path will change. All
  45. externally-connected devices will have this problem if you write Udev rules
  46. to recognize them by their physical path; the problem is not limited to CD
  47. and DVD drives.</para></important>
  48. <para>If you wish to see the values that the Udev scripts will use, then
  49. for the appropriate CD-ROM device, find the corresponding directory under
  50. <filename class="directory">/sys</filename> (e.g., this can be
  51. <filename class="directory">/sys/block/hdd</filename>) and
  52. run a command similar to the following:</para>
  53. <screen role="nodump"><userinput>udevtest /sys/block/hdd</userinput></screen>
  54. <para>Look at the lines containing the output of various *_id programs.
  55. The <quote>by-id</quote> mode will use the ID_SERIAL value if it exists and
  56. is not empty, otherwise it will use a combination of ID_MODEL and
  57. ID_REVISION. The <quote>by-path</quote> mode will use the ID_PATH value.</para>
  58. <para>If you choose the <quote>by-path</quote> mode, then the rules files
  59. installed by default with Udev will work. If you choose the <quote>by-id</quote>
  60. mode, then you will have to modify the
  61. <filename>/etc/udev/rules.d/75-cd-aliases-generator.rules</filename> file,
  62. as follows:</para>
  63. <screen><userinput>sed -i -e 's/write_cd_aliases/&amp; by-id/' \
  64. /etc/udev/rules.d/75-cd-aliases-generator.rules</userinput></screen>
  65. <para>Note that it is not necessary to create the rules files or symlinks
  66. at this time, because you have bind-mounted the host's
  67. <filename class="directory">/dev</filename> directory into the LFS system,
  68. and we assume the symlinks exist and are correct on the host. The rules
  69. will be created, along with the symlinks, the first time you boot your LFS
  70. system.</para>
  71. </sect2>
  72. <sect2>
  73. <title>Dealing with duplicate devices</title>
  74. <para>As explained in <xref linkend="ch-scripts-udev"/>, the order in
  75. which devices with the same function appear in
  76. <filename class="directory">/dev</filename> is essentially random.
  77. E.g., if you have a USB web camera and a TV tuner, sometimes
  78. <filename>/dev/video0</filename> refers to the camera and
  79. <filename>/dev/video1</filename> refers to the tuner, and sometimes
  80. after a reboot the order changes to the opposite one.
  81. For all classes of hardware except sound cards and network cards, this is
  82. fixable by creating udev rules for custom persistent symlinks.
  83. The case of network cards is covered separately in
  84. <xref linkend="ch-scripts-network"/>, and sound card configuration can
  85. be found in <ulink url="&blfs-root;">BLFS</ulink>.</para>
  86. <para>For each of your devices that is likely to have this problem
  87. (even if the problem doesn't exist in your current Linux distribution),
  88. find the corresponding directory under
  89. <filename class="directory">/sys/class</filename> or
  90. <filename class="directory">/sys/block</filename>.
  91. For video devices, this may be
  92. <filename
  93. class="directory">/sys/class/video4linux/video<replaceable>X</replaceable></filename>.
  94. Figure out the attributes that identify the device uniquely (usually,
  95. vendor and product IDs and/or serial numbers work):</para>
  96. <screen role="nodump"><userinput>udevinfo -a -p /sys/class/video4linux/video0</userinput></screen>
  97. <para>Then write rules that create the symlinks, e.g.:</para>
  98. <screen role="nodump"><userinput>cat &gt; /etc/udev/rules.d/83-duplicate_devs.rules &lt;&lt; EOF
  99. <literal>
  100. # Persistent symlinks for webcam and tuner
  101. KERNEL=="video*", ATTRS{idProduct}=="1910", ATTRS{idVendor}=="0d81", \
  102. SYMLINK+="webcam"
  103. KERNEL=="video*", ATTRS{device}=="0x036f", ATTRS{vendor}=="0x109e", \
  104. SYMLINK+="tvtuner"
  105. </literal>
  106. EOF</userinput></screen>
  107. <note>
  108. <para>Be aware that Udev does not recognize the backslash for line
  109. continuation. This example works properly because both the backslash
  110. and newline are ignored by the shell. This makes the shell send each
  111. rule to cat on only one line. (The shell ignores this sequence because
  112. the EOF string used in the here-document redirection is not enclosed in
  113. either double or single quotes. For more details, see the bash(1)
  114. manpage, and search it for "Here Documents".)</para>
  115. <para>If modifying Udev rules with an editor, be sure to leave each
  116. rule on one physical line.</para>
  117. </note>
  118. <para>The result is that <filename>/dev/video0</filename> and
  119. <filename>/dev/video1</filename> devices still refer randomly to the tuner
  120. and the web camera (and thus should never be used directly), but there are
  121. symlinks <filename>/dev/tvtuner</filename> and
  122. <filename>/dev/webcam</filename> that always point to the correct
  123. device.</para>
  124. <para>More information on writing Udev rules can be found in
  125. <filename>/usr/share/doc/udev-&udev-version;/index.html</filename>.</para>
  126. </sect2>
  127. </sect1>