【LFS】个性化Linux

教程和我的环境

bilibili:一位UP的没有讲解的参考视频

LFS:LFS的官方中文文档

环境

1
2
宿主系统:CentOS-7-x86_64-DVD-2009.iso
两个硬盘:一个硬盘sda 20GB,一个硬盘sdb 100GB

初始化环境

终端运行下面代码,生成环境检查文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 --version | head -n1`
else
echo "yacc not found"
fi

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
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`
python3 --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进行环境检查,发现有几个文件没有安装

接下来安装这些文件

1
2
3
yum -y install bison
yum -y install python3
yum -y install gcc-c++.x86_64

在运行bash version-check.sh就会发现OK了

硬盘分配

我单独分了一块硬盘出来LFS,所以要初始化一下硬盘

1
2
3
4
mkfs -v -t ext4 /dev/sdb
mkdit /mnt/lfs
export LFS=/mnt/lfs
mount /dev/sdb $LFS

安装软件包和补丁

要下载wget-list

1
2
3
mkdir -v $LFS/sources
chmod -v a+wt $LFS/sources
wget --input-file=wget-list --continue --directory-prefix=$LFS/sources

我这里虚拟机的CentOS 7连不上github,然后我配置了一个Clash,用了我自己的节点,进行的下载。如果你下载不了的话自己想办法吧,仁者见仁智者见智。

好叭Clash配置也不好用,换了一个源

下载wget-list-LFS7.7-systemd-USTC

感谢中科大提供的源!

下载md5sums进行验证是否下载完成

1
md5sum -c md5sums

最后的准备工作

1
2
mkdir -v $LFS/tools
ln -sv $LFS/tools /

在执行第一个命令的时候不知道为什么全局变量$LFS失效了,在根目录生了tools文件夹,导致第二步一直不成功。把文件夹都删了重新来就行了。

现在知道了,export设置的全局变量仅在当前终端有效,关了终端就没了,得重新设置!!!

1
2
3
4
5
6
7
groupadd lfs
useradd -s /bin/bash -g lfs -m -k /dev/null lfs
passwd lfs
//我设置成了123456
chown -v lfs $LFS/tools
chown -v lfs $LFS/sources
su - lfs//登录lfs

接着在bash下设置环境

1
2
3
cat > ~/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF
1
2
3
4
5
6
7
8
9
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
1
source ~/.bash_profile

构建临时系统

1
chmod 777 -R $LFS
1
2
//root下载texinfo
yum install -y texinfo
1
2
3
4
5
6
cd $LFS/sources/
tar -jxvf binutils-2.25.tar.bz2
cd ../binutils-2.25
sed -i '/k prot/agcc_cv_libc_provides_ssp=yes' gcc/configure
mkdir -v ../binutils-build
cd ../binutils-build
1
2
3
4
5
6
7
../binutils-2.25/configure     \
--prefix=/tools \
--with-sysroot=$LFS \
--with-lib-path=/tools/lib \
--target=$LFS_TGT \
--disable-nls \
--disable-werror
1
2
make
make install
1
2
3
cd ..
rm -rf binutils-build
rm -rf binutils-2.25
1
2
3
4
//root运行
chmod u+w /etc/sudoers
vi /etc/sudoers
//把lfs添加到sudoer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
tar -xf gmp-6.0.0a.tar.xz
cd gmp-6.0.0
./configure
make
sudo make install

cd ..
tar -xf mpfr-3.1.2.tar.xz
cd mpfr-3.1.2
./configure
make
sudo make install

cd ..
tar -xf mpc-1.0.2.tar.xz
cd mpc-1.0.2
make
sudo make install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
cd ..
tar -jxvf gcc-4.9.2.tar.bz2
mkdir -v gcc-build
cd gcc-build


../gcc-4.9.2/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
sudo make install
1
2
3
4
5
6
cd ..
rm -rf gcc-4.9.2
tar -xf linux-3.19.tar.xz
make mrproper
make INSTALL_HDR_PATH=dest headers_install
sudo cp -rv dest/include/* /tools/include
1
2
3
4
5
cd ..
rm -rf linux-3.19
tar -xf glibc-2.21.tar.xz
mkdir -v glibc-build
cd glibc-build