| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 | <chapter id="chapter-building-system" xreflabel="Chapter 6"><title>Installing basic system software</title><?dbhtml filename="chapter06.html" dir="chapter06"?><sect1 id="ch-system-introduction"><title>Introduction</title><?dbhtml filename="introduction.html" dir="chapter06"?><para>In this chapter we enter the building site, and startconstructing our LFS system in earnest. That is, we chroot intoour temporary mini Linux system, create some auxiliary things,and then start installing all the packages, one by one.</para> <para>The installation of all this software is pretty straightforward,and you will probably think it would be much shorter to give herethe generic installation instructions and explain in full only theinstallation of those packages that require an alternate method.Although we agree with that, we nevertheless choose to give the full instructions for each and every package, simply to minimizethe possibilities for mistakes.</para><para>The key to learning what makes a Linux system work is to knowwhat each package is used for and why the user (or the system) needs it.For this purpose for every installed package a summary of its content isgiven followed by concise descriptions of each program and library itinstalled.</para><para>If you plan to use compiler optimizations in this chapter, take a look atthe optimization hint at <ulink url="&hints-root;optimization.txt"/>. Compileroptimizations can make a program run slightly faster, but they may also causecompilation difficulties and even problems when running the program. If apackage refuses to compile when using optimization, try to compile it withoutoptimization and see if the problem goes away. Even if the package does compilewhen using optimization, there is the risk it may have been compiled incorrectlydue to complex interactions between the code and build tools. In short, thesmall potential gains achieved in using compiler optimization are generallyoutweighed by the risk. First time builders of LFS are encouraged to buildwithout custom optimizations. Your system will still be very fast and verystable at the same time.</para><para>The order in which packages are installed in this chapter hasto be strictly followed, to ensure that no program gets a path referringto <filename class="directory">/tools</filename> hard-wired into it.For the same reason, <emphasis>do not </emphasis> compile packagesin parallel. Compiling in parallel may save you some time (especially ondual-CPU machines), but it could result in a program containing ahard-wired path to <filename class="directory">/tools</filename>,which will cause the program to stop working when that directoryis removed.</para><para>Before the installation instructions each installation page gives someinformation about the package: a concise description of what it contains, howlong it will approximately take to build it, how much disk space it needsduring this building process, the official download location of the package(in case you just want to update a few of them), and which other packages itneeds in order to be built successfully. After the installation instructionsfollows a list of programs and libraries that the package installs, togetherwith a series of short descriptions of these.</para></sect1>&c6-mountproc;<sect1 id="ch-system-chroot"><title>Entering the chroot environment</title><?dbhtml filename="chroot.html" dir="chapter06"?><para>It is time to enter the chroot environment in order to begin buildingand installing your final LFS system. Still as <emphasis>root</emphasis> runthe following command to enter the small world that is, at the moment,populated with only the temporary tools:</para><screen><userinput>chroot $LFS /tools/bin/env -i \    HOME=/root TERM=$TERM PS1='\u:\w\$ ' \    PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \    /tools/bin/bash --login +h</userinput></screen><para>The <emphasis>-i</emphasis> option given to the<command>env</command> command will clear all variables of the chrootenvironment. After that, only the HOME, TERM, PS1 and PATH variables areset again. The TERM=$TERM construct will set the TERM variable inside chrootto the same value as outside chroot; this variable is needed for programslike <command>vim</command> and <command>less</command> to operateproperly. If you need other variables present, such as CFLAGS or CXXFLAGS,this is a good place to set them again.</para><para>From this point on there's no need to use the LFS variable anymore,because everything you do will be restricted to the LFS file system -- sincewhat the shell thinks is <filename class="directory">/</filename> is actuallythe value of <filename class="directory">$LFS</filename>, which was passed tothe chroot command.</para><para>Notice that <filename class="directory">/tools/bin</filename> comeslast in the PATH. This means that a temporary tool will not be used any moreas soon as its final version is installed. Well, at least when the shelldoesn't remember the locations of executed binaries -- for this reason hashingis switched off by passing the <emphasis>+h</emphasis> option to<command>bash</command>.</para><para>You have to make sure all the commands in the rest of this chapter andin the following chapters are run from within the chroot environment.If you ever leave this environment for any reason (rebooting for example),you must remember to first mount the proc and devpts file systems (discussedin the previous section) <emphasis>and</emphasis> enter chroot again beforecontinuing with the installations.</para><para>Note that the bash prompt will say "I have no name!" This is normal, asthe <filename>/etc/passwd</filename> file has not been created yet.</para></sect1><sect1 id="ch-system-changingowner"><title>Changing ownership</title><?dbhtml filename="changingowner.html" dir="chapter06"?><para>Right now the <filename class="directory">/tools</filename> directoryis owned by the user <emphasis>lfs</emphasis>, a user that exists only on yourhost system. Although you will probably want to delete the<filename class="directory">/tools</filename> directory once you havefinished your LFS system, you may want to keep it around, for example tobuild more LFS systems. But if you keep the<filename class="directory">/tools</filename> directory as it is, you end upwith files owned by a user ID without a corresponding account. This isdangerous because a user account created later on could get this same user IDand would suddenly own the <filename class="directory">/tools</filename>directory and all the files therein, thus exposing these files to possiblemalicious manipulation.</para><para>To avoid this issue, you could add the <emphasis>lfs</emphasis> user toyour new LFS system later on when creating the <filename>/etc/passwd</filename>file, taking care to assign it the same user and group IDs as on your hostsystem. Alternatively, you can (and the book assumes you do) assign thecontents of the <filename class="directory">/tools</filename> directory touser <emphasis>root</emphasis> by running the following command:</para><screen><userinput>chown -R 0:0 /tools</userinput></screen><para>The command uses "0:0" instead of "root:root", because<userinput>chown</userinput> is unable to resolve the name "root" until thepassword file has been created.</para></sect1><sect1 id="ch-system-creatingdirs"><title>Creating directories</title><?dbhtml filename="creatingdirs.html" dir="chapter06"?><para>Let's now create some structure in our LFS file system. Let's createa directory tree. Issuing the following commands will create a more or lessstandard tree:</para> <screen><userinput>mkdir -p /{bin,boot,dev/{pts,shm},etc/opt,home,lib,mnt,proc}mkdir -p /{root,sbin,tmp,usr/local,var,opt}for dirname in /usr /usr/local    do    mkdir $dirname/{bin,etc,include,lib,sbin,share,src}    ln -s share/{man,doc,info} $dirname    mkdir $dirname/share/{doc,info,locale,man}    mkdir $dirname/share/{misc,terminfo,zoneinfo}    mkdir $dirname/share/man/man{1,2,3,4,5,6,7,8}donemkdir /var/{lock,log,mail,run,spool}mkdir -p /var/{tmp,opt,cache,lib/misc,local}mkdir /opt/{bin,doc,include,info}mkdir -p /opt/{lib,man/man{1,2,3,4,5,6,7,8}}</userinput></screen><para>Directories are, by default, created with permission mode 755, but thisisn't desirable for all directories. We will make two changes: one to the homedirectory of <emphasis>root</emphasis>, and another to the directories fortemporary files.</para><screen><userinput>chmod 0750 /rootchmod 1777 /tmp /var/tmp</userinput></screen><para>The first mode change ensures that not just anybody can enter the<filename class="directory">/root</filename> directory -- the sameas a normal user would do with his or her home directory.The second mode change makes sure that any user can write to the<filename class="directory">/tmp</filename> and<filename class="directory">/var/tmp</filename> directories, butcannot remove other users' files from them. The latter is prohibitedby the so-called "sticky bit" -- the highest bit in the 1777 bit mask.</para><sect2><title>FHS compliance note</title><para>We have based our directory tree on the FHS standard (available at<ulink url="http://www.pathname.com/fhs/"/>). Besides the above createdtree this standard stipulates the existence of<filename class="directory">/usr/local/games</filename> and<filename class="directory">/usr/share/games</filename>, but we don'tmuch like these for a base system. However, feel free to make your systemFHS-compliant. As to the structure of the<filename class="directory">/usr/local/share</filename> subdirectory, the FHSisn't precise, so we created here the directories that we think are needed.</para></sect2></sect1><sect1 id="ch-system-createfiles"><title>Creating essential symlinks</title><?dbhtml filename="createfiles.html" dir="chapter06"?><para>Some programs hard-wire paths to programs which don't exist yet. Inorder to satisfy these programs, we create a number of symbolic links whichwill be replaced by real files throughout the course of this chapter whenwe're installing all the software.</para><screen><userinput>ln -s /tools/bin/{bash,cat,pwd,stty} /binln -s /tools/bin/perl /usr/binln -s /tools/lib/libgcc_s.so.1 /usr/libln -s bash /bin/sh</userinput></screen></sect1><sect1 id="ch-system-pwdgroup"><title>Creating the passwd, group and log files</title><?dbhtml filename="pwdgroup.html" dir="chapter06"?><para>In order for <emphasis>root</emphasis> to be able to login and for the name "root" to be recognized, there need to be relevant entries in the <filename>/etc/passwd</filename> and <filename>/etc/group</filename> files.</para><para>Create the <filename>/etc/passwd</filename> file by running the following command:</para><screen><userinput>cat > /etc/passwd << "EOF"</userinput>root:x:0:0:root:/root:/bin/bash<userinput>EOF</userinput></screen><para>The actual password for <emphasis>root</emphasis> (the "x" here is just a placeholder) will be set later.</para><para>Create the <filename>/etc/group</filename> file by running the following command:</para><screen><userinput>cat > /etc/group << "EOF"</userinput>root:x:0:bin:x:1:sys:x:2:kmem:x:3:tty:x:4:tape:x:5:daemon:x:6:floppy:x:7:disk:x:8:lp:x:9:dialout:x:10:audio:x:11:<userinput>EOF</userinput></screen><para>The created groups aren't part of any standard -- they are some of thegroups that the <command>make_devices</command> script in the next sectionuses. The LSB (<ulink url="http://www.linuxbase.org">Linux StandardBase</ulink>) recommends only that, beside the group "root" with a GID of 0, agroup "bin" with a GID of 1 be present. All other group names and GIDs canbe chosen freely by the system administrator, since well-written packages don'tdepend on GID numbers but use the group's name.</para><para>To get rid of the "I have no name!" prompt, we will start a new shell.Since we installed a full Glibc in <xref linkend="chapter-temporary-tools"/>,and have just created the <filename>/etc/passwd</filename> and<filename>/etc/group</filename> files, user name and group name resolutionwill now work.</para><screen><userinput>exec /tools/bin/bash --login +h</userinput></screen><para>Note the use of the <emphasis>+h</emphasis> directive. This tells<command>bash</command> not to use its internal path hashing. Without thisdirective, <command>bash</command> would remember the paths to binaries ithas executed. Since we want to use our newly compiled binaries as soon asthey are installed, we turn off this function for the duration of thischapter.</para><para>The <command>login</command>, <command>agetty</command> and<command>init</command> programs (and some others) use a number of logfiles to record information such as who was logged into the system and when.These programs, however, won't write to the log files if they don't alreadyexist. Initialize the log files and give them their proper permissions:</para><screen><userinput>touch /var/run/utmp /var/log/{btmp,lastlog,wtmp}chmod 644 /var/run/utmp /var/log/{btmp,lastlog,wtmp}</userinput></screen><para>The <filename>/var/run/utmp</filename> file records the users that arecurrently logged in. The <filename>/var/log/wtmp</filename> file records alllogins and logouts. The <filename>/var/log/lastlog</filename> file records foreach user when he or she last logged in. The <filename>/var/log/btmp</filename>file records the bad login attempts.</para></sect1>&c6-makedev;&c6-kernel-headers;&c6-manpages;&c6-glibc;<sect1 id="ch-system-adjustingtoolchain"><title>Re-adjusting the toolchain</title><?dbhtml filename="adjustingtoolchain.html" dir="chapter06"?><para>Now that the new C libraries have been installed, it's time to re-adjustour toolchain. We'll adjust it so that it will link any newly compiled programagainst the new C libraries. Basically, this is the reverse of what we didin the "locking in" stage in the beginning of the previous chapter.</para><para>The first thing to do is to adjust the linker. For this we retained thesource and build directories from the second pass over Binutils. Install theadjusted linker by running the following from within the<filename class="directory">binutils-build</filename> directory:</para><screen><userinput>make -C ld INSTALL=/tools/bin/install install</userinput></screen><note><para>If you somehow missed the earlier warning to retain the Binutilssource and build directories from the second pass in<xref linkend="chapter-temporary-tools"/>, or otherwise accidentally deleted them or justdon't have access to them, don't worry, all is not lost. Just ignore the abovecommand. The result will be that the next package, Binutils, will link againstthe Glibc libraries in <filename class="directory">/tools</filename> ratherthan <filename class="directory">/usr</filename>. This is not ideal, however,our testing has shown that the resulting Binutils program binaries should beidentical.</para></note><para>From now on every compiled program will link <emphasis>only</emphasis>against the libraries in <filename>/usr/lib</filename> and<filename>/lib</filename>. The extra<emphasis>INSTALL=/tools/bin/install</emphasis> is needed because the Makefilecreated during the second pass still contains the reference to<filename>/usr/bin/install</filename>, which we obviously haven't installed yet.Some host distributions contain a <filename class="symlink">ginstall</filename>symbolic link which takes precedence in the Makefile and thus can cause aproblem here. The above command takes care of this also.</para><para>You can now remove the Binutils source and build directories.</para><para>The next thing to do is to amend our GCC specs file so that it pointsto the new dynamic linker. Just like earlier on, we use a sed to accomplishthis:</para><!-- Ampersands are needed to allow cut and paste --><screen><userinput>SPECFILE=/tools/lib/gcc-lib/*/*/specs &&sed -e 's@ /tools/lib/ld-linux.so.2@ /lib/ld-linux.so.2@g' \    $SPECFILE > newspecfile &&mv -f newspecfile $SPECFILE &&unset SPECFILE</userinput></screen><para>Again, cutting and pasting the above is recommended. And just likebefore, it is a good idea to visually inspect the specs file to verify theintended change was actually made.</para><important><para>If you are working on a platform where the name of the dynamiclinker is something other than <filename>ld-linux.so.2</filename>, you<emphasis>must</emphasis> substitute <filename>ld-linux.so.2</filename> with thename of your platform's dynamic linker in the above commands. Refer back to<xref linkend="ch-tools-toolchaintechnotes"/> if necessary.</para></important><!-- HACK - Force some whitespace to appease tidy --><literallayout></literallayout><caution><para>It is imperative at this point to stop and ensure that thebasic functions (compiling and linking) of the adjusted toolchain are workingas expected. For this we are going to perform a simple sanity check:</para><screen><userinput>echo 'main(){}' > dummy.ccc dummy.creadelf -l a.out | grep ': /lib'</userinput></screen><para>If everything is working correctly, there should be no errors, and theoutput of the last command will be:</para><blockquote><screen>[Requesting program interpreter: /lib/ld-linux.so.2]</screen></blockquote><para>(Of course allowing for platform specific differences in dynamic linkername). Note especially that <filename class="directory">/lib</filename> nowappears as the prefix of our dynamic linker. If you did not receive the outputas shown above, or received no output at all, then something is seriously wrong.You will need to investigate and retrace your steps to find out where theproblem is and correct it. There is no point in continuing until this is done.Most likely something went wrong with the specs file amendment above.</para><para>Once you are satisfied that all is well, clean up the test files:</para><screen><userinput>rm dummy.c a.out</userinput></screen></caution><!-- HACK - Force some whitespace to appease tidy --><literallayout></literallayout></sect1>&c6-binutils;&c6-gcc;&c6-coreutils;&c6-zlib;&c6-lfs-utils;&c6-findutils;&c6-gawk;&c6-ncurses;&c6-vim;&c6-m4;&c6-bison;&c6-less;&c6-groff;&c6-sed;&c6-flex;&c6-gettext;&c6-nettools;&c6-inetutils;&c6-perl;&c6-texinfo;&c6-autoconf;&c6-automake;&c6-bash;&c6-file;&c6-libtool;&c6-bzip2;&c6-diffutils;&c6-ed;&c6-kbd;&c6-e2fsprogs;&c6-grep;&c6-grub;&c6-gzip;&c6-man;&c6-make;&c6-modutils;&c6-patch;&c6-procinfo;&c6-procps;&c6-psmisc;&c6-shadow;&c6-sysklogd;&c6-sysvinit;&c6-tar;&c6-utillinux;&c6-gcc-2953;<sect1 id="ch-system-revisedchroot"><title>Revised chroot command</title><?dbhtml filename="revisedchroot.html" dir="chapter06"?><para>From now on when you exit the chroot environment and wish to re-enterit, you should run the following modified chroot command:</para><screen><userinput>chroot $LFS /usr/bin/env -i \    HOME=/root TERM=$TERM PS1='\u:\w\$ ' \    PATH=/bin:/usr/bin:/sbin:/usr/sbin \    /bin/bash --login</userinput></screen><para>The reason being there is no longer any need to use programs from the<filename class="directory">/tools</filename> directory. However, we don'twant to remove the <filename class="directory">/tools</filename> directoryjust yet. There is still some use for it towards the end of the book.</para></sect1>&c6-bootscripts;&c6-aboutdebug;</chapter>
 |