编译BusyBox制作文系统

本文介绍如何使用BusyBox 1.1.3在Ubuntu 8.04环境下为ARM架构设备构建精简的文件系统。从BusyBox配置到编译安装,再到根文件系统的搭建,包括关键文件和目录的创建过程。

【原】编译BusyBox制作文系统

一、环境
Ubuntu 8.04
arm-linux-gcc 3.4.5
busybox-1.1.3

二、busybox制作文件系统
  1、下载busybox1.1.3(http://www.busybox.net/)并解压。
  2、进入解压后的目录,配置Busybox

  #make menuconfig

  Busybox Settings >

  General Configuration >
  [*] Support for devfs

  Init Utilities >
  [*] init
  [*] Support reading an inittab file /* 支持init读取/etc/inittab配置文件,一定要选上 */
[*] Be _extra_ quiet on boot
[*] Support running init from within an initrd (not initramfs)
[*] poweroff, halt, and reboot
[*] mesg

  Shells >

  Choose your default shell (ash) >
  /* (X) ash 选中ash,这样生成的时候才会生成bin/sh文件
  * 看看我们前头的linuxrc脚本的头一句:
  * #!/bin/sh 是由bin/sh来解释执行的
  */ (我开始就因为在这里没选上,结果就造成启动不起来 )
  [*] ash

添加历史记录、自动补全、删除字符的功能:
Shells --->
--- Bourne Shell Options
[ ] Hide message on interactive shell startup
[ ] Standalone shell
[*] command line editing
[*] vi-style line editing commands
   (15) history size
[*] history saving
[*] tab completion
[*] username completion
[ ] Fancy shell prompts

  Coreutils >
  [*] cp
  [*] cat
  [*] ls
  [*] mkdir
  [*] echo (basic SuSv3 version taking no options)
  [*] env
  [*] mv
  [*] pwd
  [*] rm
  [*] touch

  Editors >
  [*] vi

  Linux System Utilities >
  [*] mount
  [*] umount
  [*] Support loopback mounts
  [*] Support for the old /etc/mtab file

Linux Module Utilities --->
[*] insmod
[*] rmmod
[*] lsmod
[*] lsmod pretty output for 2.6.x Linux kernels
[*] modprobe
[*] Multiple options parsing
--- Options common to multiple modutils
[*] Support tainted module checking with new kernels
[ ] Support version 2.2.x to 2.4.x Linux kernels //此项一定不要选!!!
[*] Support version 2.6.x Linux kernels

Networking Utilities >
[*] ifconfig
[*] ping

Archival Utilities --->
[*] tar
[*] Enable archive creation (NEW)
[*] Enable -j option to handle .tar.bz2 files


注:可根据需要自已选择命令。

3、如果文件系统在2410上运行的话,不需要修改源码,如果在2440上运行,需要将busybox-1.1.3/init/init.c中的
两处
close(0);
close(1);
close(2);
注释掉:
/* Clean up */
//close(0);
//close(1);
//close(2);

/* Close whatever files are open, and reset the console. */
//close(0);
//close(1);
//close(2);
   4、编译并安装Busybox (1.1.3的Makefile里没有设置ARCH和CROSS内容,需要亲自在指令里写上,我是这么做的)

#export PATH=$PATH:usr/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/bin
#make TARGET_ARCH=arm CROSS="arm-linux-"
#make install

5、建立根文件系统结构
#cd ~
#mkdir rootfs
#cd rootfs
#mkdir bin dev etc lib proc sbin tmp usr var mnt
#chmod 1777 tmp
#mkdir usr/bin usr/lib usr/sbin
#mkdir var/lib var/lock var/log var/run var/tmp
#mkdir etc/init.d
#chmod 1777 var/tmp
把 busybox1.1.3/_install/目录下的所有文件copy过来覆盖rootfs文件夹里的空文件夹。

lib里面还要拷入一些库文件,下面仅给出一些常用的库
#cd rootfs/lib
#cp -rfd /usr/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/arm-linux/lib/ld* ./
#cp -rfd /usr/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/arm-linux/lib/libc-2.3.6.so ./
#cp -rfd /usr/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/arm-linux/lib/libc.so.6 ./
#cp -rfd /usr/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/arm-linux/lib/libm* ./
#cp -rfd /usr/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/arm-linux/lib/libcrypt* ./
当然为了方便,可以将交叉编译的库全放进去,但是这样会增加文件系统的体积。
#cp -rfd /usr/crosstool/gcc-3.4.5-glibc-2.3.6/arm-linux/arm-linux/lib/* ./ (注意-d,保持库文件的链接关系)

6、创建etc/init.d/rcS
#!/bin/sh
echo "running /etc/init.d/rcS"
echo "mount the /proc file system"
/bin/mount -t proc proc /proc

echo "mount tmpfs filesystem to /tmp"
/bin/mount -t tmpfs none /tmp

echo "mount ramfs filesystem to /var"
/bin/mount -t ramfs none /var

然后修改权限:chmod 775 rcS

7、创建etc/inittab
# This is run first except when booting
console::sysinit:/etc/init.d/rcS
# Start an "askfirst" shell on the console
#::askfirst:-/bin/bash
console::askfirst:-/bin/sh
# Stuff to do when restarting the init process
::restart:/sbin/init
# Stuff to do before rebooting
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
注:需要将内核源码drivers/char/tty_io.c中所有的
noctty = 1改为noctty = 0

8、再创建etc/fstab
none /proc proc defaults0 0
none /dev/pts devpts mode="0622" 0 0
tmpfs /dev/shm tmpfs defaults 0 0

至此,根文件系统制作完毕。

<think>我们使用中回答问题。 问题:如何使用busybox编译一个Linux内核? 回答:编译Linux内核并使用BusyBox作为根系统是一个多步骤的过程。以下是简要步骤: 1. **准备环境**:安装必要的编译工具,如gcc, make, ncurses-dev(用于menuconfig)等。 2. **下载内核源码**:从kernel.org下载所需版本的Linux内核源码,并解压。 3. **配置内核**:进入内核源码目录,使用`make defconfig`(默认配置)或`make menuconfig`(图形菜单配置)进行配置。在配置中,确保启用以下关键选项: - 在`General setup` -> `Initramfs/initrd support` 中启用。 - 在`File systems`中,启用`devtmpfs`和`ext2`(或你计划使用的系统)支持。 - 确保内核支持你的硬件架构(如x86, ARM等)。 4. **编译内核**:使用`make`命令编译内核。可以使用`-j`参数指定并行编译的线程数(例如`make -j4`)。编译完成后,内核镜像件(如bzImage)将生成在`arch/x86/boot/`(x86架构)或相应架构的目录中。 5. **构建BusyBox系统**: - 下载BusyBox源码并解压。 - 进入BusyBox目录,运行`make menuconfig`,在`Settings`中启用`Build static binary (no shared libs)`(这样可以避免依赖动态库)。 - 编译并安装:`make && make install`。这将在`_install`目录下生成基本的系统结构。 - 在`_install`目录下创建必要的目录:`mkdir proc sys dev etc etc/init.d`。 - 创建初始化脚本:在`etc/init.d/rcS`中写入以下内容(并赋予执行权限): ```sh #!/bin/sh mount -t proc none /proc mount -t sysfs none /sys mount -t devtmpfs devtmpfs /dev echo "Welcome to BusyBox Linux!" /bin/sh ``` - 创建`init`的软链接:`cd _install; ln -s bin/busybox init`。 6. **制作initramfs**: - 将BusyBox的`_install`目录打包成cpio格式(用于initramfs): ```sh cd _install find . | cpio -o -H newc | gzip > ../initramfs.cpio.gz ``` - 将生成的`initramfs.cpio.gz`复制到内核源码目录下。 7. **重新配置内核以使用initramfs**:在内核配置中(`make menuconfig`),指定initramfs件路径: - 进入`General setup` -> `Initramfs source file(s)`,输入`initramfs.cpio.gz`的路径(或使用绝对路径)。 - 或者在内核命令行参数中指定`initrd=initramfs.cpio.gz`,但通常直接编译进内核更方便。 8. **重新编译内核**:再次运行`make`命令编译内核,这样内核就会包含initramfs。 9. **测试**:使用QEMU等模拟器测试内核,例如(x86架构): ```sh qemu-system-x86_64 -kernel arch/x86/boot/bzImage -initrd initramfs.cpio.gz -append "console=ttyS0" ``` 注意:以上步骤是一个简化的流程,实际中可能需要根据具体需求调整配置。 §§相关问题§§ 1. 如何为ARM架构编译Linux内核? 2. 在配置内核时,哪些选项是使用BusyBox作为initramfs所必需的? 3. 如何创建一个动态链接的BusyBox系统,而不是静态链接? 4. 在QEMU中测试内核时,如何挂载一个真实的系统而不是使用initramfs? 5. 如何在内核编译过程中集成额外的驱动模块?</think>### 如何使用BusyBox编译Linux内核 以下是详细步骤(以x86架构为例): 1. **环境准备** ```bash sudo apt update sudo apt install build-essential libncurses-dev flex bison libssl-dev bc qemu-system-x86 ``` 2. **下载内核源码** ```bash wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.8.9.tar.xz tar -xvf linux-6.8.9.tar.xz cd linux-6.8.9 ``` 3. **配置内核** ```bash make defconfig # 使用默认配置 make menuconfig # 图形化配置(关键设置↓) ``` - **必选配置**: `General setup` → `Initramfs source file` → 留空(后续添加) `File systems` → 启用 `devtmpfs`、`ext2`、`squashfs` `Executable file formats` → 启用 `Kernel support for ELF binaries` 4. **编译内核** ```bash make -j$(nproc) # 多线程编译 # 生成件:arch/x86/boot/bzImage ``` 5. **构建BusyBox系统** ```bash wget https://busybox.net/downloads/busybox-1.37.0.tar.bz2 tar -xvf busybox-1.37.0.tar.bz2 cd busybox-1.37.0 make menuconfig ``` - **配置选项**: `Settings` → `Build static binary (no shared libs)` → 选 **是** `Installation Options` → `Destination path for 'make install'` → 设为 `_install` ```bash make -j$(nproc) && make install cd _install mkdir -p proc sys dev etc/init.d ``` 6. **创建初始化脚本** ```bash cat > etc/init.d/rcS <<EOF #!/bin/sh mount -t proc none /proc mount -t sysfs none /sys /sbin/mdev -s EOF chmod +x etc/init.d/rcS ln -s bin/busybox init # 关键软链接 ``` 7. **生成initramfs** ```bash find . | cpio -H newc -o | gzip > ../initramfs.cpio.gz cp ../initramfs.cpio.gz ~/linux-6.8.9/ ``` 8. **启动测试** ```bash cd ~/linux-6.8.9 qemu-system-x86_64 -kernel arch/x86/boot/bzImage -initrd initramfs.cpio.gz -append "console=ttyS0" -nographic ``` - 成功启动后会出现BusyBox的shell提示符:`/ # ` > **关键注意事项** > - BusyBox编译为**静态二进制**(避免动态库依赖) > - 必须创建 `init` 软链接到 `/bin/busybox` > - QEMU测试时使用 `-nographic` 可避免GUI依赖
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值