Buildroot创建ramdisk、ext4、ubifs镜像,以及mkfs.ext4/mkfs.ubifs/cpio的使用

发布时间 2023-07-07 17:56:07作者: ArnoldLu

通过mkfs.ext4和mkfs.ubifs可以生成ext4和ubi格式的文件系统文件。Buildroot中创建文件系统文件即借助这两个命令。

1. mkfs.ext4

mkfs.ext4以及mkfs.ext2/mkfs.ext3都指向mke2fs,用于创建ext4格式的文件系统。

Usage: mkfs.ext4 [-c|-l filename] [-b block-size] [-C cluster-size]
    [-i bytes-per-inode] [-I inode-size] [-J journal-options]
    [-G flex-group-size] [-N number-of-inodes] [-d root-directory]
    [-m reserved-blocks-percentage] [-o creator-os]
    [-g blocks-per-group] [-L volume-label] [-M last-mounted-directory]
    [-O feature[,...]] [-r fs-revision] [-E extended-option[,...]]
    [-t fs-type] [-T usage-type ] [-U UUID] [-e errors_behavior][-z undo_file]
    [-jnqvDFSV] device [blocks-count]
  • -b<区块大小> 指定区块大小,单位为字节。
  • -c 检查是否有损坏的区块。
  • -f<不连续区段大小> 指定不连续区段的大小,单位为字节。
  • -F 不管指定的设备为何,强制执行mke2fs。
  • -i<字节> 指定"字节/inode"的比例。
  • -N<inode数> 指定要建立的inode数目。
  • -l<文件> 从指定的文件中,读取文件西中损坏区块的信息。
  • -L<标签> 设置文件系统的标签名称。
  • -m<百分比值> 指定给管理员保留区块的比例,预设为5%。
  • -M 记录最后一次挂入的目录。
  • -q 执行时不显示任何信息。
  • -r 指定要建立的ext2文件系统版本。
  • -R=<区块数> 设置磁盘阵列参数。
  • -S 仅写入superblock与group descriptors,而不更改inode able inode bitmap以及block bitmap。
  • -v 执行时显示详细信息。
  • -V 显示版本信息。

2. mkfs.ubifs

Usage: mkfs.ubifs [OPTIONS] target
Make a UBIFS file system image from an existing directory tree

Examples:
Build file system from directory /opt/img, writting the result in the ubifs.img file
    mkfs.ubifs -m 512 -e 128KiB -c 100 -r /opt/img ubifs.img
The same, but writting directly to an UBI volume
    mkfs.ubifs -r /opt/img /dev/ubi0_0
Creating an empty UBIFS filesystem on an UBI volume
    mkfs.ubifs /dev/ubi0_0

Options:
-r, -d, --root=DIR       build file system from directory DIR
-m, --min-io-size=SIZE   minimum I/O unit size
-e, --leb-size=SIZE      logical erase block size
-c, --max-leb-cnt=COUNT  maximum logical erase block count
-o, --output=FILE        output to FILE
-j, --jrn-size=SIZE      journal size
-R, --reserved=SIZE      how much space should be reserved for the super-user
-x, --compr=TYPE         compression type - "lzo", "favor_lzo", "zlib"
                         "zstd" or "none" (default: "lzo")
-X, --favor-percent      may only be used with favor LZO compression and defines
                         how many percent better zlib should compress to make
                         mkfs.ubifs use zlib instead of LZO (default 20%)
-f, --fanout=NUM         fanout NUM (default: 8)
-F, --space-fixup        file-system free space has to be fixed up on first mount
                         (requires kernel version 3.0 or greater)
-k, --keyhash=TYPE       key hash type - "r5" or "test" (default: "r5")
-p, --orph-lebs=COUNT    count of erase blocks for orphans (default: 1)
-D, --devtable=FILE      use device table FILE
-U, --squash-uids        squash owners making all files owned by root
-l, --log-lebs=COUNT     count of erase blocks for the log (used only for
                         debugging)
-y, --yes                assume the answer is "yes" for all questions
-v, --verbose            verbose operation
-V, --version            display version information
-g, --debug=LEVEL        display debug information (0 - none, 1 - statistics,
                         2 - files, 3 - more details)
