<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  <!ENTITY % general-entities SYSTEM "../general.ent">
  %general-entities;
]>

<sect1 id="ch-tools-settingenviron">
  <?dbhtml filename="settingenvironment.html"?>

  <title>配置环境</title>

  <!--para>Set up a good working environment by creating two new startup files
  for the <command>bash</command> shell. While logged in as user
  <systemitem class="username">lfs</systemitem>, issue the following command
  to create a new <filename>.bash_profile</filename>:</para-->
  <para>为了配置一个良好的工作环境，我们为 <command>bash</command>
	  创建两个新的启动脚本。以
	  <systemitem class="username">lfs</systemitem> 的身份，执行以下命令，
	  创建一个新的 <filename>.bash_profile</filename>：</para>

<screen><userinput>cat &gt; ~/.bash_profile &lt;&lt; "EOF"
<literal>exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash</literal>
EOF</userinput></screen>

  <!--para>When logged on as user <systemitem class="username">lfs</systemitem>,
  the initial shell is usually a <emphasis>login</emphasis> shell which reads
  the <filename>/etc/profile</filename> of the host (probably containing some
  settings and environment variables) and then <filename>.bash_profile</filename>.
  The <command>exec env -i.../bin/bash</command> command in the
  <filename>.bash_profile</filename> file replaces the running shell with a new
  one with a completely empty environment, except for the <envar>HOME</envar>,
  <envar>TERM</envar>, and <envar>PS1</envar> variables. This ensures that no
  unwanted and potentially hazardous environment variables from the host system
  leak into the build environment. The technique used here achieves the goal of
  ensuring a clean environment.</para-->

  <para>在以 <systemitem class="username">lfs</systemitem> 用户登录时，
  初始的 shell 一般是一个 <emphasis>登录</emphasis> shell。它读取宿主系统的
  <filename>/etc/profile</filename> 文件 (可能包含一些设置和环境变量)，
  然后读取 <filename>.bash_profile</filename> 。
  我们在 <filename>.bash_profile</filename> 中使用 
  <command>exec env -i.../bin/bash</command> 命令，新建一个除了
  <envar>HOME</envar>, <envar>TERM</envar> 以及 <envar>PS1</envar>
  外没有任何环境变量的 shell ，替换当前 shell ，
  防止宿主环境中不必要和有潜在风险的环境变量进入编译环境。
  通过使用以上技巧，我们创建了一个干净环境。</para>

  <!--para>The new instance of the shell is a <emphasis>non-login</emphasis>
  shell, which does not read the <filename>/etc/profile</filename> or
  <filename>.bash_profile</filename> files, but rather reads the
  <filename>.bashrc</filename> file instead. Create the
  <filename>.bashrc</filename> file now:</para-->
  <para>新的 shell 实例是 <emphasis>非登录</emphasis> shell ，它不会读取
	  <filename>/etc/profile</filename> 或者
	  <filename>.bash_profile</filename>，而是读取
	  <filename>.bashrc</filename> 文件。现在我们就创建一个
	  <filename>.bashrc</filename> 文件：</para>

