arm使用ubi系统

UBI文件系统是一种用于裸flash的文件系统管理层,它是专为管理原始闪存设备而设计的,特别适用于嵌入式系统。

  1. UBI与MTD、UBIFS的关系:

    • MTD(Memory Technology Device):是Linux内核中的一个子系统,用于支持不同类型的闪存设备,如NOR Flash和NAND Flash。MTD提供了一个抽象层,使得文件系统和用户空间程序可以方便地访问底层的闪存硬件。
    • UBI:在MTD设备之上增加了一层管理,专门为NAND Flash设计,处理了NAND Flash固有的一些复杂性,如坏块管理和磨损均衡(wear leveling)。UBI将闪存划分为逻辑擦除块,并对它们进行管理。
    • UBIFS(UBI File System):是在UBI卷上运行的文件系统,充分利用UBI的特性,提供了高效可靠的文件存储解决方案。
  2. UBI的主要功能:

    • 坏块管理:UBI能够检测和管理坏块,确保数据写入时不会使用坏块。
    • 磨损均衡:UBI通过均匀分布擦写操作,延长闪存的使用寿命。
    • 逻辑卷管理:UBI支持在MTD设备上创建多个逻辑卷,每个卷可以独立使用。
  3. UBI的应用场景:

    • UBI文件系统特别适用于大容量的NAND Flash,并可以提供整个flash空间的磨损平衡和良好的扩展性。它常被用于嵌入式系统中,以提高闪存设备的可靠性和使用寿命。

查看/dev/mtd8相关信息:

复制代码
# mtdinfo /dev/mtd8
mtd8
Name:                           database
Type:                           mlc-nand
Eraseblock size:                2097152 bytes, 2.0 MiB
Amount of eraseblocks:          3584 (7516192768 bytes, 7.0 GiB)
Minimum input/output unit size: 8192 bytes
Sub-page size:                  8192 bytes
OOB size:                       448 bytes
Character device major/minor:   90:16
Bad blocks are allowed:         true
Device is writable:             true

/dev/mtd8在flash中的1c0000000 00200000:

复制代码
# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00010000 00001000 "at91bootstrap"
mtd1: 00018000 00001000 "u-boot env"
mtd2: 00020000 00001000 "u-boot env redundant"
mtd3: 000e0000 00001000 "u-boot"
mtd4: 000e0000 00001000 "u-boot logo"
mtd5: 00020000 00001000 "device tree"
mtd6: 00600000 00001000 "kernel"
mtd7: 3e000000 00200000 "rootfs"
mtd8: 1c0000000 00200000 "database"
mtd9: 32d98000 001fc000 "rootfs"
mtd10: 190198000 001fc000 "database"

要将ubi系统挂载到pc上,必须要生成镜像文件进行操作:

方法一:使用系统自带的dd命令创建ubi镜像:

复制代码
dd if=/dev/mtd8 of=/mnt/ubi.img   // 用mtd8创建虚拟镜像ubi.img
mkdosfs ubi.img                   // 使用mkdosfs工具把ubi.img 文件格式化成FAT32文件系统
modprobe g_mass_storage file=/mnt/ubi.img removable=1     // 加载驱动,挂载到pc
modprobe g_mass_storage file=/mnt/ubi.img -r(modprobe g_mass_storage  -r)   // 卸载驱动
ubi本地挂载: 
mount -t vfat -o sync /mnt/ubi.img /mnt/mmcblk0p1    // 挂载到本地

缺陷:从相关信息看出有7G左右,使用dd创建虚拟镜像时间过长,不利于批量生产;

优点:不需要担心nand flash的每个块擦写次数限制;

方法二:使用相关工具(mkfs.ubifs、ubinize)生产镜像文件;

1.下载源码mtd-utils-2.0.0.tar.bz2,版本2.1.1-2.1.2需要添加zstd相关:

