Welcome to Linux From Scratch!
Linux From Scratch (LFS) is a project that provides you with step-by-step instructions for building your own custom Linux system, entirely from source code.
http://www.linuxfromscratch.org/
1.准备工作
1.1 准备安装包
- 下载
lfs book
使用svn
下载lfs book
,这里采用8.0版本
[root@test ~]# svn co svn://svn.linuxfromscratch.org/LFS/tags/8.2
[root@test ~]# svn co svn://svn.linuxfromscratch.org/LFS/tags/8.0 # 用这个版本
- 下载
lfs packages
下载最近的8.0版本,300M多一些
wget http://ftp.lfs-matrix.net/pub/lfs/lfs-packages/lfs-packages-8.2.tar
wget http://ftp.lfs-matrix.net/pub/lfs/lfs-packages/lfs-packages-8.0.tar # 用这个版本
- 下载
alfs
脚本
svn co svn://svn.linuxfromscratch.org/ALFS/jhalfs/trunk jhalfs
wget http://www.linuxfromscratch.org/alfs/downloads/jhalfs/stable/jhalfs-2.4.tar.xz #用这个版本
1.2 准备环境
- 操作系统,
CENTOS6/RHEL6
不不能编译8.2版本,内核版本太低,但是可以编译8.0版本。
[root@test ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@test ~]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
准备一个用于安装alfs
的一个磁盘,这里是sdb
,特别注意的是分区务必使用mbr
进行分区。
[root@test ~]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 190M 0 part # 用来做boot
├─sdb2 8:18 0 976M 0 part # swap
└─sdb3 8:19 0 98.9G 0 part # 根 /
检查操作系统包安装准备
[root@test ~]# cat version-check.sh
#!/bin/bash
# Simple script to list version numbers of critical development tools
export LC_ALL=C
bash --version | head -n1 | cut -d" " -f2-4
echo "/bin/sh -> `readlink -f /bin/sh`"
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
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 yacc 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
xz --version | head -n1
echo '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
检查结果
[root@test ~]# bash version-check.sh
1.3 准备磁盘和目录
新磁盘分区并创建ext4
文件系统
[root@test ~]# mkfs.ext4 /dev/sdb1
[root@test ~]# mkfs.ext4 /dev/sdb3
设置LFS
根分区,并挂载目录
[root@test ~]# export LFS=/mnt
[root@test ~]# mount /dev/sdb3 $LFS
[root@test ~]# mkdir $LFS/boot
[root@test ~]# mount /dev/sdb1 $LFS/boot
准备源码放置目录,并将包解压到这个目录中
[root@test ~]# mkdir $LFS/sources
[root@test ~]# chmod -v a+wt $LFS/sources
mode of `/mnt/sources' changed to 1777 (rwxrwxrwt)
[root@test ~]# ls $LFS/sources
total 367152
drwxrwxr-x 14 1011 1011 4096 Aug 3 18:52 jhalfs-2.4
-rw-r--r-- 1 root root 180656 Apr 8 2017 jhalfs-2.4.tar.xz
drwxrwxr-x 2 vagrant vagrant 4096 Feb 15 2017 lfs-packages-8.0
-rw-r--r-- 1 root root 375767040 Feb 15 2017 lfs-packages-8.0.tar
drwxrwxr-x 2 vagrant vagrant 4096 Mar 3 04:44 lfs-packages-8.2
准备lfsbook放到指定位置
[root@test mnt]# cp -r /root/lfs_book/8.0 $LFS/lsbook8.0
准备alfs
,将jhalfs
目录内容拷贝到sources
[root@test ~]# ls $LFS/sources
total 367152
drwxrwxr-x 14 1011 1011 4096 Aug 3 18:52 jhalfs-2.4 # 用这个
-rw-r--r-- 1 root root 180656 Apr 8 2017 jhalfs-2.4.tar.xz
创建一个编译目录
[root@test sources]# mkdir $LFS/lfsbuild8.0
[root@test ~]# ls -l $LFS
total 37
drwxr-xr-x 3 root root 1024 Aug 2 11:18 boot
drwxr-xr-x 18 root root 4096 Aug 3 18:44 lfsbook8.0 # lfsbook位置
drwxr-xr-x 18 root root 4096 Aug 2 11:29 lfsbook8.2
drwxr-xr-x 24 root root 4096 Aug 3 11:50 lfsbuild
drwxr-xr-x 4 root root 4096 Aug 3 18:52 lfsbuild8.0 # 编译目标目录
drwx------ 2 root root 16384 Aug 2 11:18 lost+found
drwxrwxrwt 5 root root 4096 Aug 3 18:50 sources # 源文件,jhalfs和lfs-package
1.4 准备用户
添加jhalfs
用户
[root@test mnt]# useradd -m -k /dev/null jhalfs
[root@test mnt]# passwd jhalfs
[root@test mnt]# visudo
# 添加一行
jhalfs ALL=(ALL) ALL
2.开始编译
2.1 准备ALFS
首先准备和编译jhalfs
[root@test jhalfs]# pwd
/mnt/sources/jhalfs
[root@test jhalfs]# make
设置
分成三个部分:
- Book Setting
- general Setting
- Build Setting
其中BOOK setting
- 使用版本
systemd
release workcopy
,并填写位置,存档book
的位置
GENERAL settings
- build目录:存放编译好的文件
- package目录:存放本地下载的包
Build setting
可能会缺少xsltproc
和ncurce
包,进行安装
[root@test jhalfs]# yum install libxslt
[root@test jhalfs]# yum install ncurses-devel
完成会输出大概如下
Creating Makefile... START
Processing... <Chapter4 ( SETUP ) >
Processing... <Chapter5 ( LUSER ) >
Processing... <Chapter6 ( CHROOT ) >
Processing... <Chapter7/8 ( BOOT ) >
Creating Makefile... DONE
------------------------------------------------------------------------------
KERNEL............ <3.10.0-693.el7.x86_64> OK (Min version: 3.2)
BASH.............. <4.2.46(2)-release> OK (Min version: 3.2)
GCC............... <4.8.5> OK (Min version: 4.7)
G++............... <4.8.5> OK (Min version: 4.7)
GLIBC............. <2.17> OK (Min version: 2.11)
BINUTILS.......... <2.25.1-31.base.el7> OK (Min version: 2.17)
TAR............... <1.26> OK (Min version: 1.22)
BZIP2............. <1.0.6> OK (Min version: 1.0.4)
BISON............. <3.0.4> OK (Min version: 2.3)
COREUTILS......... <8.22> OK (Min version: 6.9)
DIFF.............. <3.3> OK (Min version: 2.8.1)
FIND.............. <4.5.11> OK (Min version: 4.2.31)
GAWK.............. <4.0.2> OK (Min version: 4.0.1)
GREP.............. <2.20> OK (Min version: 2.5.1a)
GZIP.............. <1.5> OK (Min version: 1.3.12)
M4................ <1.4.16> OK (Min version: 1.4.10)
MAKE.............. <3.82> OK (Min version: 3.81)
PATCH............. <2.7.1> OK (Min version: 2.5.4)
PERL.............. <5.16.3> OK (Min version: 5.8.8)
SED............... <4.2.2> OK (Min version: 4.1.5)
TEXINFO........... <5.1> OK (Min version: 4.7)
XZ................ <5.2.2> OK (Min version: 5.0.0)
------------------------------------------------------------------------------
保存了config
文件名为.config-80
:
[root@test jhalfs-2.4]# more .config-80
#
# Automatically generated configuration: don't edit
# use make menuconfig or make config to make changes
#
#
# BOOK Settings
#
# BOOK_LFS is not set
BOOK_LFS_SYSD=y
# BOOK_CLFS is not set
# BOOK_CLFS2 is not set
# BOOK_CLFS3 is not set
# BOOK_BLFS is not set
# BLFS_SYSV is not set
# BLFS_SYSD is not set
INITSYS="systemd"
PROGNAME="lfs"
RUN_ME="./jhalfs run"
# relSVN is not set
# relGIT is not set
WORKING_COPY=y
# BRANCH is not set
BRANCH_ID=""
BOOK="/mnt/lfsbook8.0"
# ARCH_X86 is not set
# ARCH_MIPS is not set
# ARCH_PPC is not set
# ARCH_SPARC is not set
# ARCH_ALPHA is not set
# ARCH_ARM is not set
# ARCH_HPPA is not set
# PLATFORM_GENERIC is not set
# PLATFORM_WRT is not set
# DATA_32 is not set
# DATA_64 is not set
# DATA_MULTI is not set
# PROC_i486 is not set
# PROC_i586 is not set
# PROC_i686 is not set
# PROC_mipsel is not set
# PROC_mips is not set
# PROC_unknown is not set
# PROC_hppa1 is not set
# PROC_hppa2 is not set
# PROC_EV5 is not set
# PROC_EV56 is not set
# PROC_PCA56 is not set
# PROC_PCA57 is not set
# PROC_EV6 is not set
# PROC_EV67 is not set
# PROC_EV68 is not set
# PROC_ARM is not set
# PROC_ARM5L is not set
# PROC_ARM5B is not set
# PROC_ULTRA1 is not set
# PROC_ULTRA2 is not set
# PROC_ULTRA3 is not set
# ABI_32 is not set
# ABI_N32 is not set
# ABI_64 is not set
TARGET=""
TARGET32=""
PLATFORM="GENERIC"
SPARC64_PROC="none"
ARCH=""
MIPS_LEVEL=""
ABI=""
ENDIAN=""
# BUILD_CHROOT is not set
# BUILD_BOOT is not set
METHOD=""
BOOT_CONFIG=""
# BLFS_TOOL is not set
# DUMMY is not set
# DEP_LIBXML is not set
# DEP_LIBXSLT is not set
# DEP_DBXML is not set
# DEP_LYNX is not set
# DEP_SUDO is not set
# DEP_WGET is not set
# DEP_GPM is not set
# DEP_SVN is not set
# DEP_OPENSSL is not set
# DEP_PYTHON is not set
# BLFS_SVN is not set
# BLFS_WORKING_COPY is not set
# BLFS_BRANCH is not set
BLFS_WC_LOCATION=""
BLFS_BRANCH_ID=""
BLFS_ROOT=""
BLFS_XML=""
# CUSTOM_TOOLS is not set
TRACKING_DIR=""
#
# General Settings
#
# CONFIG_USER is not set
DEF_USER="lfs"
SET_USER=""
# CONFIG_GROUP is not set
SET_GROUP=""
# CONFIG_HOME is not set
SET_HOME=""
LUSER="lfs"
LGROUP="lfs"
LHOME="/home"
BUILDDIR="/mnt/lfsbuild8.0"
GETPKG=y
SRC_ARCHIVE="/mnt/sources/lfs-packages-8.0"
# RETRYSRCDOWNLOAD is not set
RETRYDOWNLOADCNT=20
DOWNLOADTIMEOUT=30
SERVER="http://ftp.osuosl.org"
# RUNMAKE is not set
# CLEAN is not set
#
# Build Settings
#
# CONFIG_TESTS is not set
# TST_1 is not set
# TST_2 is not set
# TST_3 is not set
TEST=0
# NO_BOMB is not set
# BOMB is not set
# BOMB_TEST is not set
# PKGMNGT is not set
INSTALL_LOG=y
# HAVE_FSTAB is not set
FSTAB=""
# CONFIG_BUILD_KERNEL is not set
CONFIG=""
# STRIP is not set
# VIMLANG is not set
# NO_PROGRESS_BAR is not set
TIMEZONE="Asia/Shanghai"
LANG="$LANG"
FULL_LOCALE=y
PAGE_LETTER=y
# PAGE_A4 is not set
PAGE="letter"
#
# Advanced Features
#
REPORT=y
# COMPARE is not set
ITERATIONS=0
# RUN_ICA is not set
# RUN_FARCE is not set
# CONFIG_OPTIMIZE is not set
N_PARALLEL=0
# OPT_1 is not set
# OPT_2 is not set
# OPT_3 is not set
OPTIMIZE=0
#
# Internal Settings (WARNING: for jhalfs developers only)
#
SCRIPT_ROOT="jhalfs"
JHALFSDIR="$BUILDDIR/$SCRIPT_ROOT"
LOGDIRBASE="logs"
LOGDIR="$JHALFSDIR/$LOGDIRBASE"
TESTLOGDIRBASE="test-logs"
TESTLOGDIR="$JHALFSDIR/$TESTLOGDIRBASE"
FILELOGDIRBASE="installed-files"
FILELOGDIR="$JHALFSDIR/$FILELOGDIRBASE"
ICALOGDIR="$LOGDIR/ICA"
FARCELOGDIR="$LOGDIR/farce"
MKFILE="$JHALFSDIR/Makefile"
XSL="$PROGNAME.xsl"
PKG_LST="unpacked"
# REBUILD_MAKEFILE is not set
2.2 编译软件包
开始编译软件包
[root@test jhalfs-2.4]# su - jhalfs
-bash-4.2$ cd /mnt/lfsbuild8.0/
-bash-4.2$ ls
jhalfs sources
-bash-4.2$ make -C /mnt/lfsbuild8.0/jhalfs/
开始自动编译软件包
输出如下所示大概
--------------------------------------------------------------------------------
Building target 020-creatingtoolsdir
Target 020-creatingtoolsdir OK
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Building target 021-addinguser
Target 021-addinguser OK
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Building target 022-settingenvironment
Target 022-settingenvironment OK
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
mk_LUSER
You are going to log into the user account lfs
sudo requires a password
make: Entering directory `/mnt/lfsbuild8.0/jhalfs'
--------------------------------------------------------------------------------
Building target 034-binutils-pass1
[+++++++++++++++++++++++| ] 3 min. 23 sec
Target 034-binutils-pass1 OK
--------------------------------------------------------------------------------
...
时间很长,耐心等待。。。半天吧。。。
如果中间意外断开的话,再次启动即可。
[root@test jhalfs-2.4]# su - jhalfs
-bash-4.2$ cd /mnt/lfsbuild8.0/
-bash-4.2$ make -C /mnt/lfsbuild8.0/jhalfs/
开始自动编译软件包
出现下面的提示,说明完成了
--------------------------------------------------------------------------------
Have a nice day :-)
--------------------------------------------------------------------------------
make: Leaving directory `/mnt/lfsbuild8.0/jhalfs'
2.2 形成LFS
系统
拷贝目录到$LFS
中
[root@test lfsbuild8.0]# for f in `ls |grep -vE "jhalfs|sources"`;do cp -r $f /mnt/ ;done
激活和挂载dev
和虚拟文件系统
mount -v --bind /dev /mnt/dev
mount -vt devpts devpts /mnt/dev/pts -o gid=5,mode=620
mount -vt proc proc /mnt/proc
mount -vt sysfs sysfs /mnt/sys
mount -vt tmpfs tmpfs /mnt/run
chroot
chroot /mnt /usr/bin/env -i HOME=/root TERM="$TERM" PS1="$PS1" PATH=/bin:/usr/bin:/sbin:/usr/sbin /bin/bash --login
创建/etc/fstab
文件
bash-4.4# cat /etc/fstab
# Begin /etc/fstab
# file system mount-point type options dump fsck
# order
/dev/sda3 / ext4 defaults 1 1
/dev/sda1 /boot ext4 defaults 1 1
proc /proc proc nosuid,noexec,nodev 0 0
sysfs /sys sysfs nosuid,noexec,nodev 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /run tmpfs defaults 0 0
devtmpfs /dev devtmpfs mode=755,nosuid 0 0
# End /etc/fstab
2.4 编译内核
解压内核包
bash-4.4# tar -xf linux-4.9.9.tar.xz
开始配置内核
bash-4.4# cd linux-4.9.9
bash-4.4# make mrproper
bash-4.4# make menuconfig
根据lfsbook
,进行下面的调整:
General setup -->
[ ] Enable deprecated sysfs features to support old userspace tools [CONFIG_SYSFS_DEPRECATED]
[ ] Enable deprecated sysfs features by default [CONFIG_SYSFS_DEPRECATED_V2]
[*] open by fhandle syscalls [CONFIG_FHANDLE]
[ ] Auditing support [CONFIG_AUDIT]
[*] Control Group support [CONFIG_CGROUPS]
Processor type and features --->
[*] Enable seccomp to safely compute untrusted bytecode [CONFIG_SECCOMP]
Networking support --->
Networking options --->
<*> The IPv6 protocol [CONFIG_IPV6]
Device Drivers --->
Generic Driver Options --->
[ ] Support for uevent helper [CONFIG_UEVENT_HELPER]
[*] Maintain a devtmpfs filesystem to mount at /dev [CONFIG_DEVTMPFS]
[ ] Fallback user-helper invocation for firmware loading [CONFIG_FW_LOADER_USER_HELPER]
Firmware Drivers --->
[*] Export DMI identification via sysfs to userspace [CONFIG_DMIID]
File systems --->
[*] Inotify support for userspace [CONFIG_INOTIFY_USER]
<*> Kernel automounter version 4 support (also supports v3) [CONFIG_AUTOFS4_FS]
Pseudo filesystems --->
[*] Tmpfs POSIX Access Control Lists [CONFIG_TMPFS_POSIX_ACL]
[*] Tmpfs extended attributes [CONFIG_TMPFS_XATTR]
可以使用图形界面进行修改,也可以直接修改配置文件.config
,只需要修改 [ ] Support for uevent helper [CONFIG_UEVENT_HELPER]
即可, 其他都是默认参数。
开始编译安装,时间比较长:
bash-4.4# make && make modules_install
拷贝一些文件:
bash-4.4# cp -v arch/x86/boot/bzImage /boot/vmlinuz-4.9.9-lfs-8.0-systemd
bash-4.4# cp -v System.map /boot/System.map-4.9.9
bash-4.4# cp -v .config /boot/config-4.9.9
bash-4.4# install -d /usr/share/doc/linux-4.9.9
bash-4.4# cp -r Documentation/* /usr/share/doc/linux-4.9.9
修改/etc/modprobe.d
:
install -v -m755 -d /etc/modprobe.d
cat > /etc/modprobe.d/usb.conf << "EOF"
# Begin /etc/modprobe.d/usb.conf
install ohci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i ohci_hcd ; true
install uhci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i uhci_hcd ; true
# End /etc/modprobe.d/usb.conf
EOF
2.5 创建启动分区
创建分区
bash-4.4# grub-install /dev/sdb
编写配置文件
bash-4.4# cat /boot/grub/grub.cfg
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5
insmod ext4 # 根据分区的时候定义的文件系统类型
set root=(hd0,1) #boot分区的位置
menuentry "GNU/Linux, Linux 4.9.9-lfs-8.0-systemd" {
linux vmlinuz-4.9.9-lfs-8.0-systemd root=/dev/sda3 ro
}
2.6.完成配置
设置主机名
echo "lfs80" > /etc/hostname
设置密码
passwd root
将这个磁盘单独拿出,创建一个虚拟机来测试一下
3.问题处理
3.1 如果分区使用GPT
在创建启动分区的时候会出错
bash-4.4# grub-install /dev/sdb
Installing for i386-pc platform.
grub-install: warning: this GPT partition label contains no BIOS Boot Partition; embedding won't be possible.
grub-install: warning: Embedding is not possible. GRUB can only be installed in this setup by using blocklists. However, blocklists are UNRELIABLE and their use is discouraged..
grub-install: error: will not proceed with blocklists.
解决办法:
https://www.cnblogs.com/donaldjohn/p/6728306.html
或者重新使用一个磁盘,使用mbr
进行分区,将需要的文件复制过去。
4.参考
- http://www.linuxfromscratch.org/lfs/downloads/8.0-systemd/LFS-BOOK-8.0-systemd-NOCHUNKS.html
- http://ftp.lfs-matrix.net/pub/lfs/lfs-packages/8.0/
- https://wenku.baidu.com/view/3281313e00f69e3143323968011ca300a6c3f687.html?pn=51
- LFS-BOOK-7.7-systemd.pdf