下载源码

wget <https://busybox.net/downloads/busybox-1.37.0.tar.bz2>
tar xf busybox-1.37.0.tar.bz2
cd busybox-1.37.0

注意使用静态库

image.png

非 x86 的的设备没有 sha1 的支持, 所以需要取消如下的选项

image.png

image.png

busybox是根据工具链去编译的, 所以我们只需要提供对应平台的工具链编译即可。这里我使用使用的平台是 armv7h

make CROSS_COMPILE=arm-buildroot-linux-musleabihf- -j$(nproc) install

我们并没有过多配置 busybox , 所以最终的工具集合是基本的工具, 并不会有过多的工具。我们使用 qemu 验证一下是否可用。默认在 _install 文件夹下

# `qemu-arm busybox uname -a` also ok
qemu-arm _install/bin/uname -a

image.png

接下来构建我们的 initrd , 编写一个 init 文件放置在 _install 文件下下, 这里我们使用 mdev 来更新设备列表, 你也可以通过挂载 devtmpfs 来实现。

#!/bin/sh

echo "Loading, please wait..."

[ -d /dev ] || mkdir -m 0755 /dev
[ -d /root ] || mkdir --mode=0700 /root
[ -d /sys ] || mkdir /sys
[ -d /proc ] || mkdir /proc
[ -d /tmp ] || mkdir /tmp
[ -d /mnt ] || mkdir /mnt

# Mount /proc and /sys:
mount -n proc /proc -t proc
mount -n sysfs /sys -t sysfs

# Note that this only becomes /dev on the real filesystem if udev's scripts
# are used; which they will be, but it's worth pointing out
#mount -t tmpfs -o mode=0755 udev /dev
[ -e /dev/console ] || mknod /dev/console c 5 1
[ -e /dev/null ] || mknod /dev/null c 1 3

# this will scan device update /dev list
mdev -s
# /bin/sh -i
# Do your stuff here.
echo "This script just mounts and boots the rootfs, nothing else!"

# Mount the root filesystem.
[ -d /mnt/root ] || mkdir /mnt/root
mount -o rw /dev/vda /mnt/root

# Clean up.
umount /proc
umount /sys
# 在安装 openrc 前我们还是进入 shell
exec switch_root /mnt/root /bin/sh