a.解压缩(tar -xvf mtd-utils-0.0.0.tar.bz2)

b.配置环境变量export PATH=$PATH:/home/arm/arm-linux-gnueabihf-7.5.0/bin使交叉编译工具链到环境变量中

2.下载源码 lzo-2.10.tar.gz:

a.解压缩(tar -xvf lzo-2.10.tar.gz)

b.配置变量(./configure --host=arm --prefix=/home/arm/armUbi/armlzo CC=arm-linux-gnueabihf-gcc --enable-shared)

c.make

d.make install

3.下载源码e2fsprogs-1.41.14.tar.gz:

复制代码
@ubuntu:~/arm$ export PATH=$PATH:/home/arm/arm-linux-gnueabihf-7.5.0/bin

@ubuntu:~/arm/armUbi$ tar -xvf lzo-2.10.tar.gz
@ubuntu:~/arm/armUbi/lzo-2.10$ ./configure --host=arm --prefix=/home/arm/armUbi/armlzo CC=arm-linux-gnueabihf-gcc --enable-shared
@ubuntu:~/arm/armUbi/lzo-2.10$ make
@ubuntu:~/arm/armUbi/lzo-2.10$ make install

@ubuntu:~/arm/armUbi$ tar -xvf zlib-1.2.11.tar.gz
@ubuntu:~/arm/armUbi/zlib-1.2.11$ ./configure --host=arm --prefix=/home/arm/armUbi/armZlib CC=arm-linux-gnueabihf-gcc
@ubuntu:~/arm/armUbi/zlib-1.2.11$ make
@ubuntu:~/arm/armUbi/zlib-1.2.11$ make install

@ubuntu:~/arm/armUbi$ tar -zvf e2fsprogs-1.45.6.tar.gz
@ubuntu:~/arm/armUbi/e2fsprogs-1.45.6$ ./configure --host=arm --prefix=/home/arm/armUbi/armUuid CC=arm-linux-gnueabihf-gcc
@ubuntu:~/arm/armUbi/e2fsprogs-1.45.6$ make
@ubuntu:~/arm/armUbi/e2fsprogs-1.45.6$ make install

@ubuntu:~/arm$ tar -xvf mtd-utils-0.0.0.tar.bz2
@ubuntu:~/arm/armUbi/mtd-utils-2.0.0$ ./configure --host=arm --prefix=/home/arm/armUbi/armMtaUtils CC=arm-linux-gnueabihf-gcc --enable-shared --without-crypto
@ubuntu:~/arm/armUbi/mtd-utils-2.0.0$ make
@ubuntu:~/arm/armUbi/mtd-utils-2.0.0$ make install

a.错误,缺少一个或多个依赖项:

复制代码
configure: WARNING: cannot find uuid library required for mkfs.ubifs
configure: mtd-utils can optionally be built without mkfs.ubifs
configure: WARNING: cannot find LZO library required for mkfs programs
configure: mtd-utils can optionally be built without mkfs.ubifs
configure: mtd-utils can optionally be built without mkfs.jffs2
configure: mtd-utils can optionally be built without LZO support
configure: WARNING: cannot find ZSTD library required for mkfs program
configure: mtd-utils can optionally be built without mkfs.ubifs
configure: mtd-utils can optionally be built without ZSTD support
configure: WARNING: cannot find headers for extended attributes
configure: WARNING: disabling XATTR support
configure: error: missing one or more dependencies

解决(无法解决):

复制代码
sudo apt-get install libuuid1:i386
sudo apt-get install uuid-dev

下载lzo-2.10.tar.gz e2fsprogs-1.41.14.tar.gz zlib-1.2.11.tar.gz交叉编译,配置环境变量解决:

