1. 准备
- CentOS 7.9 3.10.0-957.el7.x86_64
- VMware Workstation
建议:系统内核<=3.10.0 使用busybox <= 1.33.2版本
2. 安装busybox
# 安装依赖
yum install syslinux xorriso kernel-devel kernel-headers glibc-static ncurses-devel -y# 下载
wget https://busybox.net/downloads/busybox-1.33.2.tar.bz2# 编译安装
tar -xvf busybox-1.33.2.tar.bz2
cd busybox-1.33.2
make defconfig
sed -i 's/# CONFIG_STATIC is not set/CONFIG_STATIC=y/' .config
make -j$(nproc)
make install
3. 构建RootFS
# 创建目录
mkdir -pv /rootfs/{dev,etc,mnt,proc,sys,tmp,root,var}# 创建必要的设备节点
mknod -m 666 /rootfs/dev/console c 5 1
mknod -m 666 /rootfs/dev/null c 1 3
mknod -m 666 /rootfs/dev/tty c 4 0
mknod -m 666 /rootfs/dev/tty1 c 4 1# 复制 BusyBox 到 rootfs
cp -a busybox-1.33.2/_install/* /rootfs/
cp -a busybox-1.33.2/examples/bootfloppy/etc /rootfs/
# 删除linuxrc
rm -rf /rootfs/linuxrc# 更新fstab文件
cat > /rootfs/etc/fstab <<EOF
# /etc/fstab - Static file system information# Device Mount point Type Options Dump FSCK order
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
mdev /dev tmpfs defaults 0 0
EOF# 更新inittab文件
cat > /rootfs/etc/inittab << EOF
::sysinit:/etc/init.d/rcS
console::askfirst:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a
EOF
# 增加执行权限
chmod +x /rootfs/etc/inittab# 更新rcS文件
cat > /rootfs/etc/init.d/rcS <<'EOF'
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
export PATH LD_LIBRARY_PATH/bin/mount -a
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
echo
echo "****************BusyBox******************"
echo " Welcome to Minimal Linux"
echo "*****************************************"
EOF
chmod +x /rootfs/etc/init.d/rcS# 创建init文件
cat > /rootfs/init <<EOF
#!/bin/sh
#
dmesg -n 1
mount -t sysfs sysfs /sys
mount -t proc proc /proc
mdev -sif [ -f /sbin/init ]; thenexec /sbin/init
fiexec /bin/sh
EOF
# 增加执行权限
chmod +x /rootfs/init# 更新profile
cat > /rootfs/etc/profile <<'EOF'
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
export PATH LD_LIBRARY_PATH
EOF
4. 构建ISO镜像
# 创建iso目录
mkdir -p /iso/{boot,isolinux}# 创建initramfs文件
cd /rootfs && find . | cpio -o -H newc | gzip -9 > /iso/boot/initrd.img
# 验证
lsinitrd /iso/boot/initrd.img# 拷贝内核
cp /boot/vmlinuz-$(uname -r) /iso/boot/vmlinuz
# 拷贝isolinux.bin
cp /usr/share/syslinux/isolinux.bin /iso/isolinux/# 创建isolinux.cfg
cat > /iso/isolinux/isolinux.cfg <<EOF
DEFAULT linux
PROMPT 0
TIMEOUT 50LABEL linuxMENU LABEL Boot LinuxKERNEL /boot/vmlinuzAPPEND initrd=/boot/initrd.img
EOF# 构建iso
xorriso \
-as mkisofs \
-o busybox.iso \
-b isolinux/isolinux.bin \
-c isolinux/boot.cat \
-no-emul-boot -boot-load-size 4 -boot-info-table -R -J /iso
5. 测试
使用 VMware Workstation 创建虚拟机,选择iso镜像,启动虚拟机,进入Linux系统