source: chapter06/chapter06.xml@ a0b689e

10.0 10.0-rc1 10.1 10.1-rc1 11.0 11.0-rc1 11.0-rc2 11.0-rc3 11.1 11.1-rc1 11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 6.0 6.1 6.1.1 6.3 6.4 6.5 6.6 6.7 6.8 7.0 7.1 7.2 7.3 7.4 7.5 7.5-systemd 7.6 7.6-systemd 7.7 7.7-systemd 7.8 7.8-systemd 7.9 7.9-systemd 8.0 8.1 8.2 8.3 8.4 9.0 9.1 arm bdubbs/gcc13 ml-11.0 multilib renodr/libudev-from-systemd s6-init trunk v5_1 v5_1_1 xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/lfs-next xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since a0b689e was a0b689e, checked in by Alex Gronenwoud <alex@…>, 20 years ago

Adapting the title of the 'Locking in' section.

git-svn-id: http://svn.linuxfromscratch.org/LFS/trunk/BOOK@3249 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

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