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