<screen><userinput>cat &gt; ~/.bashrc &lt;&lt; "EOF"
<literal>set +h
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH</literal>
EOF</userinput></screen>

  <!--para>The <command>set +h</command> command turns off
  <command>bash</command>'s hash function. Hashing is ordinarily a useful
  feature&mdash;<command>bash</command> uses a hash table to remember the
  full path of executable files to avoid searching the <envar>PATH</envar>
  time and again to find the same executable. However, the new tools should
  be used as soon as they are installed. By switching off the hash function,
  the shell will always search the <envar>PATH</envar> when a program is to
  be run. As such, the shell will find the newly compiled tools in
  <filename class="directory">$LFS/tools</filename> as soon as they are
  available without remembering a previous version of the same program in a
  different location.</para-->
  <para><command>set +h</command> 命令关闭 <command>bash</command>
	  的散列功能。一般情况下，<command>bash</command>
	  使用一个散列表维护各个可执行文件的完整路径，这样就不用每次都在
	  <envar>PATH</envar> 指定的目录中搜索可执行文件。
	  然而，在构建 LFS 时，我们希望总是使用最新安装的工具。
	  因此，需要关闭散列功能，使得 shell 在运行程序时总是搜索
	  <envar>PATH</envar> 。这样，shell 总是能够找到
	  <filename class="directory">$LFS/tools</filename>
	  目录中那些最新编译的工具，而不是使用之前记忆的另一个目录中的程序。
  </para>

  <!--para>Setting the user file-creation mask (umask) to 022 ensures that newly
  created files and directories are only writable by their owner, but are
  readable and executable by anyone (assuming default modes are used by the
  <function>open(2)</function> system call, new files will end up with permission
  mode 644 and directories with mode 755).</para-->
  <para>将用户的文件创建掩码 (umask) 设定为 022 ，
	  保证只有文件所有者可以写新创建的文件和目录，
	  但任何人都可读取、执行它们。
	  （如果 <function>open(2)</function> 系统调用使用默认模式，
	  则新文件将具有权限码 644 ，而新目录具有权限码 755）。</para>

  <!--para>The <envar>LFS</envar> variable should be set to the chosen mount
  point.</para-->
  <para><envar>LFS</envar> 环境变量必须被设定为之前选择的挂载点。</para>

  <!--para>The <envar>LC_ALL</envar> variable controls the localization of certain
  programs, making their messages follow the conventions of a specified country.
  Setting <envar>LC_ALL</envar> to <quote>POSIX</quote> or <quote>C</quote>
  (the two are equivalent) ensures that everything will work as expected in
  the chroot environment.</para-->
  <para><envar>LC_ALL</envar> 环境变量控制某些程序的本地化行为，
	  使得它们以特定国家的语言和惯例输出消息。将该变量设置为
	  <quote>POSIX</quote> 或者 <quote>C</quote> （这两种设置是等价的）
	  可以保证在 chroot 环境中所有命令的行为完全符合预期，
	  而与宿主的本地化设置无关。</para>

  <!--para>The <envar>LFS_TGT</envar> variable sets a non-default, but compatible machine
  description for use when building our cross compiler and linker and when cross
  compiling our temporary toolchain. More information is contained in
  <xref linkend="ch-tools-toolchaintechnotes" role=""/>.</para-->
  <para><envar>LFS_TGT</envar>变量设定了一个非默认，
	  但与宿主系统兼容的机器描述符。
	  该描述符被用于构建交叉编译器和交叉编译临时工具链。
	  <xref linkend="ch-tools-toolchaintechnotes" role=""/>
	  包含了关于这个描述符的更多信息。</para>

  <!--para>By putting <filename class="directory">/tools/bin</filename> ahead of the
  standard <envar>PATH</envar>, all the programs installed in <xref
  linkend="chapter-temporary-tools"/> are picked up by the shell immediately after
  their installation. This, combined with turning off hashing, limits the risk
  that old programs are used from the host when the same programs are available in
  the chapter 5 environment.</para-->
  <para>我们将 <filename class="directory">/tools/bin</filename>
	  附加在默认的 <envar>PATH</envar> 环境变量之前，这样在
	  <xref linkend="chapter-temporary-tools"/> 中，
	  我们一旦安装了新的程序， shell 就能立刻使用它们。
	  这与关闭散列功能相结合，
	  降低了在第 5 章环境中新程序可用时错误地使用宿主系统中旧程序的风险。
  </para>

  <!--para>Finally, to have the environment fully prepared for building the
  temporary tools, source the just-created user profile:</para-->
  <para>最后，为了完全准备好编译临时工具的环境，
  指示 shell 读取刚才创建的配置文件：</para>

<screen><userinput>source ~/.bash_profile</userinput></screen>

</sect1>
