2021-11-09bulidlfs

本文档详细介绍了如何从源代码构建Linux From Scratch(LFS)系统,版本7.7-systemd。内容包括准备构建环境,创建新的分区,安装必要的软件包和补丁,配置网络,设置静态IP,并构建Linux内核3.19。此外,还涉及GRUB的安装和配置,以及系统启动流程。
摘要由CSDN通过智能技术生成

#Revision history
#V1.1 zhaoxiaohu 2021.07.04
#V1.2 libaolin 2021.07.08

#
#       Linux From Scratch
#      Version 7.7-systemd
#
#    Created by Gerard Beekmans
# Edited by Matthew Burgess and Armin K.
#
# Implementer: Andrew Zhao
# Create Date: 4-14-2021
# Update Date: 7-01-2021
#       Locus: Hangzhou, China
#

#
# Preface
#

# VM Architecture:
# x86_64

# VM OS
# Other Linux 4.x 64-bit

# Disk sda size
# 30 GB

#
# Part I. Introduction
#

#
# Chapter 1. Introduction
#


#
# Part II. Preparing for the Build
#


# openEuler 20.09 Minimal Installation.

# Terminal
ssh root@192.168.11.130 # instead of your own IP address

yum group install -y "Development Tools"
yum install -y bc
yum install -y openssl-devel

yum install -y texinfo # for makeinfo

yum install -y vim
yum install -y nano

#
# Get "实验指导手册" and "脚本" from gitee
#

# You need to set your git account when you first run it
git config --global user.name "your-user-name"
git config --global user.email "your-email-address-on-gitee"

mkdir ~/openEuler
cd ~/openEuler
git clone https://gitee.com/openeuler-practice-courses/lfs-course
cd lfs-course/
ls
#LICENSE  README.en.md  README.md  lfs-7.7-systemd/

# Check the file cfns-4.9.2.patch
ls ~/openEuler/lfs-course/lfs-7.7-systemd/scripts/sample/patch/cfns-4.9.2.patch


cat > ./version-check.sh << "EOF"
#!/bin/bash
# Simple script to list version numbers of critical development tools
export LC_ALL=C
bash --version | head -n1 | cut -d" " -f2-4
MYSH=$(readlink -f /bin/sh)
echo "/bin/sh -> $MYSH"
echo $MYSH | grep -q bash || echo "ERROR: /bin/sh does not point to bash"
unset MYSH
echo -n "Binutils: "; ld --version | head -n1 | cut -d" " -f3-
bison --version | head -n1
if [ -h /usr/bin/yacc ]; then
echo "/usr/bin/yacc -> `readlink -f /usr/bin/yacc`";
elif [ -x /usr/bin/yacc ]; then
echo yacc is `/usr/bin/yacc -V | head -n1`
else
echo "yacc not found"
fi
bzip2 --version 2>&1 < /dev/null | head -n1 | cut -d" " -f1,6-
echo -n "Coreutils: "; chown --version | head -n1 | cut -d")" -f2
diff --version | head -n1
find --version | head -n1
gawk --version | head -n1
if [ -h /usr/bin/awk ]; then
echo "/usr/bin/awk -> `readlink -f /usr/bin/awk`";
elif [ -x /usr/bin/awk ]; then
echo awk is `/usr/bin/awk --version | head -n1`
else
echo "awk not found"
fi
gcc --version | head -n1
g++ --version | head -n1
ldd --version | head -n1 | cut -d" " -f2- # glibc version
grep --version | head -n1
gzip --version | head -n1
cat /proc/version
m4 --version | head -n1
make --version | head -n1
patch --version | head -n1
echo Perl `perl -V:version`
sed --version | head -n1
tar --version | head -n1
makeinfo --version | head -n1 # texinfo version
xz --version | head -n1
echo 'int main(){}' > dummy.c && g++ -o dummy dummy.c
if [ -x dummy ]
then echo "g++ compilation OK";
else echo "g++ compilation failed"; fi
rm -f dummy.c dummy
EOF

bash version-check.sh

