第三部 构建LFS系统
安装系统基础软件
挂载虚拟内核文件系统 ----- 指那些是由内核产生但并不存在于硬盘上(存在于内存中)的文件系统,他们被用来与内核进行通信
mkdir -v $LFS/{dev,proc,sys}
创建初始设备节点
mknod -m 600 /mnt/lfs/dev/console c 5 1
mknod -m 666 /mnt/lfs/dev/null c 1 3
挂载并填充dev目录
mount -v --bind /dev/ /mnt/lfs/dev/
挂载虚拟内核文件系统
mount -vt devpts devpts /mnt/lfs/dev/pts
mount -vt tmpfs shm /mnt/lfs/dev/shm/
mount -vt proc proc /mnt/lfs/proc/
mount -vt sysfs sysfs /mnt/lfs/sys/
进入chroot环境
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
(报错:
/bin/bash: error while loading shared libraries: libtermcap.so.2: cannot open shared object file: No such file or directory
chroot: failed to run command `bash': No such file or directory
缺少模块等等 可以使用 ldd bash 看依赖关系来解决,然后设置符号链接就可以)
会提示I have no name! 这是正常的,因为 /etc/passwd 还没有创建
创建系统目录结构
mkdir -pv /{bin,boot,etc/opt,home,lib,mnt,opt}
mkdir -pv /{media/{floppy,cdrom},sbin,srv,var}
install -dv -m 0755 /root
install -dv -m 1777 /tmp /var/tmp
mkdir �Cpv /usr/local
mkdir -pv /usr/local/{bin,include,lib,sbin,src}
mkdir -pv /usr/local/share/{doc,info,locale,man}
mkdir -v /usr/local/share/{misc,terminfo,zoneinfo}
mkdir -pv /usr/local/share/man/{1..8}
for dir in /usr /usr/local/ ;do
> ln -sv share/{man,doc,info} $dir
> done
mkdir -v /var/{lock,log,mail,run,spool}
mkdir -pv /var/{opt,cache,lib/{misc,locate},local}
case $(uname -m ) in
> x86_64) ln -sv lib /lib64 && ln -sv lib /usr/lib64;;
> esac
创建必须的文件和符号链接
ln -sv /tools/bin/{bash,cat,echo,grep,pwd,stty} /bin/
ln -sv /tools/bin/perl /usr/bin
ln -sv /tools/lib/libgcc_s.so{,.1} /usr/lib
ln -sv /tools/lib/libstdc++.so{,.6} /usr/lib/
ln -sv bash /bin/sh
touch /etc/mtab
cat > /etc/passwd <<"EOF"
> root:x:0:0:root:/root:/bin/bash
> nobody:x:99:99:unprivileged User:/dev/null:/bin/false
> 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:
> uucp:x:10:
> audio:x:11:
> video:x:12:
> utmp:x:13:
> usb:x:14:
> cdrom:x:15:
> mail:x:34:
> nogroup:x:99:
> EOF
exec /tools/bin/bash --login +h
(因为passwd 和group文件都已经创建,所以目前显示应该是root:/#)
创建一些程序的所需的日志文件,否则这些程序将无法运行:
touch /var/run/utmp /var/log/{btmp,lastlog,wtmp}
chgrp -v utmp /var/run/utmp /var/log/lastlog
chmod -v 664 /var/run/utmp /var/log/lastlog
安装linux API headers ---将内核的应用程序编程接口(API)提供给 Glibc 使用
cd sources/linux-2.6.32.8
make mrproper
make headers_check
(
报错:gcc: error trying to exec 'cc1': execvp: No such file or directory
make[1]: *** [scripts/basic/fixdep] Error 1
make: *** [scripts_basic] Error 2
解决办法:找到cc1 所在位置,并复制到对应位置,cp /mnt/lfs/tools/libexec/gcc/x86_64-unknown-linux-gnu/4.4.3/cc1 /mnt/lfs/usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.3/cc1
)
(报错:error while loading shared libraries: libmpfr.so.1: cannot open shared object file: No such file or directory 使用ldd 和find 查看所需文件放在在哪里,做符号链接或者copy都可以)
(报错:
/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.4.3/../../../../x86_64-unknown-linux-gnu/bin/as: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
解决方法与上面一样)
make INSTALL_HDR_PATH=dest headers_install
find dest/include \( -name .install -o -name ..install.cmd \) -delete
cp -rv dest/include/* /usr/include/
安装manpages ---- 包含多余1900页的帮助文档
tar -jxvf man-pages-3.23.tar.bz2
cd man-pages-3.23
make install
安装glibc ---包含了主要的 C 语言库
cd cd glibc-2.11.1
DL=$(readelf -l /bin/sh | sed -n 's@.*interpret.*/tools\(.*\)]$@\1@p')
sed -i "s|libs -o|libs -L/usr/lib -Wl,-dynamic-linker=$DL -o|" scripts/test-installation.pl
unset DL
sed -i 's|@BASH@|/bin/bash|' elf/ldd.bash.in
mkdir ../glibc-build
cd ../glibc-build
../glibc-2.11.1/configure --prefix=/usr --disable-profile --enable-add-ons --enable-kernel=2.6.18 --libexecdir=/usr/lib/glibc
cp -v ../glibc-2.11.1/iconvdata/gconv-modules iconvdata
make
make install
(报错:
找不到/etc/ld.so.conf文件,这个文件可以直接创建就可以了)
报错:
/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.4.3/../../../../x86_64-unknown-linux-gnu/bin/ld: cannot find -lcidn
Execution of gcc failed!
解决办法: 找到cidm.so相关的文件并复制到相应的位置即可)
mkdir -pv /usr/lib/locale
make localedata/install-locales
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 (配置时区)
cp -v --remove-destination /usr/share/zoneinfo/Asia/Chongqing /etc/localtime
cat > /etc/ld.so.conf << "EOF"
# Begin /etc/ld.so.conf
/usr/local/lib
/opt/lib
# End /etc/ld.so.conf
EOF
再次调整工具链
mv -v /tools/bin/{ld,ld-old}
mv -v /tools/$(gcc -dumpmachine)/bin/{ld,ld-old}
mv -v /tools/bin/{ld-new,ld}
ln -sv /tools/bin/ld /tools/$(gcc -dumpmachine)/bin/ld
gcc -dumpspecs|sed -e 's@/tools@@g'-e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' > `dirname $(gcc --print-libgcc-file-name)`/specs
安装zlib ---包含 zlib 库,很多程序中的压缩或者解压缩程序都会用到这个库
tar -jxvf zlib-1.2.3.tar.bz2
./configure --prefix=/usr --shared --libdir=/lib
make
make install
(报错:
gcc -O3 -DNO_vsnprintf -DUSE_MMAP -o example example.o -L. libz.a
/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.4.3/../../../../x86_64-unknown-linux-gnu/bin/ld: crtn.o: No such file: No such file or directory
解决方法: 找到crtn.o 并复制到相应的文件夹)
ln -sfv ../../lib/libz.so.1.2.3 /usr/lib/libz.so
make clean
./configure --prefix=/usr
make install
chmod -v 644 /usr/lib/libz.a
安装binutils ---一组开发工具,包括连接器、汇编器和其他用于目标文件和档案的工具
cd binutils-build
../sources/binutils-2.20/configure --prefix=/usr/ --enable-shared
make tooldir=/usr
make tooldir=/usr install
cp -v ../binutils-2.20/include/libiberty.h /usr/include
安装gmp ----包含数学库。库中有用于专门计算单精度数值的函数。
cd gmp-5.0.0
./configure --prefix=/usr --enable-cxx --enable-mpbsd
make
make install
安装mpfr ---包含高精度数学函数
cd mpfr-2.4.2
./configure --prefix=/usr --enable-thread-safe
make
make install
安装file --- 包含判定文件类型的工具
./configure --prefix=/usr
make
make install
安装gcc----包含 GNU 编译器,其中有 C 和 C++ 编译器
cd gcc-build
../gcc-4.4.3/configure --prefix=/usr --libexecdir=/usr/lib --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c,c++ --disable-multilib --disable-bootstrap --with-gmp=/gmp/ --with-mpfr=/mpfr/
make
make install
安装sed
./configure --prefix=/usr --bindir=/bin --htmldir=/usr/share/doc/sed-4.2.1
make
make install
安装pkg-config ---在configure 和make 中将include ,library 路径传给编译工具
tar -zxvf pkg-config-0.23.tar.gz
cd pkg-config-0.23
./configure --prefix=/usr
make
make install
安装ncurses
./configure --prefix=/usr --with-shared --without-debug --enable-widec
make
make install
安装Util-linux-ng
tar -jxvf util-linux-ng-2.17.tar.bz2
sed -e 's@etc/adjtime@var/lib/hwclock/adjtime@g' -i $(grep -rl '/etc/adjtime' .)
mkdir -pv /var/lib/hwclock
./configure --enable-arch --enable-partx --enable-write
make
make install
安装E2fsprogs --- 管理ext2、ext3,ext4文件系统
tar -zxvf e2fsprogs-1.41.10.tar.gz
mkdir -v build
cd build/
../configure --prefix=/usr --with-root-prefix="" --enable-elf-shlibs --disable-libblkid --disable-libuuid --disable-uuidd --disable-fsck
make install
make install-libs
安装Coreutils ----包括一套显示、设置基本系统属性的工具
cd coreutils-8.4
./configure --prefix=/usr --enable-no-install-program=kill,uptime
make
make install
安装Iana-Etc ---- 软件包提供了网络服务和协议的数据
tar -jxvf iana-etc-2.30.tar.bz2
cd iana-etc-2.30
make
make install
安装M4 ----包含一个宏处理器
./configure --prefix=/usr
make
make install