目录

前言

一、根文件系统是什么?

二、构建根文件系统的方法

1.debootstrap

2.builroot

3.busybox

4.ubuntu-base

三、进入主题——如何使用ubuntu-base构建根文件系统

1.在X86电脑上下载ubuntu-base

2.安装qemu-user-static

3.挂载ubuntu-base

4.打包成镜像

 5.将根文件系统烧录进开发板

未完结……







前言

最近流行国产化,原来使用树莓派做的项目改为使用RK系列开发板,但是购买回来的RK系列开发板系统不好用,因此想自己动手构建一个根文件系统。






一、根文件系统是什么?

根文件系统首先是内核启动时所mount的第一个文件系统,内核代码映像文件保存在根文件系统中,而系统引导启动程序会在根文件系统挂载之后从中把一些基本的初始化脚本和服务等加载到内存中去运行。根文件系统_百度百科 (baidu.com)https://baike.baidu.com/item/%E6%A0%B9%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F

引用百度百科。





二、构建根文件系统的方法





1.debootstrap

参考:

zh_CN/Debootstrap - Debian Wikihttps://wiki.debian.org/zh_CN/Debootstrap debian下为arm开发板创建基于debian或emdebian的根文件系统 - qiaoqiao2003 - 博客园 (cnblogs.com)https://www.cnblogs.com/qiaoqiao2003/p/3738552.html

这个方法尝试过,但是烧录进开发板之后提示启动不了 大概是:Failed to execute /bin/sh 具体报错忘记了,然后看了下该文件的类型居然是x86类型的,怪不得运行不了!估计是忘记了debootstrap --second-stage这个步骤,后面有空再来填坑,欢迎指正。





2.builroot

可以用RK官方的buildroot仓库构建镜像,方法简单,但出现奇怪的报错。

参考:

https://github.com/rockchip-linux/rk-rootfs-buildhttps://github.com/rockchip-linux/rk-rootfs-buildrockchip-linux/buildroot (github.com)https://github.com/rockchip-linux/buildroot在使用该方法时出现:configure: error: *** The available kernel headers are older than the requested。无解,遂放弃。

3.busybox

BusyBox 将许多具有共性的小版本的UNIX工具结合到一个单一的可执行文件。这样的集合可以替代大部分常用工具比如的GNU fileutils , shellutils等工具,BusyBox提供了一个比较完善的环境,可以适用于任何小的嵌入式系统。

可参考:[Linux]用Busybox做一个ROOTFS(根文件系统)_wangyijieonline的博客-CSDN博客https://blog.csdn.net/wangyijieonline/article/details/103181899

4.ubuntu-base

ubuntu-base是什么:Base - Ubuntu Wikihttps://wiki.ubuntu.com/Base

简单来说ubuntu-base就是一个超级精简版系统,然后你可以在里面用apt来安装你想要的东西。

三、进入主题——如何使用ubuntu-base构建根文件系统

1.在X86电脑上下载ubuntu-base

比如我想用20.04的ubuntu-base构建就去Ubuntu Base 20.04.3 LTS (Focal Fossa)这里下载,下载在的是64位的arm架构系统自然就是

ubuntu-base-20.04.1-base-arm64.tar.gz

下载完成后解压到某一目录下,这里是ubuntu_rootfs

mkdir ubuntu_rootfs
sudo tar -xpf ubuntu-base-20.04.1-base-arm64.tar.gz -C ubuntu_rootfs/

-x:解压文件
-p:使用原来文件相同的权限来解压文件
-f:指定解压文件
-C:解压到

2.安装qemu-user-static

qemu-user-static是一个用于利用当前操作系统来运行其它架构的一个仿真器,使用它可以在X86架构上进入上面下载的arm64 ubuntu-base系统。

sudo apt install qemu-user-static

 由于下载的ubuntu-base是aarch64架构的,因此需要拷贝qemu-aarch64-static到ubuntu_rootfs/usr/bin/下。