-a, --set-inum-attr      create user.image-inode-number extended attribute on files
                         added to the image. The attribute will contain the inode
                         number the file has in the generated image.
-s, --selinux=FILE       Selinux context file
-K, --key=FILE           load an encryption key from a specified file.
-b, --key-descriptor=HEX specify the key descriptor as a hex string.
-P, --padding=NUM        specify padding policy for encrypting filenames
                         (default = 4).
-C, --cipher=NAME        Specify cipher to use for file level encryption
                         (default is "AES-256-XTS").
-h, --help               display this help text

3. Buildroot中创建ext4、ubifs、cpio镜像

Buildroot支持生成多种镜像格式,常用的用cpio(用于制作ramdisk rootfs)、ext4(多用于SD/EMMC/HD等设备)、ubifs(多用于NAND)。

cpio

选择cpio制作ramdisk文件系统。

-i, --extract Extract files from an archive (run in copy-in mode)

-o, --create Create the archive (run in copy-out mode)

-H, --format=FORMAT,使用指定格式制作cpio文件。newc支持更大的文件系统。

  bin The obsolete binary format. (2147483647 bytes)

  odc The old (POSIX.1) portable format. (8589934591 bytes)

  newc The new (SVR4) portable format, which supports file systems having more than 65536 i-nodes. (4294967295 bytes)

  crc The new (SVR4) portable format with a checksum added.

  tar The old tar format. (8589934591 bytes)

  ustar The POSIX.1 tar format. Also recognizes GNU tar archives, which are similar but not identical. (8589934591 bytes)

  hpbin The obsolete binary format used by HPUX's cpio (which stores device files differently).

  hpodc The portable format used by HPUX's cpio (which stores device files differently).

--quiet Do not print the number of blocks copied

cd output/build/buildroot-fs/cpio/target && find . | LC_ALL=C sort | cpio  --quiet -o -H newc > output/images/rootfs.cpio

ext4

BR2_TARGET_ROOTFS_EXT2_4:表示使用的是mkfs.ext4命令。mkfs.ext4链接到mke2fs。

-d:TARGET_DIR,指定制作文件系统的源文件根目录。

-r:指定要建立的ext2文件系统版本,0或者1。

-N <inode数>:BR2_TARGET_ROOTFS_EXT2_INODES,指定要建立的inode数目。

-m <百分比值>:BR2_TARGET_ROOTFS_EXT2_RESBLKS,指定给管理保留区块的比例,预设为5%。

-L <标签>:BR2_TARGET_ROOTFS_EXT2_LABEL,设置文件系统的标签名称。

<blocks-count>:BR2_TARGET_ROOTFS_EXT2_SIZE,表示制作的文件系统分区大小。

-O:BR2_TARGET_ROOTFS_EXT2_MKFS_OPTIONS,附加的其他创建文件系统的选项。

mkfs.ext4 -d output/build/buildroot-fs/ext2/target -r 1 -N 0 -m 5 -L "rootfs" -O ^64bit output/images/rootfs.ext2 "1000M"

ubifs

mkfs.ubifs创建ubifs文件系统,BR2_TARGET_ROOTFS_UBIFS。

-m:BR2_TARGET_ROOTFS_UBIFS_MINIOSIZE,最小I/O单元大小,一般是页大小,0x800即2048 Bytes。

-e:BR2_TARGET_ROOTFS_UBIFS_LEBSIZE,可擦除逻辑块大小,一般等于(每块页数 -2)*页大小 = (64 -2) * 2048 = 124KB,即0x1f800。对应的块大小为128KB。

-c:BR2_TARGET_ROOTFS_UBIFS_MAXLEBCNT,最多可擦除逻辑块数目,实际上是设置此卷最大容量。比如256MB的容量对应的逻辑块数目 = (256 * 1024) / 128 = 2048。

-x:BR2_TARGET_ROOTFS_UBIFS_RT_LZO,指定运行时压缩算法,默认为lzo。

mkfs.ubifs -d output/build/buildroot-fs/ubifs/target -e 0x1f000 -c 2048 -m 0x800 -x lzo -o output/images/rootfs.ubifs