chapter06.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <chapter id="chapter06" xreflabel="Chapter 6">
  2. <title>Installing basic system software</title>
  3. <?dbhtml filename="chapter06.html" dir="chapter06"?>
  4. <sect1 id="ch06-introduction">
  5. <title>Introduction</title>
  6. <?dbhtml filename="introduction.html" dir="chapter06"?>
  7. <para>In this chapter we enter the building site, and start
  8. constructing our LFS system in earnest. That is, we chroot into
  9. our temporary mini Linux system, create some auxiliary things,
  10. and then start installing all the packages, one by one.</para>
  11. <para>The installation of all this software is pretty straightforward,
  12. and you will probably think it would be much shorter to give here
  13. the generic installation instructions and explain in full only the
  14. installation of those packages that require an alternate method.
  15. Although we agree with that, we nevertheless choose to give the
  16. full instructions for each and every package, simply to minimize
  17. the possibilities for mistakes.</para>
  18. <para>If you plan to use compiler optimizations in this chapter, take a look at
  19. the optimization hint at <ulink url="&hints-root;optimization.txt"/>. Compiler
  20. optimizations can make a program run slightly faster, but they may also cause
  21. compilation difficulties and even problems when running the program. If a
  22. package refuses to compile when using optimization, try to compile it without
  23. optimization and see if the problem goes away. Even if the package does compile
  24. when using optimization, there is the risk it may have been compiled incorrectly
  25. due to complex interactions between the code and build tools. In short, the
  26. small potential gains achieved in using compiler optimization are generally
  27. outweighed by the risk. First time builders of LFS are encouraged to build
  28. without custom optimizations. Your system will still be very fast and very
  29. stable at the same time.</para>
  30. <para>The order in which packages are installed in this chapter has
  31. to be strictly followed, to ensure that no program gets a path referring
  32. to <filename class="directory">/tools</filename> hard-wired into it.
  33. For the same reason, <emphasis>do not </emphasis> compile packages
  34. in parallel. Compiling in parallel may save you some time (especially on
  35. dual-CPU machines), but it could result in a program containing a
  36. hard-wired path to <filename class="directory">/tools</filename>,
  37. which will cause the program to stop working when that directory
  38. is removed.</para>
  39. </sect1>
  40. <sect1 id="ch06-chroot">
  41. <title>Entering the chroot environment</title>
  42. <?dbhtml filename="chroot.html" dir="chapter06"?>
  43. <para>It is time to enter the chroot environment in order to begin installing
  44. the packages we need. Before you can chroot, however, you need to become
  45. <emphasis>root</emphasis>, since only <emphasis>root</emphasis>
  46. can execute the <userinput>chroot</userinput> command.</para>
  47. <para>Just like earlier, ensure the LFS environment variable is set up properly
  48. by running <userinput>echo $LFS</userinput> and ensuring it shows the path to
  49. your LFS partition's mount point, which is
  50. <filename class="directory">/mnt/lfs</filename> if you followed our
  51. example.</para>
  52. <para>Become <emphasis>root</emphasis> and run the following command
  53. to enter the chroot environment:</para>
  54. <screen><userinput>chroot $LFS /tools/bin/env -i \
  55. &nbsp;&nbsp;&nbsp;&nbsp;HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
  56. &nbsp;&nbsp;&nbsp;&nbsp;PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
  57. &nbsp;&nbsp;&nbsp;&nbsp;/tools/bin/bash --login +h</userinput></screen>
  58. <para>The <userinput>-i</userinput> option given to the
  59. <userinput>env</userinput> command will clear all variables of the chroot
  60. environment. After that, only the HOME, TERM, PS1 and PATH variables are
  61. set again. The TERM=$TERM construct will set the TERM variable inside chroot
  62. to the same value as outside chroot; this variable is needed for programs
  63. like <userinput>vim</userinput> and <userinput>less</userinput> to operate
  64. properly. If you need other variables present, such as CFLAGS or CXXFLAGS,
  65. this is a good place to set them again.</para>
  66. <para>From this point on there's no need to use the LFS variable anymore,
  67. because everything you do will be restricted to the LFS file system -- since
  68. what the shell thinks is <filename class="directory">/</filename> is actually
  69. the value of <filename class="directory">$LFS</filename>, which was passed to
  70. the chroot command.</para>
  71. <para>Notice that <filename class="directory">/tools/bin</filename> comes
  72. last in the PATH. This means that a temporary tool will not be used any more
  73. as soon as its final version is installed. Well, at least when the shell
  74. doesn't remember the locations of executed binaries -- for this reason hashing
  75. is switched off by passing the <userinput>+h</userinput> option to
  76. <userinput>bash</userinput>.</para>
  77. <para>You have to make sure all the commands in the rest of this chapter and
  78. in the following chapters are run from within the chroot environment.
  79. If you ever leave this environment for any reason (rebooting for example),
  80. you must remember to again enter chroot and mount the proc and devpts
  81. filesystems (discussed later) before continuing with the installations.</para>
  82. <para>Note that the bash prompt will say "I have no name!" This is
  83. normal, as the <filename>/etc/passwd</filename> file has not been
  84. created yet.</para>
  85. </sect1>
  86. <sect1 id="ch06-changingowner">
  87. <title>Changing ownership</title>
  88. <?dbhtml filename="changingowner.html" dir="chapter06"?>
  89. <para>Right now the <filename class="directory">/tools</filename> directory
  90. is owned by the user <emphasis>lfs</emphasis>, a user that exists only on your
  91. host system. Although you will probably want to delete the
  92. <filename class="directory">/tools</filename> directory once you have
  93. finished your LFS system, you may want to keep it around, for example to
  94. build more LFS systems. But if you keep the
  95. <filename class="directory">/tools</filename> directory as it is, you end up
  96. with files owned by a user ID without a corresponding account. This is
  97. dangerous because a user account created later on could get this same user ID
  98. and would suddenly own the <filename class="directory">/tools</filename>
  99. directory and all the files therein, thus exposing these files to possible
  100. malicious manipulation.</para>
  101. <para>To avoid this issue, you could add the <emphasis>lfs</emphasis> user to
  102. your new LFS system later on when creating the <filename>/etc/passwd</filename>
  103. file, taking care to assign it the same user and group IDs as on your host
  104. system. Alternatively, you can (and the book assumes you do) assign the
  105. contents of the <filename class="directory">/tools</filename> directory to
  106. user <emphasis>root</emphasis> by running the following command:</para>
  107. <screen><userinput>chown -R 0:0 /tools</userinput></screen>
  108. <para>The command uses "0:0" instead of "root:root", because
  109. <userinput>chown</userinput> is unable to resolve the name "root" until the
  110. password file has been created.</para>
  111. </sect1>
  112. <sect1 id="ch06-creatingdirs">
  113. <title>Creating directories</title>
  114. <?dbhtml filename="creatingdirs.html" dir="chapter06"?>
  115. <para>Let's now create some structure in our LFS file system. Let's create
  116. a directory tree. Issuing the following commands will create a more or less
  117. standard tree:</para>
  118. <screen><userinput>mkdir -p /{bin,boot,dev/{pts,shm},etc/opt,home,lib,mnt,proc}
  119. mkdir -p /{root,sbin,tmp,usr/local,var,opt}
  120. for dirname in /usr /usr/local
  121. &nbsp;&nbsp;&nbsp;&nbsp;do
  122. &nbsp;&nbsp;&nbsp;&nbsp;mkdir $dirname/{bin,etc,include,lib,sbin,share,src}
  123. &nbsp;&nbsp;&nbsp;&nbsp;ln -s share/{man,doc,info} $dirname
  124. &nbsp;&nbsp;&nbsp;&nbsp;mkdir $dirname/share/{dict,doc,info,locale,man}
  125. &nbsp;&nbsp;&nbsp;&nbsp;mkdir $dirname/share/{nls,misc,terminfo,zoneinfo}
  126. &nbsp;&nbsp;&nbsp;&nbsp;mkdir $dirname/share/man/man{1,2,3,4,5,6,7,8}
  127. done
  128. mkdir /var/{lock,log,mail,run,spool}
  129. mkdir -p /var/{tmp,opt,cache,lib/misc,local}
  130. mkdir /opt/{bin,doc,include,info}
  131. mkdir -p /opt/{lib,man/man{1,2,3,4,5,6,7,8}}</userinput></screen>
  132. <para>Directories are, by default, created with permission mode 755, but this
  133. isn't desirable for all directories. We will make two changes: one to the home
  134. directory of <emphasis>root</emphasis>, and another to the directories for
  135. temporary files.</para>
  136. <screen><userinput>chmod 0750 /root
  137. chmod 1777 /tmp /var/tmp</userinput></screen>
  138. <para>The first mode change ensures that not just anybody can enter the
  139. <filename class="directory">/root</filename> directory -- the same
  140. as a normal user would do with his or her home directory.
  141. The second mode change makes sure that any user can write to the
  142. <filename class="directory">/tmp</filename> and
  143. <filename class="directory">/var/tmp</filename> directories, but
  144. cannot remove other users' files from them. The latter is prohibited
  145. by the so-called "sticky bit" -- the highest bit in the 1777 bit mask.</para>
  146. <sect2>
  147. <title>FHS compliance note</title>
  148. <para>We have based our directory tree on the FHS standard (available at
  149. <ulink url="http://www.pathname.com/fhs/"/>). Besides the above created
  150. tree this standard stipulates the existence of
  151. <filename class="directory">/usr/local/games</filename> and
  152. <filename class="directory">/usr/share/games</filename>, but we don't
  153. much like these for a base system. However, feel free to make your system
  154. FHS-compliant. As to the structure of the
  155. <filename class="directory">/usr/local/share</filename> subdirectory, the FHS
  156. isn't precise, so we created here the directories that we think are needed.</para>
  157. </sect2>
  158. </sect1>
  159. &c6-mountproc;
  160. <sect1 id="ch06-createfiles">
  161. <title>Creating essential symlinks</title>
  162. <?dbhtml filename="createfiles.html" dir="chapter06"?>
  163. <para>Some programs hard-wire paths to programs which don't exist yet. In
  164. order to satisfy these programs, we create a number of symbolic links which
  165. will be replaced by real files throughout the course of this chapter when
  166. we're installing all the software.</para>
  167. <screen><userinput>ln -s /tools/bin/{bash,cat,pwd,stty} /bin
  168. ln -s /tools/bin/perl /usr/bin
  169. ln -s /tools/lib/libgcc_s.so.1 /usr/lib
  170. ln -s bash /bin/sh</userinput></screen>
  171. </sect1>
  172. <sect1 id="ch06-pwdgroup">
  173. <title>Creating the passwd and group files</title>
  174. <?dbhtml filename="pwdgroup.html" dir="chapter06"?>
  175. <para>In order for <emphasis>root</emphasis> to be able to login and for the
  176. name "root" to be recognized, there need to be relevant entries in the
  177. <filename>/etc/passwd</filename> and <filename>/etc/group</filename> files.</para>
  178. <para>Create the <filename>/etc/passwd</filename> file by running the following
  179. command:</para>
  180. <screen><userinput>cat &gt; /etc/passwd &lt;&lt; "EOF"</userinput>
  181. root:x:0:0:root:/root:/bin/bash
  182. <userinput>EOF</userinput></screen>
  183. <para>The actual password for <emphasis>root</emphasis> (the "x" here is just a
  184. placeholder) will be set later.</para>
  185. <para>Create the <filename>/etc/group</filename> file by running the following
  186. command:</para>
  187. <screen><userinput>cat &gt; /etc/group &lt;&lt; "EOF"</userinput>
  188. root:x:0:
  189. bin:x:1:
  190. sys:x:2:
  191. kmem:x:3:
  192. tty:x:4:
  193. tape:x:5:
  194. daemon:x:6:
  195. floppy:x:7:
  196. disk:x:8:
  197. lp:x:9:
  198. dialout:x:10:
  199. audio:x:11:
  200. <userinput>EOF</userinput></screen>
  201. <para>The created groups aren't part of any standard -- they are the groups
  202. that the MAKEDEV script in the next section uses. Besides the group "root", the
  203. LSB (<ulink url="http://www.linuxbase.org"/>) recommends only a group "bin",
  204. with a GID of 1, be present. All other group names and GIDs can be chosen
  205. freely by the user, as well-written packages don't depend on GID numbers but
  206. use the group's name.</para>
  207. <para>Lastly, we re-login to the chroot environment. User name and group name
  208. resolution will start working immediately after the
  209. <filename>/etc/passwd</filename> and <filename>/etc/group</filename> files are
  210. created, because we installed a full Glibc in Chapter 5. This will get rid of
  211. the <quote>I have no name!</quote> prompt.</para>
  212. <screen><userinput>exec /tools/bin/bash --login +h</userinput></screen>
  213. <para>Note the use of the <userinput>+h</userinput> directive. This tells
  214. <userinput>bash</userinput> not to use its internal path hashing. Without this
  215. directive, <userinput>bash</userinput> would remember the paths to binaries it
  216. has executed. Since we want to use our newly compiled binaries as soon as
  217. they are installed, we turn off this function for the duration of this
  218. chapter.</para>
  219. </sect1>
  220. &c6-makedev;
  221. &c6-kernel;
  222. &c6-manpages;
  223. &c6-glibc;
  224. <sect1 id="ch06-adjustingtoolchain">
  225. <title>Re-adjusting the toolchain</title>
  226. <?dbhtml filename="adjustingtoolchain.html" dir="chapter06"?>
  227. <para>Now that the new C libraries have been installed, it's time to re-adjust
  228. our toolchain. We'll adjust it so that it will link any newly compiled program
  229. against the new C libraries. Basically, this is the reverse of what we did
  230. in the "locking in" stage in the beginning of the previous chapter.</para>
  231. <para>The first thing to do is to adjust the linker. For this we retained the
  232. source and build directories from the second pass over Binutils. Install the
  233. adjusted linker by running the following from within the
  234. <filename class="directory">binutils-build</filename> directory:</para>
  235. <screen><userinput>make -C ld INSTALL=/tools/bin/install install</userinput></screen>
  236. <note><para>If you somehow missed the earlier warning to retain the Binutils
  237. source and build directories from the second pass in Chapter 5 or otherwise
  238. accidentally deleted them or just don't have access to them, don't worry, all is
  239. not lost. Just ignore the above command. The result will be that the next
  240. package, Binutils, will link against the Glibc libraries in
  241. <filename class="directory">/tools</filename> rather than
  242. <filename class="directory">/usr</filename>. This is not ideal, however, our
  243. testing has shown that the resulting Binutils program binaries should be
  244. identical.</para></note>
  245. <para>From now on every compiled program will link <emphasis>only</emphasis>
  246. against the libraries in <filename>/usr/lib</filename> and
  247. <filename>/lib</filename>. The extra
  248. <userinput>INSTALL=/tools/bin/install</userinput> is needed because the Makefile
  249. created during the second pass still contains the reference to
  250. <filename>/usr/bin/install</filename>, which we obviously haven't installed yet.
  251. Some host distributions contain a <filename class="symlink">ginstall</filename>
  252. symbolic link which takes precedence in the Makefile and thus can cause a
  253. problem here. The above command takes care of this also.</para>
  254. <para>You can now remove the Binutils source and build directories.</para>
  255. <para>The next thing to do is to amend our GCC specs file so that it points
  256. to the new dynamic linker. Just like earlier on, we use a sed to accomplish
  257. this:</para>
  258. <!-- Ampersands are needed to allow cut and paste -->
  259. <screen><userinput>SPECFILE=/tools/lib/gcc-lib/*/*/specs &amp;&amp;
  260. sed -e 's@ /tools/lib/ld-linux.so.2@ /lib/ld-linux.so.2@g' \
  261. &nbsp;&nbsp;&nbsp;&nbsp;$SPECFILE &gt; newspecfile &amp;&amp;
  262. mv -f newspecfile $SPECFILE &amp;&amp;
  263. unset SPECFILE</userinput></screen>
  264. <para>Again, cutting and pasting the above is recommended. And just like
  265. before, it is a good idea to check the specs file to ensure the intended
  266. changes were actually made.</para>
  267. <important><para>If you are working on a platform where the name of the dynamic
  268. linker is something other than <filename>ld-linux.so.2</filename>, you
  269. <emphasis>must</emphasis> substitute <filename>ld-linux.so.2</filename> with the
  270. name of your platform's dynamic linker in the above commands. Refer back to
  271. <xref linkend="ch05-toolchaintechnotes"/> if necessary.</para></important>
  272. <!-- HACK - Force some whitespace to appease tidy -->
  273. <literallayout></literallayout>
  274. <caution><para>It is imperative at this point to stop and ensure that the
  275. basic functions (compiling and linking) of the adjusted toolchain are working
  276. as expected. For this we are going to perform a simple sanity check:</para>
  277. <screen><userinput>echo 'main(){}' &gt; dummy.c
  278. gcc dummy.c
  279. readelf -l a.out | grep ': /lib'</userinput></screen>
  280. <para>If everything is working correctly, there should be no errors, and the
  281. output of the last command will be:</para>
  282. <blockquote><screen>[Requesting program interpreter: /lib/ld-linux.so.2]</screen></blockquote>
  283. <para>If you did not receive the output as shown above, or received no output at
  284. all, then something is seriously wrong. You will need to investigate and retrace
  285. your steps to find out where the problem is and correct it. There is no point in
  286. continuing until this is done. Most likely something went wrong with the specs
  287. file amendment above. Note especially that <filename>/lib</filename> now appears
  288. as the prefix of our dynamic linker. Of course, if you are working on a platform
  289. where the name of the dynamic linker is something other than
  290. <filename>ld-linux.so.2</filename>, then the output will be slightly
  291. different.</para>
  292. <para>Once you are satisfied that all is well, clean up the test files:</para>
  293. <screen><userinput>rm dummy.c a.out</userinput></screen>
  294. </caution>
  295. <!-- HACK - Force some whitespace to appease tidy -->
  296. <literallayout></literallayout>
  297. </sect1>
  298. &c6-binutils;
  299. &c6-gcc;
  300. &c6-coreutils;
  301. &c6-zlib;
  302. &c6-lfs-utils;
  303. &c6-findutils;
  304. &c6-gawk;
  305. &c6-ncurses;
  306. &c6-vim;
  307. &c6-m4;
  308. &c6-bison;
  309. &c6-less;
  310. &c6-groff;
  311. &c6-sed;
  312. &c6-flex;
  313. &c6-gettext;
  314. &c6-nettools;
  315. &c6-inetutils;
  316. &c6-perl;
  317. &c6-texinfo;
  318. &c6-autoconf;
  319. &c6-automake;
  320. &c6-bash;
  321. &c6-file;
  322. &c6-libtool;
  323. &c6-bzip2;
  324. &c6-diffutils;
  325. &c6-ed;
  326. &c6-kbd;
  327. &c6-e2fsprogs;
  328. &c6-grep;
  329. &c6-grub;
  330. &c6-gzip;
  331. &c6-man;
  332. &c6-make;
  333. &c6-modutils;
  334. &c6-patch;
  335. &c6-procinfo;
  336. &c6-procps;
  337. &c6-psmisc;
  338. &c6-shadowpwd;
  339. &c6-sysklogd;
  340. &c6-sysvinit;
  341. &c6-tar;
  342. &c6-utillinux;
  343. &c6-gcc-2953;
  344. <sect1 id="ch06-revisedchroot">
  345. <title>Revised chroot command</title>
  346. <?dbhtml filename="revisedchroot.html" dir="chapter06"?>
  347. <para>From now on when you exit the chroot environment and wish to re-enter
  348. it, you should run the following modified chroot command:</para>
  349. <screen><userinput>chroot $LFS /usr/bin/env -i \
  350. &nbsp;&nbsp;&nbsp;&nbsp;HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
  351. &nbsp;&nbsp;&nbsp;&nbsp;PATH=/bin:/usr/bin:/sbin:/usr/sbin \
  352. &nbsp;&nbsp;&nbsp;&nbsp;/bin/bash --login</userinput></screen>
  353. <para>The reason being there is no longer any need to use programs from the
  354. <filename class="directory">/tools</filename> directory. However, we don't
  355. want to remove the <filename class="directory">/tools</filename> directory
  356. just yet. There is still some use for it towards the end of the book.</para>
  357. </sect1>
  358. &c6-bootscripts;
  359. &c6-aboutdebug;
  360. </chapter>