# More determination
cat > ./library-check.sh << "EOF"
#!/bin/bash
for lib in lib{gmp,mpfr,mpc}.la; do
echo $lib: $(if find /usr/lib* -name $lib|
grep -q $lib;then :;else echo not;fi) found
done
unset lib
EOF

bash library-check.sh
#如果出现以下保存则忽略
#libgmp.la: not found
#libmpfr.la: not found
#libmpc.la: not found

#
# Chapter 2. Preparing a New Partition
#

# 2.1. Introduction

# 2.2. Host System Requirements

# 2.3. Building LFS in Stages

# 2.3.1. Chapters 1–4
# These chapters are accomplished on the host system.

# 2.3.2. Chapter 5–6
# The /mnt/lfs partition must be mounted.
# These two chapters must be done as user lfs.

# 2.3.3. Chapter 7–10
# The /mnt/lfs partition must be mounted.
# A few operations, from “Changing Ownership” to “Entering the Chroot Environment” must be done.
# The virtual file systems must be mounted.

# 2.4. Creating a New Partition

# Adding a Disk for LFS in VM
# Size: 30GB


#关机,参考实验手册添加另外一块硬盘
shutdown -h now

# Start the machine
# Terminal
ssh root@192.168.11.130 # instead of your own IP address

# Start a disk partitioning program such as fdisk to create a Linux native partition.

# Check
lsblk
fdisk -l /dev/sdb

#开始给第二块磁盘分配
# Just create a primary partition: sdb1
fdisk /dev/sdb

#依次输入 n  p 回车 回车 回车 w
# Welcome to fdisk (util-linux 2.35.2).
# Changes will remain in memory only, until you decide to write them.
# Be careful before using the write command.

# Device does not contain a recognized partition table.
# Created a new DOS disklabel with disk identifier 0x9d1c2177.

# Command (m for help): n
# Partition type
#    p   primary (0 primary, 0 extended, 4 free)
#    e   extended (container for logical partitions)
# Select (default p):

# Using default response p.
# Partition number (1-4, default 1):
# First sector (2048-62914559, default 2048):
# Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62914559, default 62914559):

# Created a new partition 1 of type 'Linux' and of size 30 GiB.

# Command (m for help): w
# The partition table has been altered.
# Calling ioctl() to re-read partition table.
# Syncing disks.

# Determine
lsblk
blkid

# 2.5. Creating a File System on the Partition

# LFS assumes that the root file system (/) is of type ext4.
# mkfs -v -t ext4 /dev/<xxx>
# Replace <xxx> with the name of the LFS partition.
#格式化刚刚分区的磁盘
mkfs -v -t ext4 /dev/sdb1

# Determine
lsblk
blkid

# 2.6. Setting The $LFS Variable

# export LFS=/mnt/lfs
cp /root/.bash_profile{,.origin}
echo "export LFS=/mnt/lfs" >> /root/.bash_profile
source /root/.bash_profile

# Determine
echo $LFS # /mnt/lfs


# 2.7. Mounting the New Partition

mkdir -pv $LFS

# Determine
ls $LFS
#  


# Do "mount -v -t ext4 /dev/sdb1 $LFS" in fstab
cp /etc/fstab{,.origin}
echo "/dev/sdb1 /mnt/lfs ext4 defaults 1 1" >> /etc/fstab

# mount -a
# or reboot to determine
reboot

# Terminal
ssh root@192.168.11.130 # instead of your own IP address

# Determine
ls $LFS
#  lost+found


#
# Chapter 3. Packages and Patches
#

# 3.1. Introduction

mkdir -v $LFS/sources

# Make this directory writable and sticky
chmod -v a+wt $LFS/sources
#  mode of '/mnt/lfs/sources' changed from 0755 (rwxr-xr-x) to 1777 (rwxrwxrwt)

# 3.2. All Packages
# 3.3. Needed Patches

# Get the packages for LFS
# http://www.linuxfromscratch.org/lfs/packages.html#packages
# http://ftp.osuosl.org/pub/lfs/lfs-packages/
# Execuate in terminal, and replace the IP address of yours
#scp lfs-packages-7.7-systemd.tar root@192.168.11.130:/mnt/lfs/
#下载lfs-packages-7.7-systemd.tar
cd $LFS
wget https://zhuanyejianshe.obs.cn-north-4.myhuaweicloud.com/chuangxinshijianke/lfs-packages-7.7-systemd.tar

