UBI文件系統(tǒng)簡介 收藏
在linux-2.6.27以前,談到Flash文件系統(tǒng),大家很多時候多會想到cramfs、jffs2、yaffs2等文件系統(tǒng)。它們也都是基于文件系統(tǒng)+mtd+flash設(shè)備的架構(gòu)。linux-2.6.27后,內(nèi)核加入了一種新型的flash文件系統(tǒng)UBI(Unsorted Block Images)。這里簡單介紹下UBI文件系統(tǒng)加入的原因,及使用方法。我也是剛接觸到這個文件系統(tǒng),可能有理解不對的地方,也請指正。
一、產(chǎn)生的背景
FLASH具有的“先擦除再寫入”、壞塊、“有限的讀寫次數(shù)”等特性,目前管理FLASH的方法主要有:
1、采用MTD+FTL/NFTL(flash 轉(zhuǎn)換層/nand flash轉(zhuǎn)換層)+ 傳統(tǒng)文件系統(tǒng),如:FAT、ext2等。FTL/NFTL的使用就是針對FLASH的特有屬性,通過軟件的方式來實現(xiàn)日志管理、壞塊管理、損益均衡等技術(shù)。但實踐證明,由于知識產(chǎn)權(quán)、效率等各方面因素導(dǎo)致本方案有一定的局限性。
2、采用硬件翻譯層+傳統(tǒng)文件系統(tǒng)的方案。這種方法被很多存儲卡產(chǎn)品采用,如:SD卡、U盤等。這種方案對于一些產(chǎn)品來說,成本較高。
3、采用MTD+ FLASH專用文件系統(tǒng),如JFFS1/2,YAFFS1/2等。它們大大提高了FLASH的管理能力,并被廣泛應(yīng)用。
JFFS2、YAFFS2等專用文件系統(tǒng)也存在著一些技術(shù)瓶頸,如:內(nèi)存消耗大,對FLASH容量、文件系統(tǒng)大小、內(nèi)容、訪問模式等的線性依賴,損益均衡能力差或過渡損益等。在此背景下內(nèi)核加入了UBI文件系統(tǒng)的支持。
二、用法
環(huán)境:omap3530處理器、 (128MByte 16 位NAND Flash) 、linnux-2.6.28內(nèi)核
1、配置內(nèi)核支持UBIFS
Device Drivers --->Memory Technology Device (MTD) support --->UBI - Unsorted block images --->Enable UBI
配置mtd支持UBI接口
File systems --->Miscellaneous filesystems --->UBIFS file system support
配置內(nèi)核支持UBIFS文件系統(tǒng)
2、將一個MTD分區(qū)4掛載為UBIFS格式
● flash_eraseall /dev/mtd4 //擦除mtd4
● ubiattach /dev/ubi_ctrl -m 4 //和mtd4關(guān)聯(lián)
● ubimkvol /dev/ubi0 -N rootfs -s 100MiB //設(shè)定volume 大小(不是固定值,可以用工具改變)及名稱
● mount -t ubifs ubi0_0 /mnt/ubi或mount -t ubifs ubi0:rootfs /mnt/ubi
3、制作UBIFS文件系統(tǒng)
在制作UBI鏡像時,需要首先確定以下幾個參數(shù):
MTD partition size; //對應(yīng)的FLASH分區(qū)大小
flash physical eraseblock size; // FLASH物理擦除塊大小
minimum flash input/output unit size; //最小的FLASH輸入輸出單元大小
for NAND flashes - sub-page size; //對于nand flash來說,子頁大小
logical eraseblock size.//邏輯擦除塊大小
參數(shù)可以由幾種方式得到
1)如果使用的是2.6.30以后的內(nèi)核,這些信息可以通過工具從內(nèi)核獲得,如:mtdinfo –u。
2)之前的內(nèi)核可以通過以下方法:
● MTD partition size:從內(nèi)核的分區(qū)表或cat /proc/mtd獲得
● flash physical eraseblock size:從flash芯片手冊中可以得到FLASH物理擦除塊大小,或cat /proc/mtd
● minimum flash input/output unit size:
1)nor flash:通常是1個字節(jié)
2)nand falsh:一個頁面
● sub-page size:通過flash手冊獲得
● logical eraseblock size:對于有子頁的NAND FLASH來說,等于“物理擦除塊大小-1頁的大小”
3)也可以通過ubi和mtd連接時的產(chǎn)生的信息獲取,如:
#modprobe ubi mtd=4 //ubi作為模塊加載
或
#ubiattach /dev/ubi_ctrl -m 4 //通過ubiattach關(guān)聯(lián)MTD
UBI: attaching mtd4 to ubi0
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 129024 bytes
UBI: smallest flash I/O unit: 2048
UBI: sub-page size: 512
UBI: VID header offset: 512 (aligned 512)
UBI: data offset: 2048
UBI: attached mtd4 to ubi0
更詳細的解釋參見http://www.linux-mtd.infradead.org/doc/ubi.html#L_overhead
#mkfs.ubifs -r rootfs -m 2048 -e 129024 -c 812 -o ubifs.img
#ubinize -o ubi.img -m 2048 -p 128KiB -s 512 /home/lht/omap3530/tools/ubinize.cfg
-r:制定文件內(nèi)容的位置
-m:頁面大小
-e:邏輯擦除塊大小
-p:物理擦除塊大小
-c:最大的邏輯擦除塊數(shù)量
對我們這種情況,文件系統(tǒng)最多可以訪問卷上的129024*812=100M空間
-s:最小的硬件輸入輸出頁面大小,如:k9f1208為256(上下半頁訪問)
其中,ubinize.cfg的內(nèi)容為:
[ubifs]
mode=ubi
image=ubifs.img
vol_id=0
vol_size=100MiB
vol_type=dynamic
vol_name=rootfs
vol_flags=autoresize
4、利用uboot燒寫、啟動UBIFS鏡像
1)燒寫UBIFS鏡像
OMAP3 DevKit8000 # mmcinit
OMAP3 DevKit8000 # fatload mmc 0:1 81000000 ubi.img
reading ubi.img
12845056 bytes read
OMAP3 DevKit8000 # nand unlock
device 0 whole chip
nand_unlock: start: 00000000, length: 268435456!
NAND flash successfully unlocked
OMAP3 DevKit8000 # nand ecc sw
OMAP3 DevKit8000 # nand erase 680000 7980000
NAND erase: device 0 offset 0x680000, size 0x7980000
Erasing at 0x7fe0000 -- 100% complete.
OK
OMAP3 DevKit8000 # nand write.i 81000000 680000 $(filesize)
NAND write: device 0 offset 0x680000, size 0xc40000
Writing data at 0x12bf800 -- 100% complete.
12845056 bytes written: OK
燒寫過程和燒寫內(nèi)核鏡像的過程一致,所以UBI文件系統(tǒng)應(yīng)該不像yaffs文件系統(tǒng)那樣用到了nand的OOB區(qū)域。
2)設(shè)置UBIFS文件系統(tǒng)作為根文件系統(tǒng)啟動的參數(shù)
OMAP3 DevKit8000 # setenv bootargs console=ttyS2,115200n8 ubi.mtd=4 root=ubi0:rootfs
rootfstype=ubifs video=omapfb:mode:4.3inch_LCD
OMAP3 DevKit8000 # setenv bootcmd nand read.i 80300000 280000 200000/;bootm 80300000
根文件系統(tǒng)的位置在MTD4上
系統(tǒng)啟動時會打印出如下和UBI相關(guān)的信息:
Creating 5 MTD partitions on "omap2-nand":
0x00000000-0x00080000 : "X-Loader"
0x00080000-0x00260000 : "U-Boot"
0x00260000-0x00280000 : "U-Boot Env"
0x00280000-0x00680000 : "Kernel"
0x00680000-0x08000000 : "File System"
UBI: attaching mtd4 to ubi0
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 129024 bytes
UBI: smallest flash I/O unit: 2048
UBI: sub-page size: 512
UBI: VID header offset: 512 (aligned 512)
UBI: data offset: 2048
UBI: attached mtd4 to ubi0
UBI: MTD device name: "File System"
UBI: MTD device size: 121 MiB
UBI: number of good PEBs: 970
UBI: number of bad PEBs: 2
UBI: max. allowed volumes: 128
UBI: wear-leveling threshold: 4096
UBI: number of internal volumes: 1
UBI: number of user volumes: 1
UBI: available PEBs: 0
UBI: total number of reserved PEBs: 970
UBI: number of PEBs reserved for bad PEB handling: 9
UBI: max/mean erase counter: 2/0
以上是從網(wǎng)絡(luò)下載下來的關(guān)于ubifs的操作方法,有一定的參考價值。
以下是從marvell獲得操作文檔。
Building the mtd-utils
======================
For Debian distribution, you need the following packages installed for
building the head snapshot of mtd-utils,
uuid-dev
liblzo2-dev
libz-dev
Extract the latest snapshot of mtd-utils and go into the sub directory of
'mkfs.ubifs' and then type 'make' to build the mkfs.ubifs utility. Once it
is done, go up one directory and then go into the sub directory 'ubi-tuils'.
Inside that directory, type 'make' again to make the rest of ubi utilities
(ubinfo, ubinize, ubiformat, etc.).
How to find out min. I/O unit size, sub-page size, etc
======================================================
The easiest way to find this out is to attach your MTD device to UBI and
glance to the syslog/dmesg output (erase the MTD device before doing this).
The newest UBI prints something like this:
UBI: attaching mtd0 to ubi0
UBI: physical eraseblock size: 262144 bytes (256 KiB)
UBI: logical eraseblock size: 258048 bytes
UBI: smallest flash I/O unit: 2048
UBI: VID header offset: 2048 (aligned 2048)
UBI: data offset: 4096
UBI: empty MTD device detected
UBI: create volume table (copy #1)
UBI: create volume table (copy #2)
UBI: attached mtd0 to ubi0
UBI: MTD device name: "rootfs"
UBI: MTD device size: 2048 MiB
UBI: number of good PEBs: 8183
UBI: number of bad PEBs: 9
UBI: max. allowed volumes: 128
UBI: wear-leveling threshold: 4096
UBI: number of internal volumes: 1
UBI: number of user volumes: 0
UBI: available PEBs: 8098
UBI: total number of reserved PEBs: 85
UBI: number of PEBs reserved for bad PEB handling: 81
UBI: max/mean erase counter: 0/0
UBI: background thread "ubi_bgt0d" started, PID 254
How to attach an MTD device
===========================
If UBI is compiled into the kernel, the mtd device to attach may be specified
in the ubi.mtd=kernel boot parameter, e.g.,
ubi.mtd=0
(For attaching the first MTD device)
How to Mount a UBI device
=========================
First, you need to create a user volume on the UBI device.
ubimkvol /dev/ubi0 -n 0 -N rootfs -s 4096MiB
The above commands will only work after the correct kernel boot parameter is
specified. Once the user volume is created, you may use the following command
to mount it,
mount -t ubifs ubi0:rootfs /mnt
If there is no need to keep the user volume, you may use the following command
to delete it,
ubirmvol /dev/ubi0 -n 0
How to create UBI images
========================
First, you need to use the mkfs.ubifs utility to create a volume for your root
file system,
mkfs.ubifs -m 2KiB -e 258048 -x lzo -c 8000 -d foxes-rootfs-20081128/ -o system_ubifs.img
(The above command must have root previledge)
Then use the ubinize utility to combine all the volumes you need into a UBI
image,
ubinize -o system_ubi.img -m 2KiB -p 256KiB -s 2KiB ubinize.cfg
In the ubinize.cfg, the following items are specified,
# Section header
[rootfs]
# Volume mode (other option is static)
mode=ubi
# Source image
image=system_ubifs.img
# Volume ID in UBI image
vol_id=0
# Volume size
vol_size=1900MiB
# Allow for dynamic resize
vol_type=dynamic
# Volume name
vol_name=rootfs
# Autoresize volume at first mount
vol_flags=autoresize
How to flash UBI images (and preserve erase counters)
=====================================================
First, you need to detach the ubi device by using the follwing command if the
ubi device is attached,
ubidetach /dev/ubi_ctrl -d 0
The, use the ubiformat utility.
ubiformat /dev/mtd0 -f system_ubi.img
How to use UBIFS as the root file system
========================================
You must specify the following text in the kernel boot parameter,
ubi.mtd=0 root=ubi0:rootfs rootfstype=ubifs rw mtdparts=cafe_nand:2G(rootfs)
More Infromation
================
You may obtain more information at the following web sites,
(1) UBI FAQ and HOWTO (Memory Technology Device (MTD) Subsystem for Linux)
http://www.linux-mtd.infradead.org/faq/ubi.html
(2) UBIFS initial experiments (OLPC)
http://wiki.laptop.org/go/UBIFS_initial_experiments
兩者雖有重復(fù)的地方,卻有互有補充的地方。
以上的指導(dǎo),可以應(yīng)付大部分的應(yīng)用層操作了。
聯(lián)系客服