sudo cp /usr/bin/qemu-aarch64-static ubuntu_rootfs/usr/bin

拷贝本机的dns配置文件到根文件系统

sudo cp /etc/resolv.conf ubuntu_rootfs/etc/resolv.conf

3.挂载ubuntu-base

编写挂载脚本

#!/bin/bash
mnt() {
	echo "MOUNTING"
	sudo mount -t proc /proc ${2}proc
	sudo mount -t sysfs /sys ${2}sys
	sudo mount -o bind /dev ${2}dev
	sudo mount -o bind /dev/pts ${2}dev/pts
	sudo chroot ${2}
}
umnt() {
	echo "UNMOUNTING"
	sudo umount ${2}proc
	sudo umount ${2}sys
	sudo umount ${2}dev/pts
	sudo umount ${2}dev
}

if [ "$1" == "-m" ] && [ -n "$2" ] ;
then
	mnt $1 $2
elif [ "$1" == "-u" ] && [ -n "$2" ];
then
	umnt $1 $2
else
	echo ""
	echo "Either 1'st, 2'nd or both parameters were missing"
	echo ""
	echo "1'st parameter can be one of these: -m(mount) OR -u(umount)"
	echo "2'nd parameter is the full path of rootfs directory(with trailing '/')"
	echo ""
	echo "For example: ch-mount -m /media/sdcard/"
	echo ""
	echo 1st parameter : ${1}
	echo 2nd parameter : ${2}
fi

 增加脚本执行权限

sudo chmod +x mount.sh

 运行脚本挂载根文件系统

bash mount.sh -m ubuntu_rootfs/

 卸载则运行

bash mount.sh -u ubuntu_rootfs/

 挂载后进入系统安装软件,完成后输入exit退出,再用脚本卸载

4.打包成镜像

先创建一个空镜像文件,大小为4096MB

dd if=/dev/zero of=ubuntu_rootfs.img bs=1M count=4096

将该文件格式化成ext4文件系统

mkfs.ext4 ubuntu_rootfs.img

将该镜像文件挂载到一个空的文件夹上,然后将ubuntu_rootfs的文件复制到该空文件夹中

mkdir ubuntu_base_rootfs
sudo mount ubuntu_rootfs.img ubuntu_base_rootfs
sudo cp -rfp ubuntu_rootfs/* ubuntu_base_rootfs/

 复制完后用e2fsck修复及检测镜像文件系统,resize2fs 减小镜像文件的大小

$ sudo umount ubuntu_base_rootfs/
$ e2fsck -p -f ubuntu_rootfs.img
$ resize2fs -M ubuntu_rootfs.img

 5.将根文件系统烧录进开发板




四.进入系统后问题问题杂锦

1.无法获取到ip

进入到开发板后发现无法获取到ip,用ipconfig -a查看可以看到有eth0网卡,原来需要修改/etc/network/interfaces文件

sudo vim /etc/network/interfaces
rk3568@localhost:~$ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
source-directory /etc/network/interfaces.d

修改完后重启就OK。 

2.禁用系统自动休眠

 过一段时间就进不去系统,原来是休眠了。

解决办法参考:(29条消息) ubuntu 20.04 | 关闭自动休眠_m0_52650517的博客-CSDN博客icon-default.png?t=LA92https://blog.csdn.net/m0_52650517/article/details/119811966

 查看:

sudo systemctl status sleep.target

禁用:

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

未完结……

 参考文献:

(29条消息) 基于ubuntu-base进行根文件系统的修改与打包_里先森-CSDN博客https://blog.csdn.net/sements/article/details/105240490【原创】从Ubuntu-base构建ubuntu rootfs系统(以x86_64和arm为例) - 沐多 - 博客园 (cnblogs.com)https://www.cnblogs.com/wsg1100/p/13127636.html(29条消息) 从Ubuntu-base构建ubuntu rootfs(以x86_64和arm为例)_wsg1100-CSDN博客https://blog.csdn.net/qq_22654551/article/details/106753765

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