InvenSense(应美盛)传感器/陀螺仪IC芯片全系列-亿配芯城-如何配置只读属性的文件系统(Colibri iMX7为例)
你的位置:InvenSense(应美盛)传感器/陀螺仪IC芯片全系列-亿配芯城 > 芯片资讯 > 如何配置只读属性的文件系统(Colibri iMX7为例)
如何配置只读属性的文件系统(Colibri iMX7为例)
发布日期:2024-01-09 12:21     点击次数:167

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则可以写入数据。

1657097520324326.png

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里包含了需要烧录到userdatavolume的文件,这些是用户应用和配置等,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的系统文件设置为只读状态,可以降低因文件系统损坏导致无法启动的概率。对于更高要求的应用,甚至可以使用外部存储作为备份,用于恢复文件。

审核编辑:黄飞