【Linux】基于busybox移植rootfs根文件系统

【Linux】基于busybox移植rootfs根文件系统,第1张

【Linux】基于busybox移植rootfs根文件系统

文章目录

1. 前言2. 下载Busybox3. 编译Busybox4. 向rootfs根文件系统添加lib库5. 根文件系统初步测试6. 完善rootfs根文件系统7. 根文件系统最终测试8. 移植过程错误汇总

1. 前言

我们所熟悉的Linux主要由部分组成:ubootLinux Kernelrootfs(根文件系统)。uboot启动后会加载Linux Kernel,然后Kernel再来挂载rootfs文件系统,进入文件系统后,我们才可以运行我们的应用程序,对不同的设备进行 *** 作。本篇文章主要是介绍通过busybox如何移植制作rootfs最小文件系统

2. 下载Busybox

Busybox官网下载地址:https://busybox.net/

选择最新的固件:busybox-1.35.0.tar.bz2

3. 编译Busybox

将下载好的Busybox发送到ubuntu中,并且解压如下:
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,不做任何修改保存并退出。
编译busybox
make -j4
make install CONFIG_PREFIX=/home/benjamin/nfs_rootfs
COFIG_PREFIX 指定编译结果的存放目录,我这里是存放在/home/benjamin/nfs_rootfs。
编译完成截图如下:

成功安装到指定的路径如下:
4. 向rootfs根文件系统添加lib库

Linux在运行应用程序时需要一些lib库,复制工具链内的lib库文件。

在nfs_rootfs文件夹下创建lib文件夹,并且拷贝如下:(-d表示:如果原来是链接文件,仍保留链接关系)
cp -d /opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc/lib/*.so* ./lib
在nfs_rootfs文件夹下创建usr/lib文件夹,并且拷贝如下:(-d表示:如果原来是链接文件,仍保留链接关系)
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
5. 根文件系统初步测试

为了方便测试前面创建好的根文件系统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已经可以工作,但是还时缺省其它文件,需要我们进一步完善。

6. 完善rootfs根文件系统

创建/etc/init.d/rcS 文件
rcS 是个 shell 脚本,Linux 内核启动以后需要启动一些服务,而 rcS 就是规定启动哪些文件的脚本文件。在 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 文件
在 rootfs中创建/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 的详细内容可以参考 busybox 下的文件 examples/inittab。init 程序会读取/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 这个脚本文件。
第 3 行:将 console 作为控制台终端,也就是 ttymxc0。
第 4 行:重启的话运行/sbin/init。
第 5 行:按下 ctrl+alt+del 组合键的话就运行/sbin/reboot,看来 ctrl+alt+del 组合键用于重启系统。
第 6 行:关机的时候执行/bin/umount,也就是卸载各个文件系统。
第 7 行:关机的时候执行/sbin/swapoff,也就是关闭交换分区。 7. 根文件系统最终测试

经过以上的完善之后,重新通过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
/ # 

8. 移植过程错误汇总

问题一

[    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])


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5714549.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存