复制代码
export ZLIB_CFLAGS=-I/home/arm/armUbi/armZlib/include
export ZLIB_LIBS=-L/home/arm/armUbi/armZlib/lib
export LZO_CFLAGS=-I/home/arm/armUbi/armlzo/include
export LZO_LIBS=-L/home/arm/armUbi/armlzo/lib
export UUID_CFLAGS=-I/home/arm/armUbi/armUuid/include
export UUID_LIBS=-L/home/arm/armUbi/armUuid/lib

错误,找不到库:

复制代码
ubifs-utils/mkfs.ubifs/mkfs_ubifs-mkfs.ubifs.o: In function `write_super':
/home/arm/armUbi/mtd-utils-2.0.0/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c:2144: undefined reference to `uuid_generate_random'
/home/arm/armUbi/mtd-utils-2.0.0/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c:2148: undefined reference to `uuid_unparse_upper'
collect2: error: ld returned 1 exit status
Makefile:1983: recipe for target 'mkfs.ubifs' failed
make: *** [mkfs.ubifs] Error 1

配置环境变量:

复制代码
export LDFLAGS="$ZLIB_LIBS $LZO_LIBS $UUID_LIBS -luuid -lz -luuid"
export CFLAGS="-O2 -g $ZLIB_CFLAGS $LZO_CFLAGS $UUID_CFLAGS"

使用相关工具(mkfs.ubifs、ubinize)生产镜像文件:

1.查看相关的命令ls /home/libs/armMtdUtils/sbin

复制代码
doc_loadbios     ftl_check        nandwrite        ubidetach
docfdisk         ftl_format       nftl_format      ubiformat
flash_erase      jffs2dump        nftldump         ubimkvol
flash_eraseall   jffs2reader      recv_image       ubinfo
flash_lock       mkfs.jffs2       rfddump          ubinize
flash_otp_dump   mkfs.ubifs       rfdformat        ubirename
flash_otp_info   mtd_debug        serve_image      ubirmvol
flash_otp_lock   mtdinfo          sumtool          ubirsvol
flash_otp_write  mtdpart          ubiattach        ubiupdatevol
flash_unlock     nanddump         ubiblock
flashcp          nandtest         ubicrc32

2../mkfs.ubifs -m 8192 -e 2097152 -c 3584 -r /mnt/mmcblk0p1 /home/ubifs.img

-r 指定镜像目录

-m 最小单元(Minimum input/output unit size: 8192 bytes)

-e 2097152 逻辑擦除大小,通过ubi族命令可以看到该值,比如用format,attach先看看该值(Eraseblock size:2097152 bytes, 2.0 MiB)

-c 3584 (Amount of eraseblocks:3584 (7516192768 bytes, 7.0 GiB))

复制代码
# mtdinfo /dev/mtd8
mtd8
Name:                           database
Type:                           mlc-nand
Eraseblock size:                2097152 bytes, 2.0 MiB
Amount of eraseblocks:          3584 (7516192768 bytes, 7.0 GiB)
Minimum input/output unit size: 8192 bytes
Sub-page size:                  8192 bytes
OOB size:                       448 bytes
Character device major/minor:   90:16
Bad blocks are allowed:         true
Device is writable:             true

# cd home/libs/armMtdUtils/
# cd sbin/
# ./mkfs.ubifs -m 8192 -e 2097152 -c 3584 -r ^C
# ubiattach /dev/ubi_ctrl -m 8
ubi1: default fastmap pool size: 175
ubi1: default fastmap WL pool size: 87
ubi1: attaching mtd8
ubi1: scanning is finished
ubi1: attached mtd8 (name "database", size 7168 MiB)
ubi1: PEB size: 2097152 bytes (2048 KiB), LEB size: 2080768 bytes
ubi1: min./max. I/O unit sizes: 8192/8192, sub-page size 8192
ubi1: VID header offset: 8192 (aligned 8192), data offset: 16384
ubi1: good PEBs: 3580, bad PEBs: 4, corrupted PEBs: 0
ubi1: user volume: 1, internal volumes: 1, max. volumes count: 128
ubi1: max/mean erase counter: 1/0, WL threshold: 4096, image sequence number: 1409005220
ubi1: available PEBs: 121, total reserved PEBs: 3459, PEBs reserved for bad PEB handling: 76
ubi1: background thread "ubi_bgt1d" started, PID 183
UBI device number 1, total 3580 LEBs (7449149440 bytes, 6.9 GiB), available 121 LEBs (251772928 bytes, 240.1 MiB), LEB size 2080768 bytes (1.9 MiB)
# mount -t ubifs ubi1_0 /mnt/mmcblk0p1
UBIFS (ubi1:0): background thread "ubifs_bgt1_0" started, PID 189
UBIFS (ubi1:0): UBIFS: mounted UBI device 1, volume 0, name "database"
UBIFS (ubi1:0): LEB size: 2080768 bytes (2032 KiB), min./max. I/O unit sizes: 8192 bytes/8192 bytes
UBIFS (ubi1:0): FS size: 7005945856 bytes (6681 MiB, 3367 LEBs), journal size 33292288 bytes (31 MiB, 16 LEBs)
UBIFS (ubi1:0): reserved for root: 4952683 bytes (4836 KiB)
UBIFS (ubi1:0): media format: w5/r0 (latest is w5/r0), UUID EA8F755E-8AB4-4942-A4B0-9D04A44D51E2, small LPT model
# ./mkfs.ubifs -m 8192 -e 2097152 -c 3584 -r /mnt/mmcblk0p1 /home.ubifs.img^C
# ls /home/
shared      ubifs.img
# rm /home/ubifs.img 
# ./mkfs.ubifs -m 8192 -e 2097152 -c 3584 -r /mnt/mmcblk0p1 /home.ubifs.img
# ls /home -l
total 0
drwxr-xr-x    2 root     root           160 Jan  1  2012 shared
# ls
doc_loadbios     ftl_check        nandwrite        ubidetach
docfdisk         ftl_format       nftl_format      ubiformat
flash_erase      jffs2dump        nftldump         ubimkvol
flash_eraseall   jffs2reader      recv_image       ubinfo
flash_lock       mkfs.jffs2       rfddump          ubinize
flash_otp_dump   mkfs.ubifs       rfdformat        ubirename
flash_otp_info   mtd_debug        serve_image      ubirmvol
flash_otp_lock   mtdinfo          sumtool          ubirsvol
flash_otp_write  mtdpart          ubiattach        ubiupdatevol
flash_unlock     nanddump         ubiblock
flashcp          nandtest         ubicrc32
# ls /
Settings        lib             opt             tmp
bin             lib32           proc            usr
dev             linuxrc         root            var
etc             lost+found      run
home            media           sbin
home.ubifs.img  mnt             sys
# rm /home.ubifs.img 
# ./mkfs.ubifs -m 8192 -e 2097152 -c 3584 -r /mnt/mmcblk0p1 /home/ubifs.img
# ls /home/ -l
total 26624
drwxr-xr-x    2 root     root           160 Jan  1  2012 shared
-rw-r--r--    1 root     root      27262976 Jan  1 00:11 ubifs.img
# modprobe g_mass_storage file=/home/ubifs.img removable=1 ^C
# mkdosfs /home/ubifs.img 
mkfs.fat 4.1 (2017-01-24)
#  modprobe g_mass_storage file=/home/ubifs.img removable=1
Mass Storage Function, version: 2009/09/11
LUN: removable file: (no medium)
LUN: removable file: /home/ubifs.img
Number of LUNs=1
g_mass_storage gadget: Mass Storage Gadget, version: 2009/09/11
g_mass_storage gadget: userspace failed to provide iSerialNumber
g_mass_storage gadget: g_mass_storage ready
# g_mass_storage gadget: high-speed config #1: Linux File-Backed Storage
# modprobe g_mass_storage -r
# mount
ubi0:rootfs on / type ubifs (rw,relatime,assert=read-only,ubi=0,vol=0)
devtmpfs on /dev type devtmpfs (rw,relatime,size=91024k,nr_inodes=22756,mode=755)
proc on /proc type proc (rw,relatime)
devpts on /dev/pts type devpts (rw,relatime,gid=5,mode=620,ptmxmode=666)
tmpfs on /dev/shm type tmpfs (rw,relatime,mode=777)
tmpfs on /tmp type tmpfs (rw,relatime)
tmpfs on /run type tmpfs (rw,nosuid,nodev,relatime,mode=755)
sysfs on /sys type sysfs (rw,relatime)
ubi1_0 on /mnt/mmcblk0p1 type ubifs (rw,relatime,assert=read-only,ubi=1,vol=0)
# ls /mnt/
mmcblk0p1  ubi.img
# umount /mnt/mmcblk0p1/
UBIFS (ubi1:0): un-mount UBI device 1
UBIFS (ubi1:0): background thread "ubifs_bgt1_0" stops
# mount -t vfat -o sync /home/ubifs.img  /mnt/mmcblk0p1/
# ls /mnt/mmcblk0p1/
System Volume Information  sc.txt

方法三:使用自带/dev/mtdblock8挂载到pc:

mkdosfs -I /dev/mtdblock8 // 强制初始化 /dev/mtdblock8

modprobe g_mass_storage file=/dev/mtdblock8 removable=1 // 加载驱动,挂载到pc

modprobe g_mass_storage -r // 卸载驱动

mount -t vfat -o sync /dev/mtdblock8 /mnt/mmcblk0p1/ // 挂载到本地与pc间进行同步

当pc写入数据时, umount /mnt/mmcblk0p1/ 然后重新mount 数据就同步了

当linux写入数据时,PC端插拔下USB线 或者rmmod g_mass_storage 然后重新g_mass_storage.ko来同步数据

复制代码
#mount -t vfat -o sync /dev/mtdblock8 /mnt/mmcblk0p1/
mount: mounting /dev/mtdblock8 on /mnt/mmcblk0p1/ failed: Device or resource busy
#mount
/dev/mtdblock8 on /mnt/mmcblk0p1 type vfat (rw,sync,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)

mount 时会打印mount: mounting /dev/mtdblock8 on /mnt/mmcblk0p1/ failed: Device or resource busy

装载/dev/mtdblock8/失败:设备或资源忙,已经mount上了

缺陷:nand flash的每个块擦写次数大约十万次,意思就是你一直怼一个nand块 那就很快没用了。然后如果FAT没有对flash写入进行负载均衡,很容易出现坏块。

写入数据太慢,造成板子非常卡顿,无法正常操作;

优点:简单方便,不需要复杂过程;

相关推荐
律宏阔1 小时前
WSL Docker 端口明明空闲,但就是绑不上端口
linux·windows
律宏阔1 小时前
WSL 突然断网,无法 ping 内网或外网
linux·windows
穷人小水滴1 小时前
用容器编译 VirtualBox 虚拟机软件 (ArchLinux, podman)
linux·容器·virtualbox
乱码三千1 小时前
如何优雅地直连无公网 IP 的远程 GPU 服务器
linux·人工智能·程序员
yunwei371 小时前
使用 eBPF 跟踪 Nginx 请求
linux·后端·性能优化
yunwei371 小时前
使用 eBPF 跟踪 MySQL 查询
linux·后端·性能优化
yunwei371 小时前
eBPF 示例教程:使用 XDP 捕获 TCP 信息
linux·后端·性能优化
GeW2 小时前
制造业高端化,为什么必须重估Linux和数据库?
linux
GeW2 小时前
工业场景中的Linux与数据库选型:高端化转型的技术底座
linux
yunwei372 小时前
eBPF 开发者教程: 简单的 XDP 负载均衡器
linux·后端·性能优化