我们所熟悉的Linux主要由三部分组成:uboot、Linux Kernel、rootfs(根文件系统)。uboot启动后会加载Linux Kernel,然后Kernel再来挂载rootfs文件系统,进入文件系统后,我们才可以运行我们的应用程序,对不同的设备进行操作。本篇文章主要是介绍通过busybox如何移植制作rootfs最小文件系统。
Busybox官网下载地址:https://busybox.net/
选择最新的固件:busybox-1.35.0.tar.bz2
tar -jxvf busybox-1.35.0.tar.bz2
ARCH
和CROSS_COMPILE
:(NOTE:需要根据硬件平台使用的交叉工具链来配置)export ARCH=arm
export CROSS_COMPILE=arm-none-linux-gnueabihf-
export PATH=$PATH:/opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/bin
make menuconfig
,不做任何修改保存并退出。make -j4
make install CONFIG_PREFIX=/home/benjamin/nfs_rootfs
/home/benjamin/nfs_rootfs
。Linux在运行应用程序时需要一些lib库,复制工具链内的lib库文件。
cp -d /opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/lib/*.so* ./lib
cp -d /opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/lib/*so* ./usr/lib/
cp -d /opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/usr/lib/*.a ./usr/lib/
mkdir dev proc mnt sys tmp root
为了方便测试前面创建好的根文件系统rootfs,测试的方法就是使用NFS挂载。在uboot中我们需要设置bootargs和bootcmd,具体参数如下:
setenv bootargs 'console=ttymxc0,115200 root=/dev/nfs ip=dhcp nfsroot=192.168.31.218:/home/benjamin/nfs_rootfs,v3,tcp'
setenv bootcmd 'tftp 0x80800000 zImage; tftp 0x83000000 imx6ull-benjamin-emmc.dtb; bootz 0x80800000 - 0x83000000'
测试结果如下:
[ 3.003433] SMSC LAN8710/LAN8720 20b4000.ethernet-1:01: attached PHY driver [SMSC LAN8710/LAN8720] (mii_bus:phy_addr=20b4000.ethernet-1:01, irq=POLL)
[ 3.025336] fec 2188000.ethernet eth1: Unable to connect to phy
[ 3.033300] IP-Config: Failed to open eth1
[ 5.123679] fec 20b4000.ethernet eth0: Link is Up - 100Mbps/Full - flow control rx/tx
[ 5.132947] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[ 5.163149] Sending DHCP requests ., OK
[ 5.207002] IP-Config: Got DHCP answer from 192.168.31.1, my address is 192.168.31.178
[ 5.215583] IP-Config: Complete:
[ 5.219184] device=eth0, hwaddr=00:01:3f:2d:3e:4d, ipaddr=192.168.31.178, mask=255.255.255.0, gw=192.168.31.1
[ 5.229596] host=MiWiFi-R4A-srv, domain=, nis-domain=(none)
[ 5.235824] bootserver=192.168.31.1, rootserver=192.168.31.218, rootpath=
[ 5.235841] nameserver0=192.168.31.1
[ 5.247709] cfg80211: Loading compiled-in X.509 certificates for regulatory database
[ 5.259240] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7'
[ 5.266261] platform regulatory.0: Direct firmware load for regulatory.db failed with error -2
[ 5.275278] ALSA device list:
[ 5.278270] #0: wm8960-audio
[ 5.282962] platform regulatory.0: Falling back to sysfs fallback for: regulatory.db
[ 5.393641] VFS: Mounted root (nfs filesystem) readonly on device 0:16.
[ 5.403468] devtmpfs: mounted
[ 5.410154] Freeing unused kernel memory: 1024K
[ 5.433119] Run /sbin/init as init process
can't run '/etc/init.d/rcS': No such file or directory
Please press Enter to activate this console.
/ #
/ # ls
bin lib mnt root sys usr
dev linuxrc proc sbin tmp
/ #
由上面的打印log可以看出ls
命令工作正常!说明rootfs根文件系统基本工作正常,但是进入根文件系统时提示了一个错误:
can't run '/etc/init.d/rcS': No such file or directory
虽然到这里rootfs已经可以工作,但是还时缺省其它文件,需要我们进一步完善。
/etc/init.d/rcS
文件,然后在 rcS 中输入如下所示内容:(NOTE:一定要给执行权限,添加执行权限命令如:chmod 777 /etc/init.d/rcS
)#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
export PATH LD_LIBRARY_PATH
mount -a
mkdir /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
/etc/fstab
文件,fstab在Linux开机以后自动配置哪些需要自动挂载的分区,具体输入如下:#
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
/etc/inittab
这个文件,inittab 由若干条指令组成。/etc/inittab
文件具体输入如下:# /etc/inittab
::sysinit:/etc/init.d/rcS
console::askfirst:-/bin/sh
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a
第 2 行:系统启动以后运行/etc/init.d/rcS 这个脚本文件。经过以上的完善之后,重新通过NFS挂载文件系统,没有任何错误,具体Log如下:
[ 5.044138] fec 20b4000.ethernet eth0: Link is Up - 100Mbps/Full - flow control rx/tx
[ 5.073836] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[ 5.103376] Sending DHCP requests .., OK
[ 8.227482] IP-Config: Got DHCP answer from 192.168.31.1, my address is 192.168.31.178
[ 8.236830] IP-Config: Complete:
[ 8.240100] device=eth0, hwaddr=00:01:3f:2d:3e:4d, ipaddr=192.168.31.178, mask=255.255.255.0, gw=192.168.31.1
[ 8.250570] host=MiWiFi-R4A-srv, domain=, nis-domain=(none)
[ 8.256886] bootserver=192.168.31.1, rootserver=192.168.31.218, rootpath=
[ 8.256903] nameserver0=192.168.31.1
[ 8.268861] cfg80211: Loading compiled-in X.509 certificates for regulatory database
[ 8.280355] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7'
[ 8.287374] platform regulatory.0: Direct firmware load for regulatory.db failed with error -2
[ 8.296389] ALSA device list:
[ 8.299380] #0: wm8960-audio
[ 8.303418] platform regulatory.0: Falling back to sysfs fallback for: regulatory.db
[ 8.403914] VFS: Mounted root (nfs filesystem) readonly on device 0:16.
[ 8.413576] devtmpfs: mounted
[ 8.419314] Freeing unused kernel memory: 1024K
[ 8.443757] Run /sbin/init as init process
Please press Enter to activate this console.
/ #
/ #
/ #
/ # ls
bin etc linuxrc proc sbin tmp
dev lib mnt root sys usr
/ #
问题一
[ 6.173526] Run /sbin/init as init process
[ 6.412362] request_module: kmod_concurrent_max (0) close to 0 (max_modprobes: 50), for module binfmt-464c, throttling...
[ 11.443066] request_module: modprobe binfmt-464c cannot be processed, kmod busy with 50 threads for more than 5 seconds now
[ 11.480035] Starting init: /sbin/init exists but couldn't execute it (error -8)
问题分析:
Kernel在启动/sbin/init时出现的错误,查看执行文件,它是从/bin/busybox软链接过来。
查看发现/bin/busybox文件是64bit x86-64架构的,所以可以知道应该移植busybox时没有修改交叉编译工具。
解决办法:
编译前先指定架构和编译工具:
再次查看/bin/busybox文件:
问题二
/etc/init.d/rcS: line 12: can't create /proc/sys/kernel/hotplug: nonexistent directory
问题分析:
根据打印Log可以知道,Linux Kernel还没有hotplug功能,需要在Kernel里面进行添加。
解决办法:
配置CONFIG_UEVENT_HELPER=y
,位置如下:
│ Location:
│ -> Device Drivers
│ -> Generic Driver Options
│ (2) -> Support for uevent helper (UEVENT_HELPER [=y])