Linux From Scratch(从零开始构建Linux系统,简称LFS)(三)
九. 系统配置
1. 安装 LFS-Bootscripts-20150222
软件包包含一套在 LFS 系统启动和关闭时的启动和停止脚本。
cd /sources
tar -jxf lfs-bootscripts-20150222.tar.bz2
cd lfs-bootscripts-20150222
make install
cd /sources
rm -rf lfs-bootscripts-20150222
2. 管理网络设备
# 运行以下命令,创建网络设备 Udev 规则 bash /lib/udev/init-net-rules.sh # 查看生成的规则
cat /etc/udev/rules.d/70-persistent-net.rules
3. 配置系统主机名称
echo "lfs" > /etc/hostname
4. 创建 /etc/hosts
文件
cat > /etc/hosts << "EOF" # Begin /etc/hosts (network card version) 127.0.0.1 localhost # End /etc/hosts (network card version) EOF
5. 配置 Sysvinit
内核初始化期间,第一个运行的程序默认是SysVinit,这个程序读取/etc/inittab
文件
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
6. 配置系统时钟
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. CLOCKPARAMS= # End /etc/sysconfig/clock EOF
7. Bash 启动文件
cat > /etc/profile << "EOF" # Begin /etc/profile export LANG=en_US.UTF-8 # End /etc/profile EOF
8. 创建 /etc/inputrc 文件
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
9. 创建 /etc/shells 文件
shells
文件是当前系统所有可用 shell 的列表文件
cat > /etc/shells << "EOF" # Begin /etc/shells /bin/sh /bin/bash # End /etc/shells EOF
10. LFS官方文档中还包含了其它可选配置,可按需添加
十. 让 LFS 系统可引导
1. 创建 /etc/fstab 文件
cat > /etc/fstab << "EOF" # Begin /etc/fstab # file system mount-point type options dump fsck # order /dev/root / auto noatime,nodiratime 1 1 #/dev/xxxx swap swap pri=1 0 0 # End /etc/fstab EOF
2. 挂载 boot 分区
mkdir /mnt/boot mount /dev/sdc1 /mnt/boot
3. Linux-4.7.2
a. 宿主系统下运行下列命令,查看有线、无线网卡型号
lspci | grep -i ethernet
lspci | grep -i wireless
b. 根据型号选择合适的驱动,安装配置内核
cd /sources tar -Jxf linux-4.7.2.tar.xz cd linux-4.7.2 make mrproper make menuconfig #新增 #[*] Networking support ---> #-*- Wireless ---> #<*> cfg80211 - wireless configuration API #[*] cfg80211 wireless extensions compatibility # Device Drivers ---> #[*] Network device support ---> #[*] Ethernet driver support ---> #[*] Atheros devices #<M> Qualcomm Atheros AR816X/AR817x support #[*] Wireless LAN ---> #[*] Atheros/Qualcomm devices #<M> Atheros 802.11n wireless cards support #[*] Atheros ath9k PCI/PCIe bus support #<*> PPP (point-to-point protocol) support #<*> PPP support for async serial ports make make modules_install # 下面的命令假设主机是 x86 架构: cp -v arch/x86/boot/bzImage /mnt/boot/vmlinuz-4.7.2 cp -v System.map /mnt/boot/System.map-4.7.2 cp -v .config /mnt/boot/config-4.7.2 install -d /usr/share/doc/linux-4.7.2 cp -r Documentation/* /usr/share/doc/linux-4.7.2 # 配置 Linux 模块加载顺序 # 如果将 USB 设备(ehci_hcd、ohci_hcd 和 uhci_hcd) 编译为模块,则需要此文件。 # 运行以下命令建立 /etc/modprobe.d/usb.conf 文件: 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 /sources rm -rf linux-4.7.2
4. 创建 initrd.img 文件
a. 点击此下载 mkinitramfs 相关文件,并放到LFS系统对应的位置
b. mkinitramfs 命令依赖于 cpio 包,先安装
cd /sources tar -jxf cpio-2.12.tar.bz2 cd cpio-2.12 ./configure --prefix=/usr \ --bindir=/bin \ --enable-mt \ --with-rmt=/usr/libexec/rmt && make && makeinfo --html -o doc/html doc/cpio.texi && makeinfo --html --no-split -o doc/cpio.html doc/cpio.texi && makeinfo --plaintext -o doc/cpio.txt doc/cpio.texi make install && install -v -m755 -d /usr/share/doc/cpio-2.12/html && install -v -m644 doc/html/* \ /usr/share/doc/cpio-2.12/html && install -v -m644 doc/cpio.{html,txt} \ /usr/share/doc/cpio-2.12 cd /sources rm -rf cpio-2.12
c. 执行如下命令,重新生成 initrd
cp /sbin/udevadm /bin/
chmod 777 /usr/sbin/mkinitramfs
/usr/sbin/mkinitramfs --force
d. 移动 initrd.img-4.7.2.xz 到 boot 分区下
mv /boot/initrd.img-4.7.2.xz /mnt/boot/
5. 用 GRUB 设置引导过程
# 将GRUB文件安装到 /mnt/boot/grub,并修改MBR扇区 grub-install --root-directory=/mnt /dev/sdc # 创建GRUB配置文件 cat > /mnt/boot/grub/grub.cfg << "EOF" # Begin /boot/grub/grub.cfg set default=0 set timeout=5 menuentry "linux-4.7.2-lfs-7.10" { set kver=4.7.2 set boot_uuid=<boot分区uuid> set root_uuid=<root分区uuid> search.fs_uuid ${boot_uuid} root linux /vmlinuz-${kver} root=uuid/${root_uuid} initrd /initrd.img-${kver}.xz } EOF
十一. 最后
1. 创建 /etc/lfs-release
文件
cat > /etc/lsb-release << "EOF" DISTRIB_ID="Linux From Scratch" DISTRIB_RELEASE="7.10" DISTRIB_CODENAME="kevin" DISTRIB_DESCRIPTION="Linux From Scratch" EOF
2. 安装 OpenSSH
# openssh依赖于openssl库,先安装openssl cd /sources tar -zxf openssl-1.0.2h.tar.gz cd openssl-1.0.2h ./config --prefix=/usr \ --openssldir=/etc/ssl \ --libdir=lib \ shared \ zlib-dynamic && make depend && make make MANDIR=/usr/share/man MANSUFFIX=ssl install && install -dv -m755 /usr/share/doc/openssl-1.0.2h && cp -vfr doc/* /usr/share/doc/openssl-1.0.2h cd /sources rm -rf openssl-1.0.2h # 安装openssh cd /sources tar -zxf openssh-7.3p1.tar.gz cd openssh-7.3p1 install -v -m700 -d /var/lib/sshd && chown -v root:sys /var/lib/sshd && groupadd -g 50 sshd && useradd -c 'sshd PrivSep' \ -d /var/lib/sshd \ -g sshd \ -s /bin/false \ -u 50 sshd ./configure --prefix=/usr \ --sysconfdir=/etc/ssh \ --with-md5-passwords \ --with-privsep-path=/var/lib/sshd && make make install && install -v -m755 contrib/ssh-copy-id /usr/bin && install -v -m644 contrib/ssh-copy-id.1 \ /usr/share/man/man1 && install -v -m755 -d /usr/share/doc/openssh-7.3p1 && install -v -m644 INSTALL LICENCE OVERVIEW README* \ /usr/share/doc/openssh-7.3p1 cd /sources rm -rf openssh-7.3p1 # 系统启动时开启ssh服务 cd /sources tar -Jxf blfs-bootscripts-20160902.tar.xz cd blfs-bootscripts-20160902 make install-sshd cd /sources rm -rf blfs-bootscripts-20160902
3. 安装 dhcpcd
cd /sources tar -Jxf dhcpcd-6.11.3.tar.xz cd dhcpcd-6.11.3 ./configure --libexecdir=/lib/dhcpcd \ --dbdir=/var/lib/dhcpcd && make make install cd /sources rm -rf dhcpcd-6.11.3
4. 安装 wpa_supplicant
# wpa_supplicant依赖于libnl-3库,先安装libnl-3 cd /sources tar -zxf libnl-3.2.28.tar.gz cd libnl-3.2.28 ./configure --prefix=/usr \ --sysconfdir=/etc \ --disable-static && make make install cd /sources rm -rf libnl-3.2.28 # 安装wpa_supplicant cd /sources tar -zxf wpa_supplicant-2.5.tar.gz cd wpa_supplicant-2.5 cat > wpa_supplicant/.config << "EOF" CONFIG_BACKEND=file CONFIG_CTRL_IFACE=y CONFIG_DEBUG_FILE=y CONFIG_DEBUG_SYSLOG=y CONFIG_DEBUG_SYSLOG_FACILITY=LOG_DAEMON CONFIG_DRIVER_NL80211=y CONFIG_DRIVER_WEXT=y CONFIG_DRIVER_WIRED=y CONFIG_EAP_GTC=y CONFIG_EAP_LEAP=y CONFIG_EAP_MD5=y CONFIG_EAP_MSCHAPV2=y CONFIG_EAP_OTP=y CONFIG_EAP_PEAP=y CONFIG_EAP_TLS=y CONFIG_EAP_TTLS=y CONFIG_IEEE8021X_EAPOL=y CONFIG_IPV6=y CONFIG_LIBNL32=y CONFIG_PEERKEY=y CONFIG_PKCS12=y CONFIG_READLINE=y CONFIG_SMARTCARD=y CONFIG_WPS=y CFLAGS += -I/usr/include/libnl3 EOF cd wpa_supplicant && make BINDIR=/sbin LIBDIR=/lib install -v -m755 wpa_{cli,passphrase,supplicant} /sbin/ && install -v -m644 doc/docbook/wpa_supplicant.conf.5 /usr/share/man/man5/ && install -v -m644 doc/docbook/wpa_{cli,passphrase,supplicant}.8 /usr/share/man/man8/ cd /sources rm -rf wpa_supplicant-2.5
5. 网络配置:复制到命令行执行前,记得把中文注释去掉,否则会出错
# 有线网络配置 cat > /etc/sysconfig/ifconfig.eth0 << "EOF" ONBOOT="no" IFACE="eth0" # 静态IP SERVICE="ipv4-static" IP="192.168.1.2" # 网关 GATEWAY="192.168.1.1" # 子网掩码 PREFIX="24" # 广播域 BROADCAST="192.168.1.255" # 动态IP #SERVICE="dhcpcd" # -b background 后台运行 # -q quite 消息仍被记录,但是只有告警或错误的信息被打印 #DHCP_START="-b -q" # -k release 退出前释放租期 #DHCP_STOP="-k" EOF # 无线网络配置 cat > /etc/sysconfig/ifconfig.wlan0 << "EOF" ONBOOT="no" IFACE="wlan0" SERVICE="wpa" # Additional arguments to wpa_supplicant WPA_ARGS="" # 动态IP WPA_SERVICE="dhcpcd" DHCP_START="-b -q" DHCP_STOP="-k" # 静态IP #WPA_SERVICE="ipv4-static" #IP="192.168.1.2" #GATEWAY="192.168.1.1" #PREFIX="24" #BROADCAST="192.168.1.255" EOF # WIFI配置 cat > /etc/sysconfig/wpa_supplicant-wlan0.conf << "EOF" network={ ssid="WiFi-name1" psk="WiFi-password1" priority=5 } EOF
6. 卸载文件系统
logout umount -v $LFS/dev/pts umount -v $LFS/dev umount -v $LFS/run umount -v $LFS/proc umount -v $LFS/sys umount -v /mnt/boot
# 删除 boot 和 sources 文件夹 rm -rf $LFS/boot rm -rf $LFS/sources umount -v $LFS
7. 重启电脑,引导从U盘启动电脑
8. 默认登录用户名:root 密码:1
9. 连接无线网络命令
wpa_supplicant -B -i wlan0 -c /etc/sysconfig/wpa_supplicant-wlan0.conf
dhcpcd wlan0
十二. 如何将LFS系统做成镜像
1. 需要一台装有Linux系统的PC机
2. 利用 gparted 格式化并重新划分U盘:
/dev/sdc1 100M EXT4 boot分区
/dev/sdc2 900M EXT4 LFS系统分区
3. 执行 sudo fdisk -u -l /dev/sdc 命令,查看硬件的分区情况,start和end中数字的单位是512字节
4. 利用dd命令将U盘做成镜像:
dd if=/dev/sdc of=lfs-7.10-linux-4.7.2.img bs=512 count=[fdisk命令中最大的end数+1]