接上篇,当完成了工具链编译之后,我们将准备chroot环境,完成相关设置后,就将开始进行基础系统编译。
1、将丹炉目录目录权限修改为root用户,以root用户执行。
installer$ exit # 从installer用户退出到root用户
# 设置环境变量
export destDir=/mnt/build_dir
export TGT=x86_64-dan-linux-gnu
export sourcesDir=/installer/sources
chown -R root:root ${destDir}/{usr,lib,lib64,var,etc,bin,sbin,tools}
2、准备虚拟内核文件系统
mkdir -pv ${destDir}/{dev,proc,sys,run}
mountpoint -q ${destDir}/dev || mount -v --bind /dev ${destDir}/dev
mountpoint -q ${destDir}/dev/pts || mount -v --bind /dev/pts ${destDir}/dev/pts
mountpoint -q ${destDir}/proc || mount -vt proc proc ${destDir}/proc
mountpoint -q ${destDir}/sys || mount -vt sysfs sysfs ${destDir}/sys
mountpoint -q ${destDir}/run || mount -vt tmpfs tmpfs ${destDir}/run
if [ -h ${destDir}/dev/shm ]; then
mkdir -pv ${destDir}/$(readlink ${destDir}/dev/shm)
else
mountpoint -q ${destDir}/dev/shm || mount -vt tmpfs -o nosuid,nodev tmpfs ${destDir}/dev/shm
fi
3、进入 Chroot 环境
chroot ${destDir} /usr/bin/env -i \
HOME=/root \
TERM="$TERM" \
PS1='(lfs chroot) \u:\w\$ ' \
PATH=/usr/bin:/usr/sbin \
/bin/bash --login
此时如果看到Shell提示符有“I have no name!”字样,这是正常的。此命令执行完毕后,我们就进入了dan的内部,以下命令都是在丹内(Chroot环境)的操作
4、创建丹的目录
mkdir -pv /{boot,home,mnt,opt,srv}
mkdir -pv /etc/{opt,sysconfig}
mkdir -pv /lib/firmware
mkdir -pv /media/{floppy,cdrom}
mkdir -pv /usr/{,local/}{include,src}
mkdir -pv /usr/local/{bin,lib,sbin}
mkdir -pv /usr/{,local/}share/{color,dict,doc,info,locale,man}
mkdir -pv /usr/{,local/}share/{misc,terminfo,zoneinfo}
mkdir -pv /usr/{,local/}share/man/man{1..8}
mkdir -pv /var/{cache,local,log,mail,opt,spool}
mkdir -pv /var/lib/{color,misc,locate}
# LFS文档可能遗漏了这个
mkdir -pv /run/lock
ln -sfv /run /var/run
ln -sfv /run/lock /var/lock
install -dv -m 0750 /root
install -dv -m 1777 /tmp /var/tmp
5、创建文件
ln -sv /proc/self/mounts /etc/mtab
cat > /etc/hosts << eof 127.0.0.1 localhost hostname ::1 localhost eof cat> /etc/passwd << eof root:x:0:0:root:root:binbash bin:x:1:1:bin:devnull:usrbinfalse daemon:x:6:6:daemon user:devnull:usrbinfalse messagebus:x:18:18:d-bus message daemon user:rundbus:usrbinfalse systemd-journal-gateway:x:73:73:systemd journal gateway::usrbinfalse systemd-journal-remote:x:74:74:systemd journal remote::usrbinfalse systemd-journal-upload:x:75:75:systemd journal upload::usrbinfalse systemd-network:x:76:76:systemd network management::usrbinfalse systemd-resolve:x:77:77:systemd resolver::usrbinfalse systemd-timesync:x:78:78:systemd time synchronization::usrbinfalse systemd-coredump:x:79:79:systemd core dumper::usrbinfalse uuidd:x:80:80:uuid generation daemon user:devnull:usrbinfalse systemd-oom:x:81:81:systemd out of memory daemon::usrbinfalse nobody:x:65534:65534:unprivileged user:devnull:usrbinfalse eof cat> /etc/group << "EOF"
root:x:0:
bin:x:1:daemon
sys:x:2:
kmem:x:3:
tape:x:4:
tty:x:5:
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
usb:x:14:
cdrom:x:15:
adm:x:16:
messagebus:x:18:
systemd-journal:x:23:
input:x:24:
mail:x:34:
kvm:x:61:
systemd-journal-gateway:x:73:
systemd-journal-remote:x:74:
systemd-journal-upload:x:75:
systemd-network:x:76:
systemd-resolve:x:77:
systemd-timesync:x:78:
systemd-coredump:x:79:
uuidd:x:80:
systemd-oom:x:81:
wheel:x:97:
users:x:999:
nogroup:x:65534:
EOF
touch /var/log/{btmp,lastlog,faillog,wtmp}
chgrp -v utmp /var/log/lastlog
chmod -v 664 /var/log/lastlog
chmod -v 600 /var/log/btmp
至此,丹内目录和基本文件,以及虚拟设备创建完成。接下来,将在丹内编译linux系统,这将依赖于前面编译好的工具链进行,系统是干净的,几乎完全隔离于宿主机。