LFS-V-系统配置

V 系统配置

引导 Linux 系统涉及多项任务。该进程必须挂载虚拟和真实文件系统,初始化设备,激活交换,检查文件系统的完整性,挂载任何交换分区或文件,设置系统时钟,启动网络,启动系统所需的任何守护进程,并完成任何用户需要的其他自定义任务。必须组织此过程以确保以正确的顺序执行任务,但同时尽可能快地执行。

第9节 系统配置

system V

System V 是自 1983 年左右开始在 Unix 和类 Unix 系统(如 Linux)中使用的经典引导过程。它由一个小程序init 组成,它设置诸如登录(通过 getty)之类的基本程序并运行一个脚本. 该脚本通常名为rc,它控制一组附加脚本的执行,这些脚本执行初始化系统所需的任务。

初始化程序是由控制/etc/inittab文件,并整理成可以由用户运行的运行级别:

1
2
3
4
5
6
7
0 — halt
1 — Single user mode
2 — Multiuser, without networking
3 — Full multiuser mode
4 — User definable
5 — Full multiuser mode with display manager
6 — reboot

好处

  • 制度完善,人尽皆知。
  • 易于定制。

缺点

  • 启动速度可能较慢。中速基本 LFS 系统需要 8-12 秒,其中引导时间是从第一条内核消息到登录提示进行测量的。网络连接通常在登录提示后约 2 秒建立。
  • 启动任务的串行处理。这与上一点有关。任何进程(例如文件系统检查)的延迟都会延迟整个启动过程。
  • 不直接支持控制组 (cgroups) 和每用户公平共享调度等高级功能。
  • 添加脚本需要手动、静态的排序决策。

LFS-Bootscripts-20210608

1
2
3
4
5
6
7
8
tar -xvf lfs-bootscripts-20210608.tar.xz
cd lfs-bootscripts-20210608

make install

cd ..
rm -rf lfs-bootscripts-20210608

网络配置

配置系统主机名

1
echo "lfs-hippo_2021-12-11" > /etc/hostname

设置hosts文件

1
2
3
4
5
6
cat > /etc/hosts << "EOF"
# Begin /etc/hosts (no network card version)
127.0.0.1 mylinux localhost
# End /etc/hosts (no network card version)
EOF

设置网络静态地址

1
2
3
4
5
6
7
8
9
10
cd /etc/sysconfig/
cat > ifconfig.eth0 << "EOF"
ONBOOT=yes
SERVICE=ipv4-static
IP=192.168.44.128
GATEWAY=192.168.44.2
PREFIX=24
BROADCAST=192.168.44.255
EOF

设置DNS

1
2
3
4
5
6
7
cat > /etc/resolv.conf << "EOF"
# Begin /etc/resolv.conf
202.38.193.33
202.112.17.33
# End /etc/resolv.conf
EOF

System V 引导脚本使用和配置

Linux 使用一个名为 SysVinit 的特殊引导工具,它基于运行级别的概念。从一个系统到另一个系统可能会有很大的不同,所以不能假设因为在一个特定的 Linux 发行版中工作,它们在 LFS 中也应该工作相同。LFS 有自己的做事方式,但它尊重普遍接受的标准。

SysVinit(从现在起将被称为“ init ”)使用运行级别方案工作。共有七个(编号为 0 到 6)run-level(实际上还有更多的 run-level,但它们是针对特殊情况的,一般不使用。详情请参阅init(8)),每一个都对应于计算机应该在启动时执行。默认运行级别为 3。以下是对不同运行级别的描述:

