settingenviron.xml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  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-preps-settingenviron">
  8. <?dbhtml filename="settingenvironment.html"?>
  9. <title>Setting Up the Environment</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. <screen><userinput>cat &gt; ~/.bash_profile &lt;&lt; "EOF"
  15. <literal>exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash</literal>
  16. EOF</userinput></screen>
  17. <para>When logged on as user <systemitem class="username">lfs</systemitem>,
  18. the initial shell is usually a <emphasis>login</emphasis> shell which reads
  19. the <filename>/etc/profile</filename> of the host (probably containing some
  20. settings and environment variables) and then <filename>.bash_profile</filename>.
  21. The <command>exec env -i.../bin/bash</command> command in the
  22. <filename>.bash_profile</filename> file replaces the running shell with a new
  23. one with a completely empty environment, except for the <envar>HOME</envar>,
  24. <envar>TERM</envar>, and <envar>PS1</envar> variables. This ensures that no
  25. unwanted and potentially hazardous environment variables from the host system
  26. leak into the build environment. The technique used here achieves the goal of
  27. ensuring a clean environment.</para>
  28. <para>The new instance of the shell is a <emphasis>non-login</emphasis>
  29. shell, which does not read, and execute, the conten of <filename>/etc/profile</filename> or
  30. <filename>.bash_profile</filename> files, but rather reads, and executes, the
  31. <filename>.bashrc</filename> file instead. Create the
  32. <filename>.bashrc</filename> file now:</para>
  33. <screen><userinput>cat &gt; ~/.bashrc &lt;&lt; "EOF"
  34. <literal>set +h
  35. umask 022
  36. LFS=/mnt/lfs
  37. LC_ALL=POSIX
  38. LFS_TGT=$(uname -m)-lfs-linux-gnu
  39. PATH=/usr/bin
  40. if [ ! -L /bin ]; then PATH=/bin:$PATH; fi
  41. PATH=$LFS/tools/bin:$PATH
  42. export LFS LC_ALL LFS_TGT PATH</literal>
  43. EOF</userinput></screen>
  44. <variablelist>
  45. <title>The meaning of the settings in <filename>.bashrc</filename></title>
  46. <varlistentry>
  47. <term><parameter>set +h</parameter></term>
  48. <listitem>
  49. <para>The <command>set +h</command> command turns off
  50. <command>bash</command>'s hash function. Hashing is ordinarily a useful
  51. feature&mdash;<command>bash</command> uses a hash table to remember the
  52. full path of executable files to avoid searching the <envar>PATH</envar>
  53. time and again to find the same executable. However, the new tools should
  54. be used as soon as they are installed. By switching off the hash function,
  55. the shell will always search the <envar>PATH</envar> when a program is to
  56. be run. As such, the shell will find the newly compiled tools in
  57. <filename class="directory">~/tools</filename> as soon as they are
  58. available without remembering a previous version of the same program in a
  59. different location.</para>
  60. </listitem>
  61. </varlistentry>
  62. <varlistentry>
  63. <term><parameter>umask 022</parameter></term>
  64. <listitem>
  65. <para>Setting the user file-creation mask (umask) to 022 ensures that newly
  66. created files and directories are only writable by their owner, but are
  67. readable and executable by anyone (assuming default modes are used by the
  68. <function>open(2)</function> system call, new files will end up with permission
  69. mode 644 and directories with mode 755).</para>
  70. </listitem>
  71. </varlistentry>
  72. <varlistentry>
  73. <term><parameter>LFS=/mnt/lfs</parameter></term>
  74. <listitem>
  75. <para>The <envar>LFS</envar> variable should be set to the chosen mount
  76. point.</para>
  77. </listitem>
  78. </varlistentry>
  79. <varlistentry>
  80. <term><parameter>LC_ALL=POSIX</parameter></term>
  81. <listitem>
  82. <para>The <envar>LC_ALL</envar> variable controls the localization of certain
  83. programs, making their messages follow the conventions of a specified country.
  84. Setting <envar>LC_ALL</envar> to <quote>POSIX</quote> or <quote>C</quote>
  85. (the two are equivalent) ensures that everything will work as expected in
  86. the chroot environment.</para>
  87. </listitem>
  88. </varlistentry>
  89. <varlistentry>
  90. <term><parameter>LFS_TGT=(uname -m)-lfs-linux-gnu</parameter></term>
  91. <listitem>
  92. <para>The <envar>LFS_TGT</envar> variable sets a non-default, but compatible machine
  93. description for use when building our cross compiler and linker and when cross
  94. compiling our temporary toolchain. More information is contained in
  95. <xref linkend="ch-tools-toolchaintechnotes" role=""/>.</para>
  96. </listitem>
  97. </varlistentry>
  98. <varlistentry>
  99. <term><parameter>PATH=/usr/bin</parameter></term>
  100. <listitem>
  101. <para>Many modern linux distributions have merged <filename
  102. class="directory">/bin</filename> and <filename
  103. class="directory">/usr/bin</filename>. When this is the case, the standard
  104. <envar>PATH</envar> variable needs just to be set to <filename
  105. class="directory">/usr/bin/</filename> for the <xref
  106. linkend="chapter-temporary-tools"/> environment. When this is not the
  107. case, the following line adds <filename class="directory">/bin</filename>
  108. to the path.</para>
  109. </listitem>
  110. </varlistentry>
  111. <varlistentry>
  112. <term><parameter>if [ ! -L /bin ]; then PATH=/bin:$PATH; fi</parameter></term>
  113. <listitem>
  114. <para>If <filename class="directory">/bin</filename> is not a symbolic
  115. link, then it has to be added to the <envar>PATH</envar> variable.</para>
  116. </listitem>
  117. </varlistentry>
  118. <varlistentry>
  119. <term><parameter>PATH=$LFS/tools/bin:$PATH</parameter></term>
  120. <listitem>
  121. <para>By putting <filename class="directory">$LFS/tools/bin</filename> ahead of the
  122. standard <envar>PATH</envar>, the cross-compiler installed at the beginning
  123. of <xref linkend="chapter-cross-tools"/> is picked up by the shell
  124. immediately after its installation. This, combined with turning off hashing,
  125. limits the risk that the compiler from the host be used instead of the
  126. cross-compiler.</para>
  127. </listitem>
  128. </varlistentry>
  129. <varlistentry>
  130. <term><parameter>export LFS LC_ALL LFS_TGT PATH</parameter></term>
  131. <listitem>
  132. <para>While the above commands have set some variables, in order
  133. to make them visible within any sub-shells, we export them.</para>
  134. </listitem>
  135. </varlistentry>
  136. </variablelist>
  137. <para>Finally, to have the environment fully prepared for building the
  138. temporary tools, source the just-created user profile:</para>
  139. <screen><userinput>source ~/.bash_profile</userinput></screen>
  140. </sect1>