chapter06.xml 20 KB

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