含有章节索引的中文 文章模板

::-- ehu4ever [2005-10-05 07:31:37]

1. 准备工作

1.1. 分区

本文使用的lfs版本是:lfslivecd-x86-6.1-2.iso。 事先建立好分区,用ext2,swap。 建立分区之后,要记下各个分区的名字。 lfs安装在hda1,交换分区是hda2。 光盘是hdc。

1.2. 加载分区

这个步骤在每次机器重启之后都要重做。

export LFS=/mnt/lfs
mkdir -p $LFS
mount /dev/[xxx] $LFS

1.3. 准备源代码

把源代码和补丁拷贝到$LFS/sources

mkdir $LFS/sources
chmod a+wt $LFS/sources
# cp -R /sources $LFS/sources

每次改变环境之后,检查一下

echo $LFS

1.4. 准备第一个工具链的目录

第一个工具链装在$LFS/tools(别忘了先建立这个目录)。 这个link应该放在启动脚本里,重启之后要重新设置。 这个link是为了第一次次编译与第二次编译的目录上的兼容性。 注意:这一步在下面的setenv.sh脚本中完成,这里就跳过了。

1.5. 添加一个专用的用户lfs

由于使用的是livecd,所以没有这个必要。

1.6. 设置编译第一个工具链的环境

在livdcd的环境中,写一个初始化脚本,这个脚本放在$LFS:

#################################################################
# Begin setenv.sh
# Sets up clean environment for 6.1 Chapter 5
#
# Before this script is run, you must have executed the following
# after booting the live CD:
#
## export LFS=/mnt/lfs
## mkdir $LFS
## mount /dev/xxx $LFS  (where xxx is your lfs partition)

# Create the link to tools as per Ch 4.2
ln -s $LFS/tools /

# Set up the environment as per chapter 4.4
# This sets up a special environment for root based
# on the procedure for the user "lfs" which isn't
# necessary when running from the live CD.

echo "set +h" > ~/.bashrc
echo "LFS=/mnt/lfs" >> ~/.bashrc
echo "LC_ALL=POSIX" >> ~/.bashrc
echo "PATH=/tools/bin:/bin:/usr/bin" >> ~/.bashrc
echo "export LC_ALL PATH" >> ~/.bashrc

# This sets up key mapping so the delete key works:
cp /etc/inputrc ~/.inputrc

# Enter the special build environment
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash

# End setenv.sh
###################################################################

运行这个脚本:

# chmod 700 setenv.sh
# ./setenv.sh

1.7. 一些要注意的问题

编译过程中的错误: http://www.linuxfromscratch.org/lfs/build-logs/6.1/ (其中也包括测试过程中的错误)

在这个系统完成之后,应该用ghost作一个备份。

保证echo $LFS的输出值的正确。

2. 开始第一个工具链

2.1. 5.3. Binutils-2.15.94.0.2.2 - Pass 1

在$LFS/source目录下,对源代码包bz2进行解压:

tar -xjvf *****.tar.bz2

另外,在$LFS/source下建立一个单独的编译目录binutils-build:

mkdir binutils-build
cd binutils-build

../binutils-2.15.94.0.2.2/configure --prefix=/tools --disable-nls(不能用/mnt/lfs/tools)
make
make install
make -C ld clean
make -C ld LIB_PATH=/tools/lib

2.2. 5.4. GCC-3.4.3 - Pass 1

mkdir ../gcc-build
cd ../gcc-build
../gcc-3.4.3/configure --prefix=/tools \
    --libexecdir=/tools/lib --with-local-prefix=/tools \
    --disable-nls --enable-shared --enable-languages=c
make bootstrap
make install
ln -s gcc /tools/bin/cc

2.3. 5.5. Linux-Libc-Headers-2.6.11.2

tar -xvjf linux-libc-heads-****.tar.bz2
cp -R include/asm-i386 /tools/include/asm
cp -R include/linux /tools/include

2.4. 5.6. Glibc-2.3.4

patch -Np1 -i ../glibc-2.3.4-fix_test-1.patch
mkdir ../glibc-build
cd ../glibc-build
../glibc-2.3.4/configure --prefix=/tools \
    --disable-profile --enable-add-ons \
    --enable-kernel=2.6.0 --with-binutils=/tools/bin \
    --without-gd --with-headers=/tools/include \
    --without-selinux
make
make check
mkdir /tools/etc
touch /tools/etc/ld.so.conf
make install
make localedata/install-locales

