Buildroot 自动化集成 BusyBox 生成根文件系统
Buildroot 是什么
Buildroot 是一个 Python+Make 写的自动化构建工具,目标是从源码一键生成完整的嵌入式 Linux 系统。你可以把它看作一个"编译所有东西的元构建系统":
- 自动下载 BusyBox、内核、u-boot、应用程序等所有组件的源码
- 自动交叉编译(需要先指定架构和工具链)
- 自动组装根文件系统(cpio/ext4/squashfs 等格式)
- 自动处理依赖关系
BusyBox 在 Buildroot 里是默认且核心的用户态——只要选了 buildroot defconfig,就会自动包含 BusyBox 的 defconfig 版本。
使用 Buildroot 构建 BusyBox
# 1. 获取 Buildroot
git clone https://git.buildroot.net/buildroot
cd buildroot
# 2. 选择一个平台 defconfig(或自己配)
make menuconfig
# System configuration -> Root filesystem overlay directories
# System configuration -> Custom packages to be selected
# 3. 如果想自定义 BusyBox 配置
# 在 Buildroot menuconfig 里:
# Target packages -> System tools -> BusyBox
# 选择 Use custom busybox configuration file
# 指定你的 .config 路径
# 或者让 Buildroot 用 BusyBox 官方 defconfig(默认行为)
# 4. 构建(首次编译需要几分钟)
make -j$(nproc)
# 5. 产物在 output/images/
ls output/images/
# rootfs.cpio.gz — initramfs 格式
# rootfs.ext4 — 根分区镜像
# bzImage — 内核
自定义 BusyBox 配置
Buildroot 提供了几种方式让你干预 BusyBox 的编译配置:
方式一:使用 Buildroot 内置 menuconfig
make busybox-menuconfig # 进入 BusyBox 的 menuconfig
# 在里面开关 Applet,保存后退出
# 然后重新 make
make
这会把修改后的 .config 存在 output/build/busybox-xxx/.config,下次构建会保留。
方式二:提供现成的 .config 文件
# 在 Buildroot menuconfig 里:
# Target packages -> System tools -> BusyBox
# [*] Build BusyBox (requires glibc or musl)
# BusyBox version (Custom version)
# Custom BusyBox version (1.36.1)
# Use custom busybox configuration file
# (/path/to/your/busybox.config)
方式三:post-build 脚本
# 在 Buildroot menuconfig 里指定 post-build script:
# System configuration -> Post build script to run after creating .config
# 脚本可以直接操作 output/build/busybox-xxx/.config
Buildroot 产物结构
构建完成后,output/ 目录结构:
output/
├── build/
│ ├── busybox-1.36.1/ # BusyBox 源码 + 编译产物
│ │ ├── busybox # 编译好的二进制
│ │ ├── .config # 编译配置
│ │ └── _install/ # make install 目标
│ ├── linux-5.15/ # 内核
│ └── ...
├── target/ # 完整根文件系统
│ ├── bin/busybox
│ ├── bin/ls -> busybox
│ ├── bin/cat -> busybox
│ ├── sbin/init -> busybox
│ ├── etc/inittab # Buildroot 生成的默认配置
│ └── ...
├── images/ # 可烧录镜像
│ ├── rootfs.cpio.gz # initramfs
│ ├── rootfs.ext4 # ext4 根分区
│ └── bzImage # 内核
└── staging/ # 交叉编译用的 sysroot
与 OpenWrt 的区别
Buildroot 和 OpenWrt 都会用 BusyBox,但定位不同:
| 维度 | Buildroot | OpenWrt |
|---|---|---|
| 定位 | 通用嵌入式根文件系统生成器 | 专门的路由器发行版 |
| 包管理 | 无(静态构建) | opkg(运行时安装) |
| 配置系统 | Kconfig(和内核一样) | 自己的 UCI |
| BusyBox 定制 | 标准 menuconfig | 增加了路由器专用 Applet(UCI 等) |
| 适合场景 | 工业控制、IoT、自定义设备 | 路由器、网关、无线设备 |
如果你的项目不是路由器,Buildroot 是更好的起点。