lfs-7.7学习笔记(4)系统配置

五、基本系统配置

到这一步,基本系统就已经编译好了,只需要配置即可,原来的tools已经目录没用了,sources目录还后面编译内核时还要用到,暂时要保留,不想保留拷贝一份linux-3.19.tar.xz出来也可以。

重新进入chroot环境,先logout,在进入:


chroot "$LFS" /usr/bin/env -i \
 HOME=/root TERM="$TERM" PS1='\u:\w\$ ' \
 PATH=/bin:/usr/bin:/sbin:/usr/sbin \
 /bin/bash --login

干掉ools目录:

rm -rf  /tools

如果宿主系统有重启过,或卸载过虚拟内核文件系统,进入chroot前需要重新挂载:


mount -v --bind /dev $LFS/dev
mount -vt devpts devpts $LFS/dev/pts -o gid=5,mode=620
mount -vt proc proc $LFS/proc
mount -vt sysfs sysfs $LFS/sys
mount -vt tmpfs tmpfs $LFS/run

5.1 通用网络配置

5.1.1 配置网卡

默认情况下网卡的名字不是eth0,需要使用udev绑定,绑定方法:
查看网卡的mac地址:

ip link

假设物理网卡的地址是A0:48:1C:9A:01:5C,新建udev配置文件:

touch /etc/udev/rules.d/70-persistent-net.rules

内容为:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="A0:48:1C:9A:01:5C", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

这样在启动时udev就会将物理网卡绑定到eth0,可以使用eth0配置网络

配置DHCP还是静态IP,按个人情况,二选一,配置静态ip:

cat > /etc/systemd/network/10-static-eth0.network << "EOF"
[Match]
Name=eth0
[Network]
Address=192.168.0.2/24
Gateway=192.168.0.1
DNS=192.168.0.1
EOF

配置DHCP:


cat > /etc/systemd/network/10-dhcp-eth0.network << "EOF"
[Match]
Name=eth0
[Network]
DHCP=yes
EOF

5.1.2 创建 /etc/resolv.conf

resolv.conf是静态dns的配置文件

cat > /etc/resolv.conf << "EOF"
# Begin /etc/resolv.conf
domain <Your Domain Name>
nameserver <IP address of your primary nameserver>
nameserver <IP address of your secondary nameserver>

# End /etc/resolv.conf
EOF

创建标准的符号链接:

ln -sfv /run/systemd/resolve/resolv.conf /etc/resolv.conf

5.1.3 创建主机名

echo "LFS" > /etc/hostname

5.1.4 创建hosts文件

在4.5.35 安装 Perl中已经创建了 hosts文件,这里省略,需要注意的是主机名应该在hosts中
如:

cat /etc/hosts
127.0.0.1 localhost LFS

5.2 配置系统时间

system的会读取/etc/adjtime的时间配置,所以创建/etc/adjtime:

cat > /etc/adjtime << "EOF"
0.0 0 0.0
0
LOCAL
EOF

5.3 设置系统区域

查看 Glibc 支持的本地字符集:

locale -a

按照之前编译glibc时的配置,系统中是有zh_CN.gb18030,没有zh_CN.utf8,安装:

localedef -i zh_CN -f UTF-8 zh_CN.utf8

创建 /etc/locale.conf:

cat > /etc/locale.conf << "EOF"
LANG=zh_CN.UTF8
EOF

5.4 配置键盘布局

创建 /etc/inputrc 文件:

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

5.5 配置shell

创建/etc/shells 文件:

cat > /etc/shells << "EOF"
# Begin /etc/shells
/bin/sh
/bin/bash    
# End /etc/shells
EOF

5.6 配置systemd

禁止 /tmp 使用 tmpfs:

ln -sfv /dev/null /etc/systemd/system/tmp.mount

六、 LFS 系统引导配置

6.1 创建 /etc/fstab 文件


cat > /etc/fstab << "EOF"
# Begin /etc/fstab
# 文件系统 挂载点 文件类型 挂载选项 dump fsck
# order
/dev/sdb1 /  defaults 1 1
# End /etc/fstab
EOF

6.2 编译内核

令准备编译:


make mrproper

生成内核配置文件:


make defconfig

会根据机器架构生成一份配置,保存在内核源码目录下.config文件中,确保以下内容是打开的:

CONFIG_FHANDLE=y
CONFIG_CGROUPS=y
CONFIG_SECCOMP=y
CONFIG_IPV6=y
CONFIG_DEVTMPFS=y
CONFIG_DMIID=y
ONFIG_INOTIFY_USER=y
CONFIG_AUTOFS4_FS=y
CONFIG_TMPFS_POSIX_ACL=y
CONFIG_TMPFS_XATTR=y

如果宿主机是vmware,还需要如下配置:

CONFIG_FUSION=y
CONFIG_FUSION_SPI=y
CONFIG_FUSION_SAS=y
CONFIG_FUSION_MAX_SGE=y
CONFIG_FUSION_CTL=y
CONFIG_FUSION_LOGGING=y

CONFIG_VMWARE_BALLOON=y
CONFIG_VMWARE_PVSCSI=y

CONFIG_HYPERVISOR_GUEST=Y

不添加这些配置,LFS启动时会报类似"kernel panic"或者"VFS: unable to mount root fs"的错误

或者通过下面命令,使用命令行下的图形界面设置:

make LANG=zh_CN.UTF8 LC_ALL= menuconfig

编译:


make

安装模块:

make modules_install

根据必要的文件到/boot下:

cp -v arch/x86_64/boot/bzImage /boot/vmlinuz-3.19-lfs-7.7-systemd
cp -v System.map /boot/System.map-3.19

安装 Linux 内核文档:

install -d /usr/share/doc/linux-3.19
cp -r Documentation/* /usr/share/doc/linux-3.19

6.3 设置启动过程

安装Grub:


grub-install /dev/sdb

创建 GRUB 配置文件:

cat > /boot/grub/grub.cfg << "EOF"
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5
insmod ext2
set root=(hd0,1)   
menuentry "GNU/Linux, Linux 3.19-lfs-7.7-systemd" {
 linux /boot/vmlinuz-3.19-lfs-7.7-systemd root=/dev/sda1 ro
}
EOF

grub配置文件中root=/dev/sda1 ro是磁盘挂载为只读,测试启动成功后可以改成rw

6.4 创建/etc/os-release文件

cat > /etc/os-release << "EOF"
NAME="Linux From Scratch"
VERSION="7.7-systemd"
ID=lfs
PRETTY_NAME="Linux From Scratch 7.7-systemd"
EOF

到这里新系统就大功告成了,虽然好还有好多软件要配置,但可以开机启动,体验自己编译好的新系统了
最后可以去官方注册为LFS用户: http://www.linuxfromscratch.org/cgi-bin/lfscounter.php

lfs-7.7学习笔记(1)准备篇
lfs-7.7学习笔记(2)构建临时系统
lfs-7.7学习笔记(3)构建 LFS 系统
lfs-7.7学习笔记(4)系统配置