BusyBox

一、什么是 BusyBox

二、编译

wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2
tar xf busybox-1.36.1.tar.bz2
cd busybox-1.36.1

make defconfig
make menuconfig

# 设置
# Settings → Build Options → [*] Build as a static binary (no shared libs)
# Cross compiler prefix: arm-linux-gnueabihf-

make -j$(nproc)
make CONFIG_PREFIX=/path/to/rootfs install

三、BusyBox 子命令

# ls busybox --help
busybox
Currently defined functions:
    [, [[, acpid, addgroup, adduser, adjtimex, ar, arp, arping,
    ash, awk, base64, basename, bc, ... (300+)

四、init 系统

BusyBox 提供 init,配置 /etc/inittab

# /etc/inittab
::sysinit:/etc/init.d/rcS
::respawn:/sbin/getty -L ttymxc0 115200 vt100
::ctrlaltdel:/sbin/reboot
::shutdown:/etc/init.d/rcK

字段格式:id:runlevel:action:command
- :: 表示所有
- action:sysinit、respawn、once、wait、ctrlaltdel、shutdown

1. 启动脚本 /etc/init.d/rcS

#!/bin/sh
mount -a
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s

# 启动网络
ifup -a

# 自定义
for f in /etc/init.d/S??*; do
    [ -x "$f" ] && "$f" start
done

2. 关闭脚本 /etc/init.d/rcK

#!/bin/sh
for f in /etc/init.d/S??*; do
    [ -x "$f" ] && "$f" stop
done

3. 单个服务脚本

#!/bin/sh
# /etc/init.d/S50myapp
case "$1" in
    start)
        printf "Starting myapp: "
        /usr/bin/myapp
        echo "OK"
        ;;
    stop)
        printf "Stopping myapp: "
        killall myapp
        echo "OK"
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac

五、mdev

BusyBox 自带的 udev 替代:

# 扫描现有设备并创建节点
mdev -s

# 配合 hotplug
echo /sbin/mdev > /proc/sys/kernel/hotplug

更现代可选 udevd(systemd-udev)或 eudev

六、常用配置(menuconfig)

Settings --->
    [*] Build as a static binary (no shared libs)
    Cross compiler prefix: arm-linux-gnueabihf-

Installation Options --->
    Don't use /usr

Login/Password Management Utilities --->
    [*] adduser, addgroup, ...

七、安全更新

BusyBox 漏洞频发,应定期升级:
- Debian security
- Buildroot 自动跟踪

八、替代方案