注:本文内容全部为操作层面的内容,不涉及UBI及UBIFS的任何实现及介绍的内容。 个别参数值请自行根据自己设备情况修改!
下面开始介绍如何在Linux 和 U-Boot 下挂载UBIFS。内核从2.6.32版本开始,对UBIFS支持的比较完善,因此建议使用2.6.32以上的版本开发UBIFS。使用的U-Boot版本为2011.12。Flash介质为512MB Nand Flash。1、UBIFS用户态工具
工欲善其事,必先利其器。话虽这样说,实则是不得已而为之,如果不需要工具就能直接搞定岂不是更好。无奈对于挂载UBIFS,我们需要先编译出来一些工具,这些工具位于mtd-utils中,其提供的与UBIFS相关的工具有:
ubinfo - provides information about UBI devices and volumes found in the system;
ubiattach - attaches MTD devices (which describe raw flash) to UBI and creates corresponding UBI devices;
ubidetach - detaches MTD devices from UBI devices (the opposite to what ubiattach does);
ubimkvol - creates UBI volumes on UBI devices;
ubirmvol - removes UBI volumes from UBI devices;
ubiupdatevol - updates UBI volumes; this tool uses the UBI volume update feature which leaves the volume in "corrupted" state if the update was interrupted; additionally, this tool may be used to wipe out UBI volumes;
ubicrc32 - calculates CRC-32 checksum of a file with the same initial seed as UBI would use;
ubinize - generates UBI images;
ubiformat - formats empty flash, erases flash and preserves erase counters, flashes UBI images to MTD devices;
mtdinfo - reports information about MTD devices found in the system.这里我们主要会用到的有:mtdinfo、ubiformat、ubiattach、ubinfo、ubimkvol。工具的编译过程不再详述,可见参考资料【3】。
2. Linux下挂载
内核中在make menuconfig时选好UBIFS相关支持,具体见参考资料【1】。启动Linux 后,将一个MTD分区挂载为UBIFS文件系统的一般步骤为:
1. ubiformat /dev/mtdX //擦除mtdX
2. ubiattach /dev/ubi_ctrl -m Y //和mtdY关联,/dev/ubi_ctrl需要自己创建3. ubimkvol /dev/ubi0 -N ubifs -m //创建一个volume,volume才是最终挂载文件系统的地方, /dev/ubi0需要自己创建
4. mount -t ubifs ubi0_0 /mnt/ubifs //挂载UBIFS
注意:上述步骤的1、3项只在Linux下第一次挂载UBIFS时会用到,后续只要直接执行2、4即可。具体的操作步骤演示:
# cat /proc/mtd
dev: size erasesize name
mtd0: 00f00000 00020000 "fs"
mtd1: 00100000 00020000 "u-boot"
mtd2: 20000000 00020000 "NAND Flash"
可以看到,Nand Flash是mtd2。根据这个信息,我们创建相应的mtd设备:
# mknod -m 777 /dev/mtd4 c 90 4
这时我们可以使用mtdinfo工具来查看一下Nand Flash的信息:
/ # mtdinfo /dev/mtd4
mtd2
Name: NAND Flash
Type: nand
Eraseblock size: 131072 bytes, 128.0 KiB
Amount of eraseblocks: 4096 (536870912 bytes, 512.0 MiB)
Minimum input/output unit size: 2048 bytes
Sub-page size: 512 bytes
OOB size: 64 bytes
Character device major/minor: 90:4
Bad blocks are allowed: true
Device is writable: true
这里的Sub-page size参数会在ubiformat时候用到,另外还要啰嗦一句就是上面的/dev/mtd4 和 mtd2(cat /proc/mtd时的mtd2) 是指同一个设备,只是使用不同命令时显示的不太一样,这点大家千万要搞清楚。
在操作步骤1、2中,还需要创建两个设备:/dev/ubi_ctrl和/dev/ubi0。ubi_ctrl是一个misc设备,主设备号是10,而次设备号是自动分配的:
/ # cat /proc/misc
62 ubi_ctrl
/ #
可以看到,ubi_ctrl的次设备号为62,根据这些信息,我们创建/dev/ubi_ctrl设备:#mknod -m 777 /dev/ubi_ctrl c 10 62
有了/dev/ubi_ctrl设备后,我们便可以执行上述步骤中的1、2:
/ # ubiformat /dev/mtd4 -s 512 -O 2048
ubiformat: mtd2 (nand), size 536870912 bytes (512.0 MiB), 4096 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes
libscan: scanning eraseblock 4095 -- 100 % complete
ubiformat: 4092 eraseblocks have valid erase counter, mean value is 3
ubiformat: 4 bad eraseblocks found, numbers: 4092, 4093, 4094, 4095
ubiformat: formatting eraseblock 4095 -- 100 % complete
这里的-s 512就是我们使用mtdinfo是的Sub-page size的值。
步骤2:
/ # ubiattach /dev/ubi_ctrl -m 2 -O 2048
UBI: attaching mtd2 to ubi0
UBI: physical eraseblock size: 131072 bytes (128 KiB)
UBI: logical eraseblock size: 126976 bytes
UBI: smallest flash I/O unit: 2048
UBI: sub-page size: 512
UBI: VID header offset: 2048 (aligned 2048)
UBI: data offset: 4096
UBI: attached mtd2 to ubi0
UBI: MTD device name: "NAND Flash"
UBI: MTD device size: 512 MiB
UBI: number of good PEBs: 4092
UBI: number of bad PEBs: 4
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: 4048
UBI: total number of reserved PEBs: 44
UBI: number of PEBs reserved for bad PEB handling: 40
UBI: max/mean erase counter: 8/4
UBI: image sequence number: 1638257489
UBI device number 0, total 4092 LEBs (519585792 bytes, 495.5 MiBUBI: background thread "ubi_bgt0d" started, PID 1438
), available 4048 LEBs (513998848 bytes, 490.2 MiB), LEB size 126976 bytes (124.0 KiB)
执行完ubiattach后,系统会产生/dev/ubi0的设备号:
/ # cat /proc/devicesCharacter devices:
1 mem
2 pty
3 ttyp
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/ptmx
7 vcs
9 st
10 misc
13 input
21 sg
89 i2c
90 mtd
128 ptm
136 pts
254 ubi0
可以看到,ubi0的主设备号是254,由于我们只有一个设备,因此次设备号给0即可。
#mknod -m 777 /dev/ubi0 c 254 0
/ # ubimkvol /dev/ubi0 -N ubifs -m
Set volume size to 513998848
Volume ID 0, size 4048 LEBs (513998848 bytes, 490.2 MiB), LEB size 126976 bytes (124.0 KiB), dynamic, name "ubifs", alignment 1
可以通过ubinfo来查看信息:
/ # ubinfo -a
UBI version: 1
Count of UBI devices: 1
UBI control device major/minor: 10:62
Present UBI devices: ubi0
ubi0
Volumes count: 1
Logical eraseblock size: 126976 bytes, 124.0 KiB
Total amount of logical eraseblocks: 4092 (519585792 bytes, 495.5 MiB)
Amount of available logical eraseblocks: 0 (0 bytes)
Maximum count of volumes 128
Count of bad physical eraseblocks: 4
Count of reserved physical eraseblocks: 40
Current maximum erase counter value: 9
Minimum input/output unit size: 2048 bytes
Character device major/minor: 254:0
Present volumes: 0
Volume ID: 0 (on ubi0)
Type: dynamic
Alignment: 1
Size: 4048 LEBs (513998848 bytes, 490.2 MiB)
State: OK
Name: ubifs
Character device major/minor: 254:1
创建挂载目录:
#mkdir /mnt/ubifs步骤4:
/ # mount -t ubifs ubi0_0 /mnt/ubifs
UBIFS: default file-system created
UBIFS: mounted UBI device 0, volume 0, name "ubifs"
UBIFS: file system size: 512094208 bytes (500092 KiB, 488 MiB, 4033 LEBs)
UBIFS: journal size: 25649152 bytes (25048 KiB, 24 MiB, 202 LEBs)
UBIFS: media format: w4/r0 (latest is w4/r0)
UBIFS: default compressor: lzo
UBIFS: reserved for root: 4952683 bytes (4836 KiB)
/ #
查看挂载信息:
/ # df -hFilesystem Size Used Available Use% Mounted on
ubi0_0 451.0M 24.0K 446.3M 0% /mnt/ubifs
上述命令的一些参数不在这里做详细的解释,大家可以自己使用对应命令的 --help 查看这些参数的含义。
3. U-Boot下的挂载
U-Boot下同样需要打开一些UBIFS相关的宏定义,具体内容略。
U-BOOT #
U-BOOT # mtdparts
mtdparts variable not set, see 'help mtdparts'
no partitions defined
defaults:
mtdids : nand0=nand0
mtdparts: mtdparts=nand0:512M(ubifs)
U-BOOT # mtdparts default
U-BOOT #
U-BOOT # mtdparts
device nand0 <nand0>, # parts = 1
#: name size offset mask_flags
0: ubifs 0x20000000 0x00000000 0
active partition: nand0,0 - (ubifs) 0x20000000 @ 0x00000000
defaults:
mtdids : nand0=nand0
mtdparts: mtdparts=nand0:512M(ubifs)
U-BOOT #
U-BOOT # ubi part ubifs
Creating 1 MTD partitions on "nand0":
0x000000000000-0x000020000000 : "mtd=0"
UBI: attaching mtd1 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 mtd1 to ubi0
UBI: MTD device name: "mtd=0"
UBI: MTD device size: 512 MiB
UBI: number of good PEBs: 4092
UBI: number of bad PEBs: 4
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: 4092
UBI: number of PEBs reserved for bad PEB handling: 40
UBI: max/mean erase counter: 7/3
U-BOOT #
U-BOOT #
U-BOOT # ubi part
Device 0: nand0, partition ubifs
U-BOOT #
U-BOOT # ubifsmount ubifs
UBIFS: mounted UBI device 0, volume 0, name "ubifs"
UBIFS: mounted read-only
UBIFS: file system size: 520353792 bytes (508158 KiB, 496 MiB, 4033 LEBs)
UBIFS: journal size: 26062848 bytes (25452 KiB, 24 MiB, 202 LEBs)
UBIFS: media format: w4/r0 (latest is w4/r0)
UBIFS: default compressor: LZO
UBIFS: reserved for root: 5182151 bytes (5060 KiB)
U-BOOT #
U-BOOT #
U-BOOT # ubifsls
14965624 Thu June 01 02::22 2013 vmlinux
U-BOOT #
参考资料:
【1】http://processors.wiki.ti.com/index.php/UBIFS_Support
【2】http://www.linux-mtd.infradead.org/faq/ubi.html
【3】http://processors.wiki.ti.com/index.php/MTD_Utilities#MTD-Utils_Compilation
【4】http://blog.chinaunix.net/uid-20632682-id-82370.html
本文介绍了如何挂载UBIFS文件系统,包括使用mtd-utils中的工具如ubiformat、ubimkvol和mount进行操作,以及挂载过程中的关键参数设置。
1703

被折叠的 条评论
为什么被折叠?



