By Toradex胡珊逢
在先前的文章我們已經(jīng)介紹在使用eMMC的模塊上配置只讀屬性的文件系統(tǒng),以及利用squashfs和overlayfs掛載可寫(xiě)分區(qū)。Toradex的產(chǎn)品除了使用eMMC存儲(chǔ)外,還有部分是采用Nand Flash,例如Colibri iMX7和Colibri iMX6ULL。下面將以Colibri iMX7為例介紹如何配置只讀屬性的文件系統(tǒng)。
由于存儲(chǔ)介質(zhì)不同,Nand Flash上通常采用如jffs2、UBI等格式文件系統(tǒng)。Toradex的Linux系統(tǒng)使用UBI文件系統(tǒng)。
在Colibri iMX7的Nand Flash上會(huì)采用以下規(guī)劃。Nand Flash總體上劃分為兩個(gè)部分。最前面的raw部分不采用任何文件系統(tǒng),直接存儲(chǔ)模塊硬件信息bcb,u-boot和u-boot環(huán)境變量。第二部分則使用UBI,創(chuàng)建5個(gè)volume,用于存儲(chǔ)內(nèi)核文件(kernel)、設(shè)備樹(shù)文件(dtb)、M4的固件(m4-fw)、Linux文件系統(tǒng)(rootfs)、用戶文件(userdata)。其中rootfs將設(shè)置成自讀屬性,而userdata則可以寫(xiě)入數(shù)據(jù)。
Toradex Easy Installer可以通過(guò)image.json文件方便地修改分區(qū),從而避免使用命令工具。首先從這里下載用于Colibri iMX7S的Linux BSP v5.x安裝文件。解壓后在image.json中添加userdata的相關(guān)配置。
-----------------------------------
{
"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的文件,這些是用戶應(yīng)用和配置等,uncompressed_size是指app.tar.xz未壓縮的大小,用于顯示Toradex Easy Installer的安裝進(jìn)度條。更多關(guān)于image.json配置說(shuō)明請(qǐng)參考這里。
使用Toradex Easy Installer將上面修改的鏡像燒錄到Colibri iMX7S即可。啟動(dòng)后進(jìn)入u-boot,使用下面名可以看到所創(chuàng)建的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
-----------------------------------
啟動(dòng)進(jìn)入Linux后,userdata并不會(huì)被自動(dòng)掛載,需要將下面內(nèi)容添加到/etc/fstab文件中。現(xiàn)在rootfs根目錄還沒(méi)有設(shè)置成只讀屬性,可以創(chuàng)建/home/root/userdata目錄用于掛載userdata卷。
-----------------------------------
ubi:userdata /home/root/userdata ubifs defaults,noatime,rw 1 1
-----------------------------------
于此同時(shí),還可以進(jìn)行系統(tǒng)配置。例如添加一個(gè)開(kāi)機(jī)啟動(dòng)應(yīng)用。該應(yīng)用write_to_file在運(yùn)行時(shí)會(huì)往/home/root/userdata寫(xiě)入一個(gè)文件。在/etc/systemd/system/目錄下創(chuàng)建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
-----------------------------------
運(yùn)行下面命令使user-demo.service開(kāi)機(jī)運(yùn)行。然后重啟系統(tǒng)。
-----------------------------------
~# systemctl enable user-demo.service
~#reboot
-----------------------------------
此時(shí),使用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寫(xiě)入的文件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根目錄設(shè)置為只讀屬性,noatime后面添加ro。
-----------------------------------
/dev/root / auto noatime,ro 1 1
-----------------------------------
重啟系統(tǒng),進(jìn)入u-boot命令模式,配置下參數(shù)。
-----------------------------------
setenv ubiargs "ubi.mtd=ubi root=ubi0:rootfs ro rootfstype=ubifs ubi.fm_autoconvert=1"
saveenv
reset
-----------------------------------
重啟進(jìn)入Linux系統(tǒng)。根目錄/已經(jīng)是只讀狀態(tài),無(wú)法創(chuàng)建文件。而/home/root/userdata目錄下的應(yīng)用仍可以正常執(zhí)行并寫(xiě)入文件。
-----------------------------------
:~# 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
-----------------------------------
總結(jié)
通過(guò)將Linux的系統(tǒng)文件設(shè)置為只讀狀態(tài),可以降低因文件系統(tǒng)損壞導(dǎo)致無(wú)法啟動(dòng)的概率。對(duì)于更高要求的應(yīng)用,甚至可以使用外部存儲(chǔ)作為備份,用于恢復(fù)文件。
審核編輯:黃飛
-
Linux
+關(guān)注
關(guān)注
87文章
11420瀏覽量
212364 -
Nand flash
+關(guān)注
關(guān)注
7文章
241瀏覽量
40461 -
emmc
+關(guān)注
關(guān)注
7文章
225瀏覽量
53587 -
UBI
+關(guān)注
關(guān)注
0文章
9瀏覽量
4167 -
rootfs
+關(guān)注
關(guān)注
0文章
20瀏覽量
4759
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
基于Buildroot的Linux系統(tǒng)構(gòu)建之根文件系統(tǒng)
linux驅(qū)動(dòng)開(kāi)發(fā)_文件系統(tǒng)本地掛載

適用于iMX7處理器的的小巧高效完整電源解決方案
一文搞懂定制Ubuntu文件系統(tǒng)-基于迅為imx6開(kāi)發(fā)板
基于iMX7示例了雙路以太網(wǎng)的設(shè)計(jì)和配置思路
基于iMX7 M4 SPI Slave模式的驅(qū)動(dòng)供參考
基于NXP iMX7 arm處理器展示鋰電池的應(yīng)用方案
imx8qxp-mek將文件推送到“/vendor/etc”目錄時(shí)出現(xiàn)只讀文件系統(tǒng)錯(cuò)誤是怎么回事?
基于Cramfs的根文件系統(tǒng)配置
NXP iMX7 ARM處理器上部署FreeRTOS

適用iMX7系列應(yīng)用處理器的小巧高效且靈活易用型電源參考設(shè)計(jì)
Linux嵌入式文件系統(tǒng)如何構(gòu)建
只讀壓縮文件系統(tǒng)EROFS的設(shè)計(jì)與實(shí)現(xiàn)

如何使用squashfs只讀文件系統(tǒng)制作Linux系統(tǒng)文件

評(píng)論