Blog:
UBI 只读文件系统

Mittwoch, 6. Juli 2022

在先前的文章我们已经介绍在使用 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 的系统文件设置为只读状态,可以降低因文件系统损坏导致无法启动的概率。对于更高要求的应用,甚至可以使用外部存储作为备份,用于恢复文件。

Autor: 胡珊逢,FAE,韬睿(上海)

Kommentar hinterlassen

Please login to leave a comment!
Have a Question?