settingenviron.xml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-tools-settingenviron">
  8. <?dbhtml filename="settingenvironment.html"?>
  9. <title>配置环境</title>
  10. <!--para>Set up a good working environment by creating two new startup files
  11. for the <command>bash</command> shell. While logged in as user
  12. <systemitem class="username">lfs</systemitem>, issue the following command
  13. to create a new <filename>.bash_profile</filename>:</para-->
  14. <para>为了配置一个良好的工作环境,我们为 <command>bash</command>
  15. 创建两个新的启动脚本。以
  16. <systemitem class="username">lfs</systemitem> 的身份,执行以下命令,
  17. 创建一个新的 <filename>.bash_profile</filename>:</para>
  18. <screen><userinput>cat &gt; ~/.bash_profile &lt;&lt; "EOF"
  19. <literal>exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash</literal>
  20. EOF</userinput></screen>
  21. <!--para>When logged on as user <systemitem class="username">lfs</systemitem>,
  22. the initial shell is usually a <emphasis>login</emphasis> shell which reads
  23. the <filename>/etc/profile</filename> of the host (probably containing some
  24. settings and environment variables) and then <filename>.bash_profile</filename>.
  25. The <command>exec env -i.../bin/bash</command> command in the
  26. <filename>.bash_profile</filename> file replaces the running shell with a new
  27. one with a completely empty environment, except for the <envar>HOME</envar>,
  28. <envar>TERM</envar>, and <envar>PS1</envar> variables. This ensures that no
  29. unwanted and potentially hazardous environment variables from the host system
  30. leak into the build environment. The technique used here achieves the goal of
  31. ensuring a clean environment.</para-->
  32. <para>在以 <systemitem class="username">lfs</systemitem> 用户登录时,
  33. 初始的 shell 一般是一个 <emphasis>登录</emphasis> shell。它读取宿主系统的
  34. <filename>/etc/profile</filename> 文件 (可能包含一些设置和环境变量),
  35. 然后读取 <filename>.bash_profile</filename> 。
  36. 我们在 <filename>.bash_profile</filename> 中使用
  37. <command>exec env -i.../bin/bash</command> 命令,新建一个除了
  38. <envar>HOME</envar>, <envar>TERM</envar> 以及 <envar>PS1</envar>
  39. 外没有任何环境变量的 shell ,替换当前 shell ,
  40. 防止宿主环境中不必要和有潜在风险的环境变量进入编译环境。
  41. 通过使用以上技巧,我们创建了一个干净环境。</para>
  42. <!--para>The new instance of the shell is a <emphasis>non-login</emphasis>
  43. shell, which does not read the <filename>/etc/profile</filename> or
  44. <filename>.bash_profile</filename> files, but rather reads the
  45. <filename>.bashrc</filename> file instead. Create the
  46. <filename>.bashrc</filename> file now:</para-->
  47. <para>新的 shell 实例是 <emphasis>非登录</emphasis> shell ,它不会读取
  48. <filename>/etc/profile</filename> 或者
  49. <filename>.bash_profile</filename>,而是读取
  50. <filename>.bashrc</filename> 文件。现在我们就创建一个
  51. <filename>.bashrc</filename> 文件:</para>
  52. <screen><userinput>cat &gt; ~/.bashrc &lt;&lt; "EOF"
  53. <literal>set +h
  54. umask 022
  55. LFS=/mnt/lfs
  56. LC_ALL=POSIX
  57. LFS_TGT=$(uname -m)-lfs-linux-gnu
  58. PATH=/tools/bin:/bin:/usr/bin
  59. export LFS LC_ALL LFS_TGT PATH</literal>
  60. EOF</userinput></screen>
  61. <!--para>The <command>set +h</command> command turns off
  62. <command>bash</command>'s hash function. Hashing is ordinarily a useful
  63. feature&mdash;<command>bash</command> uses a hash table to remember the
  64. full path of executable files to avoid searching the <envar>PATH</envar>
  65. time and again to find the same executable. However, the new tools should
  66. be used as soon as they are installed. By switching off the hash function,
  67. the shell will always search the <envar>PATH</envar> when a program is to
  68. be run. As such, the shell will find the newly compiled tools in
  69. <filename class="directory">$LFS/tools</filename> as soon as they are
  70. available without remembering a previous version of the same program in a
  71. different location.</para-->
  72. <para><command>set +h</command> 命令关闭 <command>bash</command>
  73. 的散列功能。一般情况下,<command>bash</command>
  74. 使用一个散列表维护各个可执行文件的完整路径,这样就不用每次都在
  75. <envar>PATH</envar> 指定的目录中搜索可执行文件。
  76. 然而,在构建 LFS 时,我们希望总是使用最新安装的工具。
  77. 因此,需要关闭散列功能,使得 shell 在运行程序时总是搜索
  78. <envar>PATH</envar> 。这样,shell 总是能够找到
  79. <filename class="directory">$LFS/tools</filename>
  80. 目录中那些最新编译的工具,而不是使用之前记忆的另一个目录中的程序。
  81. </para>
  82. <!--para>Setting the user file-creation mask (umask) to 022 ensures that newly
  83. created files and directories are only writable by their owner, but are
  84. readable and executable by anyone (assuming default modes are used by the
  85. <function>open(2)</function> system call, new files will end up with permission
  86. mode 644 and directories with mode 755).</para-->
  87. <para>将用户的文件创建掩码 (umask) 设定为 022 ,
  88. 保证只有文件所有者可以写新创建的文件和目录,
  89. 但任何人都可读取、执行它们。
  90. (如果 <function>open(2)</function> 系统调用使用默认模式,
  91. 则新文件将具有权限码 644 ,而新目录具有权限码 755)。</para>
  92. <!--para>The <envar>LFS</envar> variable should be set to the chosen mount
  93. point.</para-->
  94. <para><envar>LFS</envar> 环境变量必须被设定为之前选择的挂载点。</para>
  95. <!--para>The <envar>LC_ALL</envar> variable controls the localization of certain
  96. programs, making their messages follow the conventions of a specified country.
  97. Setting <envar>LC_ALL</envar> to <quote>POSIX</quote> or <quote>C</quote>
  98. (the two are equivalent) ensures that everything will work as expected in
  99. the chroot environment.</para-->
  100. <para><envar>LC_ALL</envar> 环境变量控制某些程序的本地化行为,
  101. 使得它们以特定国家的语言和惯例输出消息。将该变量设置为
  102. <quote>POSIX</quote> 或者 <quote>C</quote> (这两种设置是等价的)
  103. 可以保证在 chroot 环境中所有命令的行为完全符合预期,
  104. 而与宿主的本地化设置无关。</para>
  105. <!--para>The <envar>LFS_TGT</envar> variable sets a non-default, but compatible machine
  106. description for use when building our cross compiler and linker and when cross
  107. compiling our temporary toolchain. More information is contained in
  108. <xref linkend="ch-tools-toolchaintechnotes" role=""/>.</para-->
  109. <para><envar>LFS_TGT</envar>变量设定了一个非默认,
  110. 但与宿主系统兼容的机器描述符。
  111. 该描述符被用于构建交叉编译器和交叉编译临时工具链。
  112. <xref linkend="ch-tools-toolchaintechnotes" role=""/>
  113. 包含了关于这个描述符的更多信息。</para>
  114. <!--para>By putting <filename class="directory">/tools/bin</filename> ahead of the
  115. standard <envar>PATH</envar>, all the programs installed in <xref
  116. linkend="chapter-temporary-tools"/> are picked up by the shell immediately after
  117. their installation. This, combined with turning off hashing, limits the risk
  118. that old programs are used from the host when the same programs are available in
  119. the chapter 5 environment.</para-->
  120. <para>我们将 <filename class="directory">/tools/bin</filename>
  121. 附加在默认的 <envar>PATH</envar> 环境变量之前,这样在
  122. <xref linkend="chapter-temporary-tools"/> 中,
  123. 我们一旦安装了新的程序, shell 就能立刻使用它们。
  124. 这与关闭散列功能相结合,
  125. 降低了在第 5 章环境中新程序可用时错误地使用宿主系统中旧程序的风险。
  126. </para>
  127. <!--para>Finally, to have the environment fully prepared for building the
  128. temporary tools, source the just-created user profile:</para-->
  129. <para>最后,为了完全准备好编译临时工具的环境,
  130. 指示 shell 读取刚才创建的配置文件:</para>
  131. <screen><userinput>source ~/.bash_profile</userinput></screen>
  132. </sect1>