#或者用下面链接,速度可能会慢些
#wget http://ftp.osuosl.org/pub/lfs/lfs-packages/lfs-packages-7.7-systemd.tar


#
# The bug is on the GCC 4.9 side, so either you need to patch it,
# or build with -std=gnu++98 - then __GNUC_STDC_INLINE__
# will not be defined and it ought to compile fine.
#
# Now I modified this file in my own way. And maybe you'd better  
# fix it by a 'political correctness' way.
#
# ps.: set a check-point

scp ~/openEuler/lfs-course/lfs-7.7-systemd/scripts/sample/patch/cfns-4.9.2.patch $LFS/


#
# Chapter 4. Final Preparations
#

# 4.1. Introduction

# 4.2. Creating a limited directory layout in LFS filesystem

# Creating the $LFS/tools Directory to separate the cross-compiler in
# the chapter 6 from the other programs
mkdir -pv $LFS/tools

ln -sv $LFS/tools /

# 4.3. Adding the LFS User

groupadd lfs
useradd -s /bin/bash -g lfs -m -k /dev/null lfs

passwd lfs # Input password by handwork
#  lfs@123

# Grant lfs full access to all directories under $LFS
# by making lfs the directory owner

chown -v lfs $LFS/tools
#  changed ownership of '/mnt/lfs/tools' from root to lfs
chown -v lfs $LFS/sources
#  changed ownership of '/mnt/lfs/sources' from root to lfs

# Check
whoami # root

# Adding sudo for lfs, by Andrew

cp /etc/sudoers{,.origin}
nano +100 /etc/sudoers

#复制root这一行,将root替换为lfs,替换后结果如下:
#  root    ALL=(ALL)       ALL
#  lfs     ALL=(ALL)       ALL
#按Ctrl+o回车保存,Ctrl+x退出

#
# Login as user lfs!!!
#
su - lfs

# To test for getting password
sudo ls

# [lfs@localhost ~]$
# Determine
whoami # lfs

# 4.4. Setting Up the Environment

# While logged in as user lfs, issue the following command
# to create a new .bash_profile
cat > ~/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF

# The new instance of the shell reads, and executes, the .bashrc file instead
cat > ~/.bashrc << "EOF"
set +h
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH
EOF

# To have the environment fully prepared for building the temporary tools,
# source the just-created user profile.
source ~/.bash_profile

# lfs:~$
whoami # lfs

# To make sure that ...
# LFS=/mnt/lfs
# bash is the shell in use.
# sh is a symbolic link to bash.
# /usr/bin/awk is a symbolic link to gawk.
# /usr/bin/yacc is a symbolic link to bison or a small script that executes bison.

echo $LFS

# More determination

cat /etc/shells
#  /bin/sh
#  /bin/bash
#  /usr/bin/sh
#  /usr/bin/bash

ls -l /bin/sh
#  lrwxrwxrwx. 1 root root 4 Sep 27  2020 /bin/sh -> bash

echo $SHELL
#  /bin/bash

ls -l /usr/bin/awk
#  lrwxrwxrwx. 1 root root 4 Sep 27  2020 /usr/bin/awk -> gawk

ls -l /usr/bin/yacc
# -rwxr-xr-x. 1 root root 109128 Sep 27  2020 /usr/bin/yacc

whereis bison
#  bison: /usr/bin/bison /usr/share/bison

exit

reboot


# Chapter 5. Constructing a Temporary System

# 5.1. Introduction

# 5.2. Toolchain Technical Notes

# 5.3. General Compilation Instructions

# For each package:
# a. Using the tar program, extract the package to be built. In Chapter 5 and Chapter 6, ensure you are
#    the lfs user when extracting the package.
# b. Change to the directory created when the package was extracted.
# c. Follow the book's instructions for building the package.
# d. Change back to the sources directory.
# e. Delete the extracted source directory unless instructed otherwise.