以下这些locale要安装,命令是这样的:localedef -i zh_CN -f GB18030 zh_CN (GB18030安装的时间要长一些,我还以为是死机呢:) (其实只装上en_US就得了,其它的可以以后再说)

en_US.UTF-8
en_US.ISO-8859-1
zh_CN.GB18030
zh_CN.GBK
zh_CN.UTF-8
zh_CN.GB2312
zh_HK.UTF-8
zh_HK.BIG5-JKSCS
zh_TW.UTF-8
zh_TW.EUC-TW UTF-8 
zh_TW.BIG5
zh_SG.FB2312
zh_SG.GBK 

2.5. 5.7. Adjusting the Toolchain

这之后要编译的包就以之前编译的几个包为基础!

2.5.1. the linker and the compiler's specs file need to be adjusted.

make -C ld install

安装linker,在binutils-build目录中执行上面这条命令。 现在binutils的两个包可以删除了(bz2留下)。 修改GCC specs file,这样在link的时候就使用我们新装的gcc和glibc:

如果是在X下做的就用复制粘贴拉。 自己打的话 注意

SPECFILE=`gcc --print-file specs` &&

这个` 是TAB键上面那个。

sed 's@ /lib/ld-linux.so.2@ /tools/lib/ld-linux.so.2@g'

这个'是单引号了。

SPECFILE=`gcc --print-file specs` &&
sed 's@ /lib/ld-linux.so.2@ /tools/lib/ld-linux.so.2@g' \
    $SPECFILE > tempspecfile &&
mv -f tempspecfile $SPECFILE &&
unset SPECFILE

rm -f /tools/lib/gcc/*/*/include/{pthread.h,bits/sigthread.h}

2.5.2. 进行一些必要的测试

echo 'main(){}' > dummy.c
cc dummy.c
readelf -l a.out | grep ': /tools'

最后的输出应该是: [Requesting program interpreter:

  • /tools/lib/ld-linux.so.2]

意思是说之后编译的模块用的是/tools/lib目录下的库。 注意,如果是/mnt/lfs/tools/lib/ld-linux.so.2就错了!! 测试之后要清空测试文件:

rm dummy.c a.out

请注意一下,你的gcc是哪个gcc? 用which gcc测试一下。 当lfs进行到这一步,你的cc和gcc都应该是/tools/bin里的!!!! 另外,ldd gcc的结果的目录中也应该是/tools/lib开头的。

2.6. 5.8. Tcl-8.4.9

cd unix
./configure --prefix=/tools
make

TZ=UTC make test    //测试一下

make install
cd ..
export TCLPATH=`pwd`(注意不是单引号,这是把TCLPATH设成当前目录)
ln -s tclsh8.4 /tools/bin/tclsh

2.7. 5.9. Expect-5.43.0

patch -Np1 -i ../expect-5.43.0-spawn-1.patch
./configure --prefix=/tools --with-tcl=/tools/lib \
   --with-tclinclude=$TCLPATH --with-x=no
make
make SCRIPTS="" install
unset TCLPATH

2.8. 5.10. DejaGNU-1.4.4

./configure --prefix=/tools
make install

2.9. 5.11. GCC-3.4.3 - Pass 2

expect -c "spawn ls" //(the response is "spawn ls")
patch -Np1 -i ../gcc-3.4.3-no_fixincludes-1.patch
patch -Np1 -i ../gcc-3.4.3-specs-2.patch
mkdir ../gcc-build
cd ../gcc-build
/* Before starting to build GCC, remember to unset any environment 
 * variables that override the default optimization flags.
 */
../gcc-3.4.3/configure --prefix=/tools \
    --libexecdir=/tools/lib --with-local-prefix=/tools \
    --enable-clocale=gnu --enable-shared \
    --enable-threads=posix --enable-__cxa_atexit \
    --enable-languages=c,c++ --disable-libstdcxx-pch
make
make -k check
../gcc-3.4.3/contrib/test_summary
make install

echo $LFS
echo 'main(){}' > dummy.c
cc dummy.c
readelf -l a.out | grep ': /tools'
最后的输出应该是:
[Requesting program interpreter: 
    /tools/lib/ld-linux.so.2]
测试之后要清空测试文件:
rm dummy.c a.out

2.10. 5.12. Binutils-2.15.94.0.2.2 - Pass 2

mkdir ../binutils-build
cd ../binutils-build
../binutils-2.15.94.0.2.2/configure --prefix=/tools \
    --disable-nls --enable-shared --with-lib-path=/tools/lib
make
make check
make install
make -C ld clean
make -C ld LIB_PATH=/usr/lib:/lib

3. Chapter 6 Installing Basic System Software

For the procedures in chapter 6 we will no longer be using the setenv.sh script to set up the build environment. Instead we "chroot" into the $LFS directory. This will require running a script to enter the chroot environment, and then another script to configure the temporaty filesystems and devices within that environment. If you are still in the environment set up by setenv.sh, type exit to return to the live CD root prompt. Or to be safe, reboot the live CD. 主系统建立的过程中,会在/bin之类的目录下产生相应的可执行文件。这一点要注意一下。

3.1. 6.2.Mounting Virtual Kernel File Systems

mkdir -p $LFS/{proc,sys}

Create the $LFS/proc and $LFS/sys directories, but do not mount the file systems. We will mount the filesystems in the lfschroot.sh script below.

3.2. Entering the Chroot Environment

################################################################
# Begin lfschroot.sh
# Changes root directory for use in LFS 6.1 Chapter 6

# Mount required filesystems
mount -t proc proc $LFS/proc
mount -t sysfs sysfs $LFS/sys

# These "fake mounts" are also needed now
mount -f -t ramfs ramfs $LFS/dev
mount -f -t tmpfs tmpfs $LFS/dev/shm
mount -f -t devpts -o gid=4,mode=620 devpts $LFS/dev/pts

echo ""
echo "Do not forget to populate /dev !"
echo ""

# Chroot into the LFS system with a reduced environment
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

# End lfschroot.sh
###################################################################

Make the script executable with this command.

# chmod 700 lfschroot.sh

Enter the lfs chroot environment:

# ./lfschroot.sh

如果是env的出错,检查一下ldd /tools/bin/env的结果有没有错,它的结果不应该有/mnt/lfs开头的的目录。 另外,还有必要核查一下/tools/bin目录下其它可执行文件的ldd结果。 Ignore the reminder about populating /dev. I will cover that in chapter 6.8

Once in the chroot environment a lot of things will be "broken". You no longer have access to the text editors or many of the other tools from the live CD. In fact, all you have to work with is what you have installed in chapter 5.

3.3. 6.4. Changing Ownership

#chown -R 0:0 /tools

Since we skipped creating the lfs user, you can skip this too. Note that when in the chroot environment the files are allready owned by 0:0, whereas outside the in the live CD environment that are owned by root:root. This is because the files are never really owned by "root", they are owned by user 0 group 0. A "normal" OS translates the user and group numbers to the user and group names specified in /etc/passwd and /etc/group. Since we have no passwd or group files, "I have no name!"

3.4. 6.5.Creating Directories

install -d /{bin,boot,dev,etc/opt,home,lib,mnt}
install -d /{sbin,srv,usr/local,var,opt}
install -d /root -m 0750
install -d /tmp /var/tmp -m 1777
install -d /media/{floppy,cdrom}
install -d /usr/{bin,include,lib,sbin,share,src}
ln -s share/{man,doc,info} /usr
install -d /usr/share/{doc,info,locale,man}
install -d /usr/share/{misc,terminfo,zoneinfo}
install -d /usr/share/man/man{1,2,3,4,5,6,7,8}
install -d /usr/local/{bin,etc,include,lib,sbin,share,src}
ln -s share/{man,doc,info} /usr/local
install -d /usr/local/share/{doc,info,locale,man}
install -d /usr/local/share/{misc,terminfo,zoneinfo}
install -d /usr/local/share/man/man{1,2,3,4,5,6,7,8}
install -d /var/{lock,log,mail,run,spool}
install -d /var/{opt,cache,lib/{misc,locate},local}
install -d /opt/{bin,doc,include,info}
install -d /opt/{lib,man/man{1,2,3,4,5,6,7,8}}

3.5. 6.6.Creating Essential Symlinks

ln -s /tools/bin/{bash,cat,pwd,stty} /bin
ln -s /tools/bin/perl /usr/bin
ln -s /tools/lib/libgcc_s.so{,.1} /usr/lib
ln -s bash /bin/sh

3.6. 6.7. Creating the passwd, group, and log Files

cat > /etc/passwd << "EOF"
root:x:0:0:root:/root:/bin/bash
EOF
cat > /etc/group << "EOF"
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:
video:x:12:
utmp:x:13:
usb:x:14:
EOF

exec /tools/bin/bash --login +h

touch /var/run/utmp /var/log/{btmp,lastlog,wtmp}
chgrp utmp /var/run/utmp /var/log/lastlog
chmod 664 /var/run/utmp /var/log/lastlog

3.7. 6.8. Populating /dev

Make the two nodes as decribed in 6.8.1

mknod -m 600 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3

We are going to make a script for this, so that we can populate /dev at will while building the rest of chapter 6. Exit the chroot environment and create the following script in your $LFS directory to load the device nodes.

#########################################################
# Begin devpop.sh
# Based on LFS 6.1 Chapter 6.8.2
# This mounts a tempfs to /dev and populates
# the /dev directories with a minimal set of device nodes.
# ONLY RUN AFTER YOU HAVE CHROOTed INTO LFS DIRECTORY!!!

mount -n -t tmpfs none /dev

mknod -m 622 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3
mknod -m 666 /dev/zero c 1 5
mknod -m 666 /dev/ptmx c 5 2
mknod -m 666 /dev/tty c 5 0
mknod -m 444 /dev/random c 1 8
mknod -m 444 /dev/urandom c 1 9
chown root:tty /dev/{console,ptmx,tty}

# These symbolic links are required by LFS
ln -s /proc/self/fd /dev/fd
ln -s /proc/self/fd/0 /dev/stdin
ln -s /proc/self/fd/1 /dev/stdout
ln -s /proc/self/fd/2 /dev/stderr
ln -s /proc/kcore /dev/core

# Mount the kernel filesystems within /dev
mkdir /dev/pts
mkdir /dev/shm
mount -t devpts -o gid=4,mode=620 none /dev/pts
mount -t tmpfs none /dev/shm

# Test to see if udev is built yet.  If so, run it.
if [ -f /sbin/usevstart ]; then
        /sbin/udevstart
fi

# End devpop.sh
#################################################################

So, for the rest of chapter 6, anytime you reboot, you must run the following commands:

# export LFS=/mnt/lfs
# mkdir $LFS
# mount /dev/[xxx] $LFS
# $LFS/lfschroot.sh
# ./devpop.sh

(Replace [xxx] with the partition that you have created for your LFS installation.)

If you don't reboot, but only leave the build environment (with the exit command), you may re-enter the chroot environment by running the lfschroot and devpop scripts. Note that you will receive some warnings, since the scripts try to mount filesystems that are allready mounted. These are safe to ignore.

So, you can now continue by installing the packages in chapter 6...

3.8. 6.11. Glibc-2.3.4

tar -xjvf /sources/glibc-linuxthreads-2.3.4.tar.bz2
patch -Np1 -i ../glibc-2.3.4-fix_test-1.patch
mkdir ../glibc-build
cd ../glibc-build
../glibc-2.3.4/configure --prefix=/usr \
    --disable-profile --enable-add-ons \
    --enable-kernel=2.6.0 --libexecdir=/usr/lib/glibc

make
make check
touch /etc/ld.so.conf
make install
make localedata/install-locales
###################################################################
以下这些locale要安装,命令是这样的:localedef -i zh_CN -f GB18030 zh_CN
当然,别忘了先建一个目录!
GB18030的耗时会长
en_US.UTF-8
en_US.ISO-8859-1
zh_CN.GB18030
zh_CN.GBK
zh_CN.UTF-8
zh_CN.GB2312
zh_HK.UTF-8
zh_HK.BIG5-JKSCS
zh_TW.UTF-8
zh_TW.EUC-TW UTF-8 
zh_TW.BIG5
zh_SG.FB2312
zh_SG.GBK 
###################################################################
make -C ../glibc-2.3.4/linuxthreads/man
make -C ../glibc-2.3.4/linuxthreads/man install
cat > /etc/nsswitch.conf << "EOF"
# Begin /etc/nsswitch.conf

passwd: files
group: files
shadow: files

hosts: files dns
networks: files

protocols: files
services: files
ethers: files
rpc: files

# End /etc/nsswitch.conf
EOF

tzselect

我用的是Asia/Shanghai

cp --remove-destination /usr/share/zoneinfo/[xxx] \

  • /etc/localtime

Replace [xxx] with the name of the time zone that tzselect provided (e.g., Canada/Eastern).
cat > /etc/ld.so.conf << "EOF"
# Begin /etc/ld.so.conf

/usr/local/lib
/opt/lib

# End /etc/ld.so.conf
EOF

3.9. 6.12. Re-adjusting the Toolchain

make -C ld INSTALL=/tools/bin/install install
// Install the adjusted linker by running the following 
// command from within the binutils-build directory:
perl -pi -e 's@ /tools/lib/ld-linux.so.2@ /lib/ld-linux.so.2@g;' \
    -e 's@\*startfile_prefix_spec:\n@$_/usr/lib/ @g;' \
        `gcc --print-file specs`


echo 'main(){}' > dummy.c
cc dummy.c
readelf -l a.out | grep ': /lib'
rm dummy.c a.out

Remove the Binutils source and build directories now.

3.10. 6.22. Readline-5.0

Now that readline is installed, you can make the delete key work if you want. Just create the files specified in chapters 7.8 & 7.9. (You will have to exit and chroot again to have the changes take effect.)

cat > /etc/inputrc << "EOF"
# Begin /etc/inputrc
# Modified by Chris Lynn <[email protected]>

# Allow the command prompt to wrap to the next line
set horizontal-scroll-mode Off

# Enable 8bit input
set meta-flag On
set input-meta On

# Turns off 8th bit stripping
set convert-meta Off

# Keep the 8th bit for display
set output-meta On

# none, visible or audible
set bell-style none

# All of the following map the escape sequence of the
# value contained inside the 1st argument to the
# readline specific functions

"\eOd": backward-word
"\eOc": forward-word

# for linux console
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert

# for xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# for Konsole
"\e[H": beginning-of-line
"\e[F": end-of-line

# End /etc/inputrc
EOF

locale -a
LC_ALL=[locale name] locale charmap

cat > /etc/profile << "EOF"
# Begin /etc/profile

export LANG=zh_CN.GB18030 //字符界面下不能显示中文,所以还是用en_US.ISO-8859-1好了。
export INPUTRC=/etc/inputrc

# End /etc/profile
EOF

3.11. 6.61. Stripping Again

If you followed the instructions, and you have not exited the build enviromnent since Chapter 6.37 (Bash), you will be running the newly installed bash executable. If so, exit the build environment and reenter it as usual: # exit # $LFS/lfschroot.sh

Now you will be running the version of bash from the live CD. Procede with stripping if desired.

logout

chroot $LFS /tools/bin/env -i \
    HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
    PATH=/bin:/usr/bin:/sbin:/usr/sbin \
    /tools/bin/bash --login

/tools/bin/find /{,usr/}{bin,lib,sbin} -type f \
   -exec /tools/bin/strip --strip-debug '{}' ';'

3.12. 6.62. Cleaning Up

If desired, remove the /tools directory. If you do this, you must edit lfschroot.sh to remove the references to /tools from the chroot command. And you can remove the +h bash option. Edit the chroot command to look like this:

# Chroot into the LFS system with a reduced environment for Chapter 7
chroot "$LFS" /usr/bin/env -i \
        HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
        PATH=/bin:/usr/bin:/sbin:/usr/sbin \
        /bin/bash --login

3.13. 7.5. Configuring the setclock Script

cat > /etc/sysconfig/clock << "EOF"
# Begin /etc/sysconfig/clock

UTC=0

# End /etc/sysconfig/clock
EOF

3.14. 7.6. Configuring the Linux Console

什么也不做

3.15. 7.7. Configuring the sysklogd script

跳过

3.16. 7.10. Configuring the localnet Script

echo "HOSTNAME=ehux" > /etc/sysconfig/network

3.17. 7.11. Creating the /etc/hosts File

cat > /etc/hosts << "EOF"
# Begin /etc/hosts (no network card version)

127.0.0.1 ehux.ehuhome.org ehux localhost

# End /etc/hosts (no network card version)
EOF

3.18. 7.12. Configuring the network Script

cd /etc/sysconfig/network-devices &&
mkdir ifconfig.eth0 &&
cat > ifconfig.eth0/ipv4 << "EOF"
ONBOOT=yes
SERVICE=ipv4-static
IP=192.168.1.5
GATEWAY=192.168.1.1
PREFIX=24
BROADCAST=192.168.1.255
EOF

cat > /etc/resolv.conf << "EOF"
# Begin /etc/resolv.conf

domain {ehuhome.org}
nameserver 221.12.65.228
nameserver 221.12.33.228

# End /etc/resolv.conf
EOF

3.19. 8.3. Linux-2.6.11.12

/usr/share/kbd/keymaps/i386/qwerty/us.map.gz

you can search the us.map.gz for an US keyboard(normal case for Chinese). search using:

[root@/usr/share/kbd/keymaps#] find |grep us

Notice the PATH! you will see the result.