source: chapter06/chapter06.xml@ a195a6c

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 a195a6c was a195a6c, checked in by Greg Schafer <greg@…>, 20 years ago

Miscellaneous small fixes from the list.

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

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