重新打包 Firefly-RK3288 rootfs 镜像
2025-03-22 18:54:56

折腾开发板的过程中反复烧录镜像,每次烧录镜像后安装过的软件、配置都丢失了,又得重新搞。
于是想在官方镜像上配置好,重新打包根文件镜像,以后再刷写镜像后就省事多了。

下载镜像

下载页面:https://www.t-firefly.com/doc/download/4.html

选择“Ubuntu 根文件系统(32位)”,我这里用的 Ubuntu16,直链地址:rk3288_ubuntu_16.04_armhf_20200403142221.img.7z

解压后得到文件FIREFLY-RK3288-UBUNTU16.04-GPT-20200326-1507.img,MD5是1B69032F910C8B1DF68ABA46C9393AA7

重新打包

整个过程要在 Linux 系统中操作,最好也在相同的系统中操作,我用的 Ubuntu16 x86_64 的虚拟机。

增加镜像容量

因为官方的镜像已经压缩过了,直接用的话无法更新软件等操作,因为磁盘已满。

1
2
3
4
5
6
7
apt install -y qemu-utils

# 增加镜像文件大小
qemu-img resize rk3288_ubuntu_rootfs.img +500M

# 调整文件系统大小以填充增加的 500 MB 空间
resize2fs -f rk3288_ubuntu_rootfs.img

我这里只+500M,更新软件够用了,如果要装新软件可以适量加大些。

挂载镜像

写一个挂载、卸载用的脚本,方便日后使用,抄网上的,改了一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
#

function mnt() {
echo "MOUNTING"
# 确保挂载目录以 '/' 结尾
local mount_dir=${2%/}/
# 检查挂载目录是否存在,如果不存在则创建
if [ ! -d "$mount_dir" ]; then
sudo mkdir -p $mount_dir
fi
# 检查是否提供了镜像文件路径
if [ -n "$3" ]; then
# 挂载镜像文件到指定目录
sudo mount -o loop $3 $mount_dir
fi
# 挂载其他必要的文件系统
sudo mount -t proc /proc ${mount_dir}proc
sudo mount -t sysfs /sys ${mount_dir}sys
sudo mount -o bind /dev ${mount_dir}dev
sudo mount -o bind /dev/pts ${mount_dir}dev/pts
sudo chroot ${mount_dir}
}

function umnt() {
echo "UNMOUNTING"
# 确保挂载目录以 '/' 结尾
local mount_dir=${2%/}/
# 首先卸载其他挂载的文件系统
sudo umount ${mount_dir}dev/pts
sudo umount ${mount_dir}dev
sudo umount ${mount_dir}sys
sudo umount ${mount_dir}proc
# 检查是否有镜像文件路径参数,如果有,则卸载镜像文件
if [ -n "$3" ]; then
sudo umount $mount_dir
fi
}

if [ "$1" == "-m" ] && [ -n "$2" ] ;
then
mnt $1 $2 $3
elif [ "$1" == "-u" ] && [ -n "$2" ];
then
umnt $1 $2 $3
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"
echo "3'rd parameter (optional) is the path to the image file"
echo ""
echo "For example: ./ch-mount -m /mnt/rootfs/ /path/to/image.img"
echo ""
echo 1st parameter : ${1}
echo 2nd parameter : ${2}
echo 3rd parameter : ${3}
fi

命名为ch-mount.sh。然后挂载镜像:

1
./ch-mount.sh -m /mnt/rootfs/ rk3288_ubuntu_rootfs.img

此时就进入了根文件系统。

替换软件源

1
vim /etc/apt/sources.list

我选择了清华源,替换文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-proposed main restricted universe multiverse
# # deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-proposed main restricted universe multiverse

生成本地化数据

Firefly官方镜像没有安装en_US.UTF-8,更新软件可能会报错。

1
2
locale-gen en_US.UTF-8
locale-gen zh_CN.UTF-8

设置时区

1
dpkg-reconfigure tzdata

这是用 UI 方式设置的。

升级软件

保持软件最新。

1
2
apt update
apt upgrade -y

更新完后可以再安装自己所需的软件。


最后出现一个错误,尝试配置 blueman 包时,因为它尝试与系统消息总线(D-Bus)交互,而在 chroot 环境中,D-Bus 服务通常是不可用的。这是因为 chroot 环境缺少运行完整系统服务的环境,特别是像 D-Bus 这样的系统级服务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ apt upgrade -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up blueman (2.0.4-1ubuntu2.1) ...
Running in chroot, ignoring request.
* Reloading system message bus config... Failed to open connection to "system" message bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
invoke-rc.d: initscript dbus, action "reload" failed.
dpkg: error processing package blueman (--configure):
subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
blueman
E: Sub-process /usr/bin/dpkg returned an error code (1)

不用管它了。

打包

1
2
3
4
5
6
7
8
# 卸载镜像
./ch-mount.sh -u /mnt/rootfs/ rk3288_ubuntu_rootfs.img

# 查找并尝试修复文件系统中的任何错误
e2fsck -f rk3288_ubuntu_rootfs.img

# 将文件系统最小化
resize2fs -M rk3288_ubuntu_rootfs.img