# 此处开始第一遍编译,以lfs用户登录
ssh lfs@192.168.11.130 # instead of your own IP address

cd $LFS
tar xf ./lfs-packages-7.7-systemd.tar -C ./sources

#
# 5.4. Binutils-2.25 - Pass 1
#

cd $LFS/sources/
tar xjf binutils-2.25.tar.bz2
cd binutils-2.25
mkdir build && cd build

../configure --prefix=/tools --with-sysroot=$LFS --with-lib-path=/tools/lib --target=$LFS_TGT --disable-nls --disable-werror
make

# Architecture determination
uname -m # x86_64

# For x86_64 platform
mkdir -v /tools/lib && ln -sv lib /tools/lib64

make install

# Clean
cd $LFS/sources/
rm -rf binutils-2.25


#
# 5.5. GCC-4.9.2 - Pass 1
#

cd $LFS/sources/
tar xjf gcc-4.9.2.tar.bz2
cd gcc-4.9.2

# GCC now requires the GMP, MPFR and MPC packages.
tar -xf ../mpfr-3.1.2.tar.xz
mv -v mpfr-3.1.2 mpfr
tar -xf ../gmp-6.0.0a.tar.xz
mv -v gmp-6.0.0 gmp
tar -xf ../mpc-1.0.2.tar.gz
mv -v mpc-1.0.2 mpc

# The following command will change the location of GCC's default dynamic linker to use the one installed in /tools.
# It also removes /usr/include from GCC's include search path. Issue:
cat > ./gcc-lambda.sh << "EOF"
#!/bin/sh

for file in \
    $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
do
    cp -uv $file{,.orig}
    sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
        -e 's@/usr@/tools@g' $file.orig > $file
    echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
    touch $file.orig
done
EOF

# Just for character set issue if any.
##vim lambda.sh # :set ff=unix
sh gcc-lambda.sh

sed -i '/k prot/agcc_cv_libc_provides_ssp=yes' gcc/configure

cd $LFS/sources/gcc-4.9.2

cp $LFS/cfns-4.9.2.patch ./
patch -p1 < cfns-4.9.2.patch

mkdir -v build
cd build/
../configure --target=$LFS_TGT --prefix=/tools --with-sysroot=$LFS --with-newlib --without-headers --with-local-prefix=/tools --with-native-system-header-dir=/tools/include --disable-nls --disable-shared --disable-multilib --disable-decimal-float --disable-threads --disable-libatomic --disable-libgomp --disable-libitm --disable-libquadmath --disable-libsanitizer --disable-libssp --disable-libvtv --disable-libcilkrts --disable-libstdc++-v3 --enable-languages=c,c++

make -j 4
make install

# Clean
cd $LFS/sources/
rm -rf gcc-4.9.2


#
# 5.6. Linux-3.19 API Headers
#

cd $LFS/sources/
tar xJf linux-3.19.tar.xz
cd linux-3.19

make mrproper
make INSTALL_HDR_PATH=dest headers_install
cp -rv dest/include/* /tools/include

# Clean
cd $LFS/sources/
rm -rf linux-3.19


#
# 5.7. Glibc-2.21
#

cd $LFS/sources/
tar xJf glibc-2.21.tar.xz
cd glibc-2.21

##patch -p1 < ../glibc-2.21-fhs-1.patch

cat > ./glibc-lambda.sh << "EOF"
#!/bin/sh

if [ ! -r /usr/include/rpc/types.h ]; then
echo "ture"
fi
EOF
sh glibc-lambda.sh

# sudo: need lfs password
sudo ls /usr/include/rpc/ # To input the lfs password
sudo mv /usr/include/rpc/netdb.h{,.origin}
sudo cp -v $LFS/sources/glibc-2.21/sunrpc/rpc/*.h /usr/include/rpc/

cp sysdeps/i386/i686/multiarch/mempcpy_chk.S{,.origin}
sed -e '/ia32/s/^/1:/' -e '/SSE2/s/^1://' -i sysdeps/i386/i686/multiarch/mempcpy_chk.S

# Do these by lfs user
mkdir build && cd build

../configure --prefix=/tools --host=$LFS_TGT --build

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值