chapter04.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <chapter id="chapter-preparation" xreflabel="Chapter 4">
  2. <title>Last preparations</title>
  3. <?dbhtml filename="chapter04.html" dir="chapter04"?>
  4. <sect1 id="prepare-aboutlfs">
  5. <title>About $LFS</title>
  6. <?dbhtml filename="aboutlfs.html" dir="chapter04"?>
  7. <para>Throughout this book the environment variable LFS will be used several
  8. times. It is paramount that this variable is always defined. It should be set
  9. to the mount point you chose for your LFS partition. Check that your LFS
  10. variable is set up properly with:</para>
  11. <screen><userinput>echo $LFS</userinput></screen>
  12. <para>Make sure the output shows the path to your LFS partition's mount
  13. point, which is <filename class="directory">/mnt/lfs</filename> if you
  14. followed our example. If the output is wrong, you can always set the variable
  15. with:</para>
  16. <screen><userinput>export LFS=/mnt/lfs</userinput></screen>
  17. <para>Having this variable set means that if you are told to run a command like
  18. <userinput>mkdir $LFS/tools</userinput>, you can type it literally. Your shell
  19. will replace "$LFS" with "/mnt/lfs" (or whatever you set the variable to) when
  20. it processes the command line.</para>
  21. </sect1>
  22. <sect1 id="prepare-creatingtoolsdir">
  23. <title>Creating the $LFS/tools directory</title>
  24. <?dbhtml filename="creatingtoolsdir.html" dir="chapter05"?>
  25. <para>All programs compiled in this chapter will be installed under <filename
  26. class="directory">$LFS/tools</filename> to keep them separate from the
  27. programs compiled in the next chapter. The programs compiled here are only
  28. temporary tools and won't be a part of the final LFS system and by keeping them
  29. in a separate directory, we can later easily throw them away.</para>
  30. <para>Later on you might wish to search through the binaries of your system to
  31. see what files they make use of or link against. To make this searching easier
  32. you may want to choose a unique name for the directory in which the temporary
  33. tools are stored. Instead of the simple "tools" you could use something like
  34. "tools-for-lfs". However, you'll need to be careful to adjust all references to
  35. "tools" throughout the book -- including those in any patches, notably the
  36. GCC Specs Patch.</para>
  37. <para>Create the required directory by running the following:</para>
  38. <screen><userinput>mkdir $LFS/tools</userinput></screen>
  39. <para>The next step is to create a <filename>/tools</filename> symlink on
  40. your host system. It will point to the directory we just created on the LFS
  41. partition:</para>
  42. <screen><userinput>ln -s $LFS/tools /</userinput></screen>
  43. <note><para>The above command is correct. The <command>ln</command> command
  44. has a few syntactic variations, so be sure to check the info page before
  45. reporting what you may think is an error.</para></note>
  46. <para>The created symlink enables us to compile our toolchain so that it always
  47. refers to <filename>/tools</filename>, meaning that the compiler, assembler
  48. and linker will work both in this chapter (when we are still using some tools
  49. from the host) <emphasis>and</emphasis> in the next (when we are chrooted to
  50. the LFS partition).</para>
  51. </sect1>
  52. <sect1 id="prepar-addinguser">
  53. <title>Adding the user lfs</title>
  54. <?dbhtml filename="addinguser.html" dir="chapter05"?>
  55. <para>When logged in as <emphasis>root</emphasis>, making a single mistake
  56. can damage or even wreck your system. Therefore we recommend that you
  57. build the packages in this chapter as an unprivileged user. You could
  58. of course use your own user name, but to make it easier to set up a clean
  59. work environment we'll create a new user <emphasis>lfs</emphasis> and
  60. use this one during the installation process. As <emphasis>root</emphasis>,
  61. issue the following command to add the new user:</para>
  62. <screen><userinput>useradd -s /bin/bash -m -k /dev/null lfs</userinput></screen>
  63. <para>The meaning of the switches:</para>
  64. <itemizedlist>
  65. <listitem><para><userinput>-s /bin/bash</userinput>: This makes
  66. <userinput>bash</userinput> the default shell for user
  67. <emphasis>lfs</emphasis>.</para></listitem>
  68. <listitem><para><userinput>-m -k /dev/null</userinput>: These create a home
  69. directory for <emphasis>lfs</emphasis>, while preventing the files from a
  70. possible <filename>/etc/skel</filename> being copied into it.</para></listitem>
  71. </itemizedlist>
  72. <para>If you want to be able to log in as <emphasis>lfs</emphasis>, then give
  73. this new user a password:</para>
  74. <screen><userinput>passwd lfs</userinput></screen>
  75. <para>Now grant this new user <emphasis>lfs</emphasis> full access to
  76. <filename class="directory">$LFS/tools</filename> by giving it ownership
  77. of the directory:</para>
  78. <screen><userinput>chown lfs $LFS/tools</userinput></screen>
  79. <para>If you made a separate working directory as suggested, give user
  80. <emphasis>lfs</emphasis> ownership of this directory too:</para>
  81. <screen><userinput>chown lfs $LFS/sources</userinput></screen>
  82. <para>Next, login as user <emphasis>lfs</emphasis>. This can be done via a
  83. virtual console, through a display manager, or with the following substitute
  84. user command:</para>
  85. <screen><userinput>su - lfs</userinput></screen>
  86. <para>The "<command>-</command>" instructs <command>su</command> to start a
  87. <emphasis>login</emphasis> shell.</para>
  88. </sect1>
  89. <sect1 id="prepare-settingenvironment">
  90. <title>Setting up the environment</title>
  91. <?dbhtml filename="settingenvironment.html" dir="chapter05"?>
  92. <para>We're going to set up a good working environment by creating two new
  93. startup files for the <command>bash</command> shell. While logged in as
  94. user <emphasis>lfs</emphasis>, issue the following command to create a new
  95. <filename>.bash_profile</filename>:</para>
  96. <screen><userinput>cat &gt; ~/.bash_profile &lt;&lt; "EOF"</userinput>
  97. exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
  98. <userinput>EOF</userinput></screen>
  99. <para>Normally, when you log on as user <emphasis>lfs</emphasis>,
  100. the initial shell is a <emphasis>login</emphasis> shell which reads the
  101. <filename>/etc/profile</filename> of your host (probably containing some
  102. settings of environment variables) and then <filename>.bash_profile</filename>.
  103. The <command>exec env -i ... /bin/bash</command> command in the latter file
  104. replaces the running shell with a new one with a completely empty environment,
  105. except for the HOME, TERM and PS1 variables. This ensures that no unwanted and
  106. potentially hazardous environment variables from the host system leak into our
  107. build environment. The technique used here is a little strange, but it achieves
  108. the goal of enforcing a clean environment.</para>
  109. <para>The new instance of the shell is a <emphasis>non-login</emphasis> shell,
  110. which doesn't read the <filename>/etc/profile</filename> or
  111. <filename>.bash_profile</filename> files, but reads the
  112. <filename>.bashrc</filename> file instead. Create this latter file now:</para>
  113. <screen><userinput>cat &gt; ~/.bashrc &lt;&lt; "EOF"</userinput>
  114. set +h
  115. umask 022
  116. LFS=/mnt/lfs
  117. LC_ALL=POSIX
  118. PATH=/tools/bin:/bin:/usr/bin
  119. export LFS LC_ALL PATH
  120. <userinput>EOF</userinput></screen>
  121. <para>The <command>set +h</command> command turns off
  122. <command>bash</command>'s hash function. Normally hashing is a useful
  123. feature: <command>bash</command> uses a hash table to remember the
  124. full pathnames of executable files to avoid searching the PATH time and time
  125. again to find the same executable. However, we'd like the new tools to be
  126. used as soon as they are installed. By switching off the hash function, our
  127. "interactive" commands (<command>make</command>,
  128. <command>patch</command>, <command>sed</command>,
  129. <command>cp</command> and so forth) will always use
  130. the newest available version during the build process.</para>
  131. <para>Setting the user file-creation mask to 022 ensures that newly created
  132. files and directories are only writable for their owner, but readable and
  133. executable for anyone.</para>
  134. <para>The LFS variable should of course be set to the mount point you
  135. chose.</para>
  136. <para>The LC_ALL variable controls the localization of certain programs,
  137. making their messages follow the conventions of a specified country. If your
  138. host system uses a version of Glibc older than 2.2.4,
  139. having LC_ALL set to something other than "POSIX" or "C" during this chapter
  140. may cause trouble if you exit the chroot environment and wish to return later.
  141. By setting LC_ALL to "POSIX" (or "C", the two are equivalent) we ensure that
  142. everything will work as expected in the chroot environment.</para>
  143. <para>We prepend <filename>/tools/bin</filename> to the standard PATH so
  144. that, as we move along through this chapter, the tools we build will get used
  145. during the rest of the building process.</para>
  146. <para>Finally, to have our environment fully prepared for building the
  147. temporary tools, source the just-created profile:</para>
  148. <screen><userinput>source ~/.bash_profile</userinput></screen>
  149. </sect1>
  150. <sect1 id="prepare-aboutsbus">
  151. <title>About SBUs</title>
  152. <?dbhtml filename="aboutsbus.html" dir="chapter04"?>
  153. <para>Most people would like to know beforehand how long it approximately
  154. takes to compile and install each package. But "Linux from Scratch" is built
  155. on so many different systems, it is not possible to give actual times that are
  156. anywhere near accurate: the biggest package (Glibc) won't take more than
  157. twenty minutes on the fastest systems, but will take something like three days
  158. on the slowest -- no kidding. So instead of giving actual times, we've come up
  159. with the idea of using the <emphasis>Static Binutils Unit</emphasis>
  160. (abbreviated to <emphasis>SBU</emphasis>).</para>
  161. <para>It works like this: the first package you compile in this book is the
  162. statically linked Binutils in <xref linkend="chapter-temporary-tools"/>, and the time it
  163. takes to compile this package is what we call the "Static Binutils Unit" or
  164. "SBU". All other compile times will be expressed relative to this time.</para>
  165. <para>For example, the time it takes to build the static version of GCC is
  166. &gcc-time-tools-pass1;s. This means that if on your system it took 10 minutes
  167. to compile and install the static Binutils, then you know it will take
  168. approximately 45 minutes to build the static GCC. Fortunately, most build times
  169. are much shorter than the one of Binutils.</para>
  170. <para>Note that if the system compiler on your host is GCC-2 based, the SBUs
  171. listed may end up being somewhat understated. This is because the SBU is based
  172. on the very first package, compiled with the old GCC, while the rest of the
  173. system is compiled with the newer GCC-&gcc-version; which is known to be
  174. approximately 30% slower.</para>
  175. <para>Also note that SBUs don't work well for SMP-based machines. But if you're
  176. so lucky as to have multiple processors, chances are that your system is so fast
  177. that you won't mind.</para>
  178. <para>If you wish to see actual timings for specific machines, have a look at
  179. <ulink url="&lfs-root;~bdubbs/"/>.</para>
  180. </sect1>
  181. <sect1 id="prepare-abouttestsuites">
  182. <title>About the test suites</title>
  183. <?dbhtml filename="abouttestsuites.html" dir="chapter04"?>
  184. <para>Most packages provide a test suite. Running the test suite for a newly
  185. built package is generally a good idea, as it can provide a nice sanity check
  186. that everything compiled correctly. A test suite that passes its set of checks
  187. usually proves that the package is functioning as the developer intended. It
  188. does not, however, guarantee that the package is totally bug free.</para>
  189. <para>Some test suites are more important than others. For example, the test
  190. suites for the core toolchain packages -- GCC, Binutils, and Glibc -- are of
  191. the utmost importance due to their central role in a properly functioning
  192. system. But be warned, the test suites for GCC and Glibc can take a very long
  193. time to complete, especially on slower hardware.</para>
  194. <note><para>Experience has shown us that there is little to be gained from running
  195. the test suites in <xref linkend="chapter-temporary-tools"/>. There can be no
  196. escaping the fact that the host system always exerts some influence on the
  197. tests in that chapter, often causing weird and inexplicable failures. Not only
  198. that, the tools built in <xref linkend="chapter-temporary-tools"/> are
  199. temporary and eventually discarded. For the average reader of this book we
  200. recommend <emphasis>not</emphasis> to run the test suites in <xref
  201. linkend="chapter-temporary-tools"/>. The instructions for running those test
  202. suites are still provided for the benefit of testers and developers, but they
  203. are strictly optional for everyone else.</para></note>
  204. <para>A common problem when running the test suites for Binutils and GCC is
  205. running out of pseudo terminals (PTYs for short). The symptom is a very high
  206. number of failing tests. This can happen for several reasons, but the most
  207. likely cause is that the host system doesn't have the
  208. <emphasis>devpts</emphasis> file system set up correctly. We'll discuss this in
  209. more detail later on in <xref linkend="chapter-temporary-tools"/>.</para>
  210. <para>Sometimes package test suites will give false failures. You can
  211. consult the LFS Wiki at <ulink url="&wiki-root;"/> to verify that these
  212. failures are normal. This applies to all tests throughout the book.</para>
  213. </sect1>
  214. </chapter>