0:停止计算机
1:单用户模式
2:不联网的多用户模式
3:联网的多用户模式
4:保留用于定制,其他同 3
5:同 4,通常用于GUI 登录(如 X 的 xdm 或 KDE 的 kdm
6:重新启动计算机

配置Sysinit

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
cat > /etc/inittab << "EOF"
# Begin /etc/inittab

id:3:initdefault:

si::sysinit:/etc/rc.d/init.d/rc S

l0:0:wait:/etc/rc.d/init.d/rc 0
l1:S1:wait:/etc/rc.d/init.d/rc 1
l2:2:wait:/etc/rc.d/init.d/rc 2
l3:3:wait:/etc/rc.d/init.d/rc 3
l4:4:wait:/etc/rc.d/init.d/rc 4
l5:5:wait:/etc/rc.d/init.d/rc 5
l6:6:wait:/etc/rc.d/init.d/rc 6

ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

su:S016:once:/sbin/sulogin

1:2345:respawn:/sbin/agetty --noclear tty1 9600
2:2345:respawn:/sbin/agetty tty2 9600
3:2345:respawn:/sbin/agetty tty3 9600
4:2345:respawn:/sbin/agetty tty4 9600
5:2345:respawn:/sbin/agetty tty5 9600
6:2345:respawn:/sbin/agetty tty6 9600

# End /etc/inittab
EOF

配置系统时钟

1
2
3
4
5
6
7
8
9
10
cat > /etc/sysconfig/clock << "EOF"
# Begin /etc/sysconfig/clock

UTC=1

# Set this to any options you might need to give to hwclock,
# such as machine hardware clock type for Alphas.

# End /etc/sysconfig/clock
EOF

创建 /etc/inputrc 文件

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
cat > /etc/inputrc << "EOF"
# Begin /etc/inputrc
# Modified by Chris Lynn <roryo@roryo.dynup.net>

# 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 in 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

创建 /etc/shells 文件

1
2
3
4
5
6
7
8
cat > /etc/shells << "EOF"
# Begin /etc/shells

/bin/sh
/bin/bash

# End /etc/shells
EOF

创建Bash Shell启动文件

1
2
3
4
5
6
7
8
cat > /etc/profile << "EOF"
# Begin /etc/profile
export LANG=zh_CN.UTF-8
export INPUTRC=/etc/inputrc
alias ls="ls --color"
export PS1='\u:\w\$ '
# End /etc/profile
EOF

第10节 使LFS系统可引导

创建/etc/fstab文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat > /etc/fstab << "EOF"
# Begin /etc/fstab

# file system mount-point type options dump fsck
# order

/dev/sda3 / ext4 defaults 1 1
/dev/sda2 swap swap pri=1 0 0
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=0755,nosuid 0 0

# End /etc/fstab
EOF

安装内核Linux-5.13.12

1
2
3
4
5
6
7
cd /sources
tar -xvf linux-5.13.12.tar.xz
cd linux-5.13.12

make mrproper
make menuconfig

一定要按照以下列表启用/禁用/设定其中列出的内核特性,否则系统可能不能正常工作,甚至根本无法引导:

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
Device Drivers  --->
Generic Driver Options --->
[ ] Support for uevent helper [CONFIG_UEVENT_HELPER]
[*] Maintain a devtmpfs filesystem to mount at /dev [CONFIG_DEVTMPFS]


General setup -->
[ ] Auditing Support [CONFIG_AUDIT]
[*] Control Group support [CONFIG_CGROUPS]
[ ] Enable deprecated sysfs features to support old userspace tools [CONFIG_SYSFS_DEPRECATED]
[*] Configure standard kernel features (expert users) [CONFIG_EXPERT] --->
[*] open by fhandle syscalls [CONFIG_FHANDLE]
Processor type and features --->
[*] Enable seccomp to safely compute untrusted bytecode [CONFIG_SECCOMP]?????
Firmware Drivers --->
[*] Export DMI identification via sysfs to userspace [CONFIG_DMIID]
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]
Firmware Loader --->
[ ] Enable the firmware sysfs fallback mechanism [CONFIG_FW_LOADER_USER_HELPER]
File systems --->
[*] Inotify support for userspace [CONFIG_INOTIFY_USER]
Pseudo filesystems --->
[*] Tmpfs POSIX Access Control Lists [CONFIG_TMPFS_POSIX_ACL]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
make
make modules_install
cp -iv arch/x86_64/boot/bzImage /boot/vmlinuz-5.13.12-lfs-11.0-systemd
cp -iv System.map /boot/System.map-5.13.12
cp -iv .config /boot/config-5.13.12
install -d /usr/share/doc/linux-5.13.12
cp -r Documentation/* /usr/share/doc/linux-5.13.12

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

cd ..
rm -rf linux-5.13.12

GRUB

设定 GRUB 配置
GRUB 的工作方式是,将数据写入硬盘的第一个物理磁道。这里不属于任何文件系统,在启动时,第一个物理磁道中的程序从引导分区加载 GRUB 模块,默认在 /boot/grub 中查找模块。

引导分区的位置由负责进行配置的用户自己决定,作者推荐创建一个小的 (建议大小为 200 MB) 分区,专门存放引导信息。这样,不同的 Linux 系统 (无论是 LFS 还是商业发行版) 在启动时和启动后都能访问相同的引导文件。如果您选择这样做,您需要挂载这个单独的分区,将 /boot 中已有的文件 (例如上一节中构建的内核) 移动到新的分区中。之后,解除该分区的挂载,并将它挂载为 /boot。另外,还要注意更新 /etc/fstab。

直接使用 LFS 分区也是可以的,但这样在配置多系统启动时比较麻烦。

根据以上信息,确定 LFS 根分区 (或 boot 分区,如果使用了独立的 boot 分区) 的名称。下面 LFS 根分区 (或 boot 分区) 是 sda3。

将 GRUB 文件安装到 /boot/grub 并设定引导磁道:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
grub-install /dev/sda

cat > /boot/grub/grub.cfg << "EOF"
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod ext2
set root=(hd0,3)

menuentry "GNU/Linux, Linux 5.13.12-lfs-11.0-systemd" {
linux /boot/vmlinuz-5.13.12-lfs-11.0-systemd root=/dev/sda3 ro
}
EOF

GRUB 是一个非常强大的程序,它提供了大量的选项来从各种设备、操作系统和分区类型进行引导。还有许多自定义选项,例如图形闪屏、播放声音、鼠标输入等。这些选项的详细信息超出了本介绍的范围。

第11节 尾声

创建关于LFS的版本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
echo 11.0 > /etc/lfs-release

cat > /etc/lsb-release << "EOF"
DISTRIB_ID="Linux From Scratch"
DISTRIB_RELEASE="11.0"
DISTRIB_CODENAME="LFS-11.0_2021_12_11"
DISTRIB_DESCRIPTION="Linux From Scratch"
EOF


cat > /etc/os-release << "EOF"
NAME="Linux From Scratch"
VERSION="11.0"
ID=lfs
PRETTY_NAME="Linux From Scratch 11.0"
VERSION_CODENAME="LFS-11.0_2021_12_11"
EOF

重启

1
2
3
4
5
logout

umount -RV $LFS

shutdown -r now

笔者重启后无法进入Gurb引导界面,因此无法进入构建的LFS系统,于是直接寄寄寄寄,大寄。