1
0

symlinks.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/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>管理设备</title>
  10. <sect2 revision="sysv">
  11. <title>网络设备</title>
  12. <!--para>Udev, by default, names network devices according to Firmware/BIOS
  13. data or physical characteristics like the bus, slot, or MAC address. The
  14. purpose of this naming convention is to ensure that network devices are
  15. named consistently and not based on the time the network card was
  16. discovered. For example, on a computer having two network cards made by
  17. Intel and Realtek, the network card manufactured by Intel may become eth0
  18. and the Realtek card becomes eth1. In some cases, after a reboot the cards
  19. get renumbered the other way around.</para-->
  20. <para>Udev 在默认情况下,根据固件与 BIOS 的数据,或总线、插槽与 MAC 地址
  21. 等物理特性命名网络设备。
  22. 这种命名法的主要目的是保证网络设备在每次引导时获得一致的命名,
  23. 而不是基于网卡被系统发现的时间进行命名。
  24. 例如,在老式命名方法中,如果一台计算机拥有两块网卡,
  25. 其中一块由 Intel 生产,另一块由 Realtek 生产,
  26. 则 Intel 网卡可能被命名为 eth0 ,而 Realtek 网卡被命名为 eth1,
  27. 然而在重新启动系统后,它们的顺序可能会颠倒。</para>
  28. <!--para>In the new naming scheme, typical network device names would then
  29. be something like enp5s0 or wlp3s0. If this naming convention is not
  30. desired, the traditional naming scheme or a custom scheme can be
  31. implemented.</para-->
  32. <para>在新的命名架构中,典型的网络设备名称就像 enp5s0 或 wlp3s0 这样。
  33. 如果您不喜欢这种命名惯例,可以采用传统或自定义命名架构。</para>
  34. <sect3>
  35. <title>在内核命令行中禁用一致性命名</title>
  36. <para>传统的,如同 eth0,eth1 这样的命名架构可以通过在内核命令行中加入
  37. <userinput>net.ifnames=0</userinput> 而恢复。
  38. 这在那些没有两块同类以太网设备的机器上最为合适。
  39. 笔记本一般拥有两个以太网连接,在这种命名方式下分别是
  40. eth0 和 wlan0,也可以采用这种方法。
  41. 内核命令行需要写入 GRUB 配置文件中,参阅
  42. <xref linkend="grub-cfg"/>。</para>
  43. </sect3>
  44. <sect3>
  45. <title>创建自定义 Udev 规则</title>
  46. <!--para>The naming scheme can be customized by creating custom Udev
  47. rules. A script has been included that generates the initial rules.
  48. Generate these rules by running:</para-->
  49. <para>可以创建自定义 Udev 规则,定制命名架构。
  50. 系统中包含一个生成初始规则的脚本,执行以下命令生成初始规则:
  51. </para>
  52. <screen role="install"><userinput>bash /lib/udev/init-net-rules.sh</userinput></screen>
  53. <para> 现在检查文件
  54. <filename>/etc/udev/rules.d/70-persistent-net.rules</filename>,
  55. 确认网络设备与命名的对应关系:</para>
  56. <screen role="nodump"><userinput>cat /etc/udev/rules.d/70-persistent-net.rules</userinput></screen>
  57. <note><!--para>In some cases such as when MAC addresses have been assigned to
  58. a network card manually or in a virtual environment such as Qemu or Xen,
  59. the network rules file may not have been generated because addresses
  60. are not consistently assigned. In these cases, this method cannot
  61. be used.</para-->
  62. <para>某些情况下,例如 MAC 地址被手动指定给了某块网卡,或在 Qemu、
  63. Xen 等虚拟环境下,可能不会生成网络设备规则文件,
  64. 因为 MAC 地址的分配没有一致性。此时不能使用本方法。</para>
  65. </note>
  66. <!--para>The file begins with a comment block followed by two lines for each
  67. NIC. The first line for each NIC is a commented description showing its
  68. hardware IDs (e.g. its PCI vendor and device IDs, if it's a PCI card),
  69. along with its driver in parentheses, if the driver can be found. Neither
  70. the hardware ID nor the driver is used to determine which name to give an
  71. interface; this information is only for reference. The second line is the
  72. Udev rule that matches this NIC and actually assigns it a name.</para>
  73. <para>All Udev rules are made up of several keys, separated by commas and
  74. optional whitespace. This rule's keys and an explanation of each of them
  75. are as follows:</para-->
  76. <para>该文件的开头是一个注释块,之后对于每个网络接口设备(NIC)
  77. 都给出两行,第一行是注释,给出该 NIC 的硬件 ID (例如它的
  78. PCI 生产商 ID 和设备 ID,如果它是 PCI 设备的话),
  79. 并在括号中给出它的驱动程序,如果能找到驱动程序的话。
  80. 它们并不被用于确定该设备的命名,仅供您在编写规则时进行参考。
  81. 第二行是匹配该 NIC 的 udev 规则,和实际赋予它的设备名。</para>
  82. <para>所有 udev 规则包含若干键值,用逗号和可选的空格进行分隔。
  83. 下面给出规则中包含的键值和对它们的解释:</para>
  84. <itemizedlist>
  85. <listitem>
  86. <para><literal>SUBSYSTEM=="net"</literal> -
  87. 告诉 udev 忽略除网卡以外的设备。</para>
  88. </listitem>
  89. <listitem>
  90. <para><literal>ACTION=="add"</literal> -
  91. 告诉 udev 忽略除了<quote>添加</quote>外的所有 uevent
  92. (也存在<quote>删除</quote>或<quote>修改</quote>类型的
  93. uevent,但在这种情况下不需要重命名网络接口。</para>
  94. </listitem>
  95. <listitem>
  96. <para><literal>DRIVERS=="?*"</literal> -
  97. 这使得 udev 忽略 VLAN 或桥接子接口(它们没有驱动程序)。
  98. 它们必须被忽略,否则其命名会与父设备冲突。</para>
  99. </listitem>
  100. <listitem>
  101. <para><literal>ATTR{address}</literal> -
  102. 该键的值是 NIC 的 MAC 地址。</para>
  103. </listitem>
  104. <listitem>
  105. <para><literal>ATTR{type}=="1"</literal> -
  106. 这保证在使用创建多个虚拟接口的无线驱动程序时,
  107. 只匹配主要接口。跳过其他接口的原因和忽略 VLAN 与桥接子接口一样,
  108. 是为了防止命名冲突。</para>
  109. </listitem>
  110. <listitem>
  111. <para><literal>NAME</literal> -
  112. 该键的值是 udev 将赋予该网络接口的命名</para>
  113. </listitem>
  114. </itemizedlist>
  115. <para><literal>NAME</literal> 的值是我们关注的重点。
  116. 您在继续阅读之前,需要确保自己知道赋予每个网络接口的命名,
  117. 在之后的配置文件中需要使用这些名称。</para>
  118. </sect3>
  119. </sect2>
  120. <sect2 revision="sysv">
  121. <title>CD-ROM 符号链接</title>
  122. <!--para>Some software that you may want to install later (e.g., various
  123. media players) expect the <filename class="symlink">/dev/cdrom</filename>
  124. and <filename class="symlink">/dev/dvd</filename> symlinks to exist, and
  125. to point to a CD-ROM or DVD-ROM device. Also, it may be convenient to put
  126. references to those symlinks into <filename>/etc/fstab</filename>. Udev
  127. comes with a script that will generate rules files to create these symlinks
  128. for you, depending on the capabilities of each device, but you need to
  129. decide which of two modes of operation you wish to have the script use.</para-->
  130. <para>您之后可能希望安装的一些程序(如某些媒体播放器)预期
  131. <filename class="symlink">/dev/cdrom</filename>
  132. 和 <filename class="symlink">/dev/dvd</filename>
  133. 符号链接存在,并指向 CD-ROM 或 DVD-ROM 设备。
  134. 另外,在 <filename>/etc/fstab</filename>
  135. 中引用它们也非常方便。Udev 提供了一个脚本,
  136. 能根据每个设备的功能,为您生成创建这两个符号链接的规则。
  137. 但是,您需要确定,自己希望使用该脚本提供的两种操作模式中的哪一种。
  138. </para>
  139. <!--para>First, the script can operate in <quote>by-path</quote> mode (used by
  140. default for USB and FireWire devices), where the rules it creates depend on
  141. the physical path to the CD or DVD device. Second, it can operate in
  142. <quote>by-id</quote> mode (default for IDE and SCSI devices), where the
  143. rules it creates depend on identification strings stored in the CD or DVD
  144. device itself. The path is determined by Udev's <command>path_id</command>
  145. script, and the identification strings are read from the hardware by its
  146. <command>ata_id</command> or <command>scsi_id</command> programs, depending
  147. on which type of device you have.</para-->
  148. <para>首先,该脚本可以在 <quote>by-path</quote> 模式下运行
  149. (这是 USB 和 FireWire 设备的默认模式),此时它创建的规则依赖于
  150. CD 或 DVD 设备的物理路径。其次,它可以在 <quote>by-id</quote>
  151. 模式下运行,此时它创建的规则依赖于 CD 或 DVD
  152. 设备本身存储的识别字符串。物理路径由 udev 的
  153. <command>path_id</command> 脚本确定,
  154. 而识别字符串由 <command>ata_id</command> 或
  155. <command>scsi_id</command> 程序(根据设备类型选用其中一个)
  156. 从硬件中读取。</para>
  157. <!--para>There are advantages to each approach; the correct approach to use
  158. will depend on what kinds of device changes may happen. If you expect the
  159. physical path to the device (that is, the ports and/or slots that it plugs
  160. into) to change, for example because you plan on moving the drive to a
  161. different IDE port or a different USB connector, then you should use the
  162. <quote>by-id</quote> mode. On the other hand, if you expect the device's
  163. identification to change, for example because it may die, and you would
  164. replace it with a different device with the same capabilities and which
  165. is plugged into the same connectors, then you should use the
  166. <quote>by-path</quote> mode.</para-->
  167. <para>两种方式各有优缺点,正确地方式依赖于设备可能发生的变化。
  168. 如果您预期指向设备的物理路径(即它连接的端口或插槽)可能变化,
  169. 例如您可能会将它移动到另一个 IDE 接口或另一个 USB 接口,
  170. 您应该使用 <quote>by-id</quote> 模式。
  171. 另一方面,如果您可能用具有相同功能,
  172. 并接入相同接口的另一台设备替换它,则您应该使用
  173. <quote>by-path</quote> 模式。</para>
  174. <!--para>If either type of change is possible with your drive, then choose a
  175. mode based on the type of change you expect to happen more often.</para-->
  176. <para>如果两种变化都可能发生,则根据您预期较常发生的变化选择模式。
  177. </para>
  178. <!-- If you use by-id mode, the symlinks will survive even the transition
  179. to libata for IDE drives, but that is not for the book. -->
  180. <!--important><para>External devices (for example, a USB-connected CD drive)
  181. should not use by-path persistence, because each time the device is plugged
  182. into a new external port, its physical path will change. All
  183. externally-connected devices will have this problem if you write Udev rules
  184. to recognize them by their physical path; the problem is not limited to CD
  185. and DVD drives.</para></important-->
  186. <important>
  187. <para>外接设备(例如 USB 接口的 CD 驱动器)不应使用 by-path 模式,
  188. 因为每次将该设备插入到新的外部接口时,其物理路径都会变化。
  189. 只要您使用了基于物理路径识别外接设备的 udev 规则,
  190. 都会导致这个问题,并不仅限于 CD 或 DVD 设备。
  191. </para>
  192. </important>
  193. <para>如果您希望知道 udev 脚本会使用的路径或识别字符串值,
  194. 对于正确的 CD-ROM 驱动器,在
  195. <filename class="directory">/sys</filename> 目录中找到对应的目录
  196. (例如 <filename class="directory">/sys/block/hdd</filename>),
  197. 然后运行类似下面这样的命令:</para>
  198. <screen role="nodump"><userinput>udevadm test /sys/block/hdd</userinput></screen>
  199. <para>观察包含一些 *_id 程序输出的行。
  200. <quote>by-id</quote> 模式在 ID_SERIAL 存在且非空时会使用它,
  201. 否则就使用 ID_MODEL 和 ID_REVISION 的组合。
  202. <quote>by-path</quote> 模式会使用 ID_PATH 的值。</para>
  203. <para>如果默认模式不适合您的情况,可以像下面这样修改
  204. <filename>/etc/udev/rules.d/83-cdrom-symlinks.rules</filename> 文件
  205. (将 <replaceable>mode</replaceable> 替换成
  206. <quote>by-id</quote> 或 <quote>by-path</quote> 中的一个):</para>
  207. <screen role="nodump"><userinput>sed -i -e 's/"write_cd_rules"/"write_cd_rules <replaceable>mode</replaceable>"/' \
  208. /etc/udev/rules.d/83-cdrom-symlinks.rules</userinput></screen>
  209. <para>注意现在并不需要创建规则文件和符号链接,因为已经绑定挂载了宿主的
  210. <filename class="directory">/dev</filename> 目录,
  211. 我们假定宿主系统存在正确的符号链接。
  212. 只要在第一次引导 LFS 系统后创建规则和符号链接即可。</para>
  213. <!--para>However, if you have multiple CD-ROM devices, then the symlinks
  214. generated at that time may point to different devices than they point to on
  215. your host, because devices are not discovered in a predictable order. The
  216. assignments created when you first boot the LFS system will be stable, so
  217. this is only an issue if you need the symlinks on both systems to point to
  218. the same device. If you need that, then inspect (and possibly edit) the
  219. generated <filename>/etc/udev/rules.d/70-persistent-cd.rules</filename>
  220. file after booting, to make sure the assigned symlinks match what you need.</para-->
  221. <para>然而,如果您有多个 CD-ROM 设备,
  222. 则生成的符号链接可能指向不同于您的宿主系统的设备,
  223. 因为设备发现的顺序不可预测。在您第一次引导 LFS 系统后,
  224. 创建的设备分配将会是稳定的,因此这仅在您希望宿主系统和 LFS
  225. 中的符号链接指向同一设备时才会成为问题。
  226. 如果您需要这样,在引导后检查(如果需要的话修改)生成的
  227. <filename>/etc/udev/rules.d/70-persistent-cd.rules</filename>
  228. 文件,保证分配的符号链接和您的需要一致。</para>
  229. </sect2>
  230. <sect2>
  231. <title>处理重复设备</title>
  232. <!--para>As explained in <xref linkend="ch-scripts-udev"/>, the order in
  233. which devices with the same function appear in
  234. <filename class="directory">/dev</filename> is essentially random.
  235. E.g., if you have a USB web camera and a TV tuner, sometimes
  236. <filename>/dev/video0</filename> refers to the camera and
  237. <filename>/dev/video1</filename> refers to the tuner, and sometimes
  238. after a reboot the order changes to the opposite one.
  239. For all classes of hardware except sound cards and network cards, this is
  240. fixable by creating Udev rules for custom persistent symlinks.
  241. The case of network cards is covered separately in
  242. <xref linkend="ch-scripts-network"/>, and sound card configuration can
  243. be found in <ulink url="&blfs-book;postlfs/devices.html">BLFS</ulink>.</para-->
  244. <para>正如 <xref linkend="ch-scripts-udev"/> 中所述,
  245. 那些功能相同的设备在 <filename class="directory">/dev</filename>
  246. 中的顺序是随机的。例如,如果您有一个 USB 摄像头和一个电视棒,
  247. 有时 <filename>/dev/video0</filename> 会指向摄像头,
  248. <filename>/dev/video1</filename> 指向电视棒,
  249. 而有时在重启后这个顺序正好颠倒过来。
  250. 对于所有除了声卡和网卡以外的设备,
  251. 该问题都可以通过为自定义永久符号链接创建 Udev 规则来解决。
  252. 对于网卡的解决方案在 <xref linkend="ch-scripts-network"/>
  253. 中单独描述,而声卡配置可以在
  254. <ulink url="&blfs-book;postlfs/devices.html">BLFS</ulink>
  255. 中找到。</para>
  256. <!--para>For each of your devices that is likely to have this problem
  257. (even if the problem doesn't exist in your current Linux distribution),
  258. find the corresponding directory under
  259. <filename class="directory">/sys/class</filename> or
  260. <filename class="directory">/sys/block</filename>.
  261. For video devices, this may be
  262. <filename
  263. class="directory">/sys/class/video4linux/video<replaceable>X</replaceable></filename>.
  264. Figure out the attributes that identify the device uniquely (usually,
  265. vendor and product IDs and/or serial numbers work):</para-->
  266. <para>对于您的每个可能有这类问题的设备(即使在您当前使用的 Linux
  267. 发行版上并没有问题),找到 <filename class="directory">/sys/class
  268. </filename> 或 <filename class="directory">/sys/block</filename>
  269. 中的对应目录。对于视频设备,目录可能是
  270. <filename class="directory">
  271. /sys/class/video4linux/video<replaceable>X</replaceable>
  272. </filename>。找出能够唯一确认该设备的属性
  273. (通常是厂商和产品 ID,或者序列号):</para>
  274. <screen role="nodump"><userinput>udevadm info -a -p /sys/class/video4linux/video0</userinput></screen>
  275. <para>然后编写创建符号链接的规则,例如:</para>
  276. <screen role="nodump"><userinput>cat &gt; /etc/udev/rules.d/83-duplicate_devs.rules &lt;&lt; "EOF"
  277. <literal>
  278. # 摄像头和电视棒的持久化符号链接
  279. KERNEL=="video*", ATTRS{idProduct}=="1910", ATTRS{idVendor}=="0d81", \
  280. SYMLINK+="webcam"
  281. KERNEL=="video*", ATTRS{device}=="0x036f", ATTRS{vendor}=="0x109e", \
  282. SYMLINK+="tvtuner"
  283. </literal>
  284. EOF</userinput></screen>
  285. <!--para>The result is that <filename>/dev/video0</filename> and
  286. <filename>/dev/video1</filename> devices still refer randomly to the tuner
  287. and the web camera (and thus should never be used directly), but there are
  288. symlinks <filename>/dev/tvtuner</filename> and
  289. <filename>/dev/webcam</filename> that always point to the correct
  290. device.</para-->
  291. <para>结果是,<filename>/dev/video0</filename> 和
  292. <filename>/dev/video1</filename> 仍然会随机指向电视棒和摄像头
  293. (因此不应直接使用它们),但符号链接
  294. <filename>/dev/tvtuner</filename> 和
  295. <filename>/dev/webcam</filename> 总会指向正确设备。</para>
  296. </sect2>
  297. </sect1>