如何配置只读属性的文件系统(Colibri iMX7为例)

by toradex胡珊逢
在先前的文章我们已经介绍在使用 emmc 的模块上配置只读属性的文件系统,以及利用 squashfs和overlayfs 挂载可写分区。toradex 的产品除了使用 emmc 存储外,还有部分是采用 nand flash,例如 colibri imx7 和 colibri imx6ull。下面将以 colibri imx7 为例介绍如何配置只读属性的文件系统。
由于存储介质不同,nand flash 上通常采用如 jffs2、ubi 等格式文件系统。toradex 的 linux 系统使用 ubi 文件系统。
在 colibri imx7 的 nand flash 上会采用以下规划。nand flash 总体上划分为两个部分。最前面的 raw 部分不采用任何文件系统,直接存储模块硬件信息 bcb,u-boot 和 u-boot 环境变量。第二部分则使用 ubi,创建 5 个 volume,用于存储内核文件(kernel)、设备树文件(dtb)、m4 的固件(m4-fw)、linux 文件系统(rootfs)、用户文件(userdata)。其中 rootfs 将设置成自读属性,而 userdata 则可以写入数据。
toradex easy installer 可以通过 image.json 文件方便地修改分区,从而避免使用命令工具。首先从这里下载用于 colibri imx7s 的 linux bsp v5.x 安装文件。解压后在 image.json 中添加 userdata 的相关配置。
-----------------------------------
{
name: rootfs,
content: {
filesystem_type: ubifs,
filename: reference-minimal-image-colibri-imx7.tar.xz,
uncompressed_size: 108.1171875
},
size_kib: 102400
},
{
name: userdata,
content: {
filesystem_type: ubifs,
filename: app.tar.xz,
uncompressed_size: 0.1171875
}
}
-----------------------------------
这里 name 指定 ubi volume 的名字,filesystem_type 用于指定 ubifs 文件格式,filename 里包含了需要烧录到 userdata volume 的文件,这些是用户应用和配置等,uncompressed_size 是指 app.tar.xz 未压缩的大小,用于显示 toradex easy installer 的安装进度条。更多关于 image.json 配置说明请参考这里。
使用 toradex easy installer 将上面修改的镜像烧录到 colibri imx7s 即可。启动后进入 u-boot,使用下面名可以看到所创建的 volume。
-----------------------------------
colibri imx7 # ubi part ubi
colibri imx7 # ubi info  layout
volume information dump:
vol_id          0
......
name            kernel
volume information dump:
vol_id          1
......
skip_check      0
name            dtb
volume information dump:
vol_id          2
......
skip_check      0
name            m4firmware
volume information dump:
vol_id          3
......
skip_check      0
name            rootfs
volume information dump:
vol_id          4
......
skip_check      0
name            userdata
-----------------------------------
启动进入 linux 后,userdata 并不会被自动挂载,需要将下面内容添加到 /etc/fstab 文件中。现在 rootfs 根目录还没有设置成只读属性,可以创建 /home/root/userdata 目录用于挂载 userdata 卷。
-----------------------------------
ubi:userdata         /home/root/userdata  ubifs      defaults,noatime,rw   1  1
-----------------------------------
于此同时,还可以进行系统配置。例如添加一个开机启动应用。该应用 write_to_file 在运行时会往 /home/root/userdata 写入一个文件。在 /etc/systemd/system/ 目录下创建 user-demo.service。
user-demo.service
-----------------------------------
[unit]
description=launch user's demo on dedicated partition
conditionfileisexecutable=/home/root/userdata/write_to_file
after=multi-user.target
[service]
workingdirectory=/home/root/userdata
type=simple
execstart=/home/root/userdata/write_to_file
[install]
wantedby=multi-user.target
-----------------------------------
运行下面命令使 user-demo.service 开机运行。然后重启系统。
-----------------------------------
~# systemctl enable user-demo.service
~# reboot
-----------------------------------
此时,使用 mount 命令查看所挂载的卷,其中有 ubi:userdata。
-----------------------------------
~# mount -l
tmpfs on /var/volatile type tmpfs (rw,relatime)
ubi:userdata on /home/root/userdata type ubifs (rw,noatime,assert=read-only,ubi=0,vol=4)
-----------------------------------
在 /home/root/userdata 目录下也可以看到 write_to_file 写入的文件 file.txt。
-----------------------------------
~/userdata# ls
file.txt       write_to_file
~/userdata# cat file.txt
this is a writing file test
~/userdata# systemctl status user-demo.service
* user-demo.service - launch user's demo on dedicated partition
loaded: loaded (/etc/systemd/system/user-demo.service; enabled; vendor preset: disabled)
active: inactive (dead) since wed 2022-07-06 06:09:44 utc; 14min ago
process: 316 execstart=/home/root/userdata/write_to_file (code=exited, status=0/success)
main pid: 316 (code=exited, status=0/success)
jul 06 06:09:44 colibri-imx7-02873356 systemd[1]: started launch user's demo on dedicated partition.
jul 06 06:09:44 colibri-imx7-02873356 systemd[1]: user-demo.service: succeeded.
-----------------------------------
最后需要再次修改 /etc/fstab 将 rootfs 根目录设置为只读属性,noatime 后面添加 ro。
-----------------------------------
/dev/root            /                    auto       noatime,ro            1  1
-----------------------------------
重启系统,进入 u-boot 命令模式,配置下参数。
-----------------------------------
setenv ubiargs ubi.mtd=ubi root=ubi0:rootfs ro rootfstype=ubifs ubi.fm_autoconvert=1
saveenv
reset
-----------------------------------
重启进入 linux 系统。根目录 / 已经是只读状态,无法创建文件。而 /home/root/userdata 目录下的应用仍可以正常执行并写入文件。
-----------------------------------
:~# mount -l
ubi0:rootfs on / type ubifs (ro,noatime,assert=read-only,ubi=0,vol=3)
~# mkdir test
mkdir: can't create directory 'test': read-only file system
-----------------------------------
总结
通过将linux 的系统文件设置为只读状态,可以降低因文件系统损坏导致无法启动的概率。对于更高要求的应用,甚至可以使用外部存储作为备份,用于恢复文件。


【前沿技术】全栈式AI驱动型EDA解决方案Synopsys.ai
CDMO模式降低成本是否现实?
Banana Pi 开源社区在深圳国际电子展(2023)上展示全系列新产品
抗高温大功率二极管
LED幕墙灯安装方法及注意事项
如何配置只读属性的文件系统(Colibri iMX7为例)
线交互式UPS逆变器的功能部件与性能特点
基于模型设计的HDL代码自动生成技术应用需求
采访超越申泰:国产计算怎么走?
汽车线束的传统超声波焊接应用
电晃的产生以及解决方案
关于区块链黑客松的事情你了解了多少
全球首个5G+V2X智能终端的广汽埃安AION V量产下线
汽车车门线束的连接布置原则
Oculus将推出来自Oculus VR的三个新项目
轮毂电机的结构形式与主要分类
Dialog半导体将收购Creative Chips公司,扩充工业物联网产品线
透视智能化战场软件建设
PLC控制系统的安装和使用是怎样的
三星电子危机处理:借鉴谷歌赋予董事会更大权利 或任命外部董事局主席