LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

squashfs配置和使用

Squashfs的设计是专门为一般的只读文件系统的使用而设计,它可应用于数据备份,或是系统资源紧张的电脑上使用。

  • 最初版本的Squashfs采用 gzip 的数据压缩。版本 2.6.34 之后的Linux内核增加了对 LZMA[1] 和 LZO [2]压缩算法的支持,版本 2.6.38 的内核增加了对LZMA2的支持,该算法同时也是xz使用的压缩算法。
  • squashfs 系统支持以回环(loopback)的方式挂载,然后便可以访问其上的文件了,在访问这些文件时,它们就会被解压缩并装载在 RAM 中,而不需要将整个文件解压缩后才去访问其中的文件,这样一来访问速度就快多了。

1 内核squashfs相关配置

1.1 kernel配置

Linux中打开squashfs功能:

File systems
  ->Miscellaneous filesystems
    ->SquashFS 4.0 - Squashed file system support
      ->File decompression options (Decompress file data into an intermediate buffer)
      ->Decompressor parallelisation options (Single threaded compression)
      ->Squashfs XATTR support
      ->Include support for ZLIB compressed file systems
      ->Include support for LZ4 compressed file systems
      ->Include support for LZO compressed file systems
      ->Include support for XZ compressed file systems
      ->Include support for ZSTD compressed file systems
      ->Use 4K device block size?
      ->Additional option for memory-constrained systems

1.2 作为rootfs挂载

 在bootargs上增加如下命令:

root=/dev/mtdblock20 ro rootfstype=squashfs rootwait 

通过mount命令加载squashfs:

mount -t squashfs /dev/mtdblock20 /rootfs

2 Buildroot生成squashfs镜像

 Buildroot配置如下:

Filesystem images
  squashfs root filesystem
    block size (128k)
    pad to a 4K boundary
    Compression algorithm (gzip)
    extreme compression when available

使能squashfs后编译mksquashfs工具,使用方法如下:

SYNTAX:./output/host/bin/mksquashfs source1 source2 ...  dest [options] [-e list of exclude
dirs/files]

Filesystem build options:
-tar            read uncompressed tar file from standard in (stdin)
-no-strip        act like tar, and do not strip leading directories
            from source files
-tarstyle        alternative name for -no-strip
-cpiostyle        act like cpio, and read files from standard in (stdin)
-cpiostyle0        like -cpiostyle, but filenames are null terminated
-comp <comp>        select <comp> compression--指定压缩算法。
            Compressors available:
                gzip (default)
                lzma
                lzo
                lz4
                xz
                zstd
-b <block_size>        set data block to <block_size>.  Default 128 Kbytes--指定block大小。
            Optionally a suffix of K or M can be given to specify
            Kbytes or Mbytes respectively
...
Filesystem filter options:
...
Filesystem append options:
-noappend        do not append to existing filesystem--创建全新文件系统。
-root-becomes <name>    when appending source files/directories, make the
            original root become a subdirectory in the new root
            called <name>, rather than adding the new source items
            to the original root

Mksquashfs runtime options:
-version        print version, licence and copyright message
-exit-on-error        treat normally ignored errors as fatal
-recover <name>        recover filesystem data using recovery file <name>
-no-recovery        don't generate a recovery file
-recovery-path <name>    use <name> as the directory to store the recovery file
-quiet            no verbose output
-info            print files written to filesystem
-no-progress        don't display the progress bar
-progress        display progress bar when using the -info option
-throttle <percentage>    throttle the I/O input rate by the given percentage.
            This can be used to reduce the I/O and CPU consumption
            of Mksquashfs
-limit <percentage>    limit the I/O input rate to the given percentage.
            This can be used to reduce the I/O and CPU consumption
            of Mksquashfs (alternative to -throttle)
-processors <number>    Use <number> processors.  By default will use number of--工作时可使用的的核数。
            processors available
-mem <size>        Use <size> physical memory.  Currently set to 969M
            Optionally a suffix of K, M or G can be given to specify
            Kbytes, Mbytes or Gbytes respectively

Miscellaneous options:
...
Compressors available and compressor specific options:--压缩算法的子选项,提供更精细的配置。
    gzip (default)
      -Xcompression-level <compression-level>
        <compression-level> should be 1 .. 9 (default 9)
      -Xwindow-size <window-size>
        <window-size> should be 8 .. 15 (default 15)
      -Xstrategy strategy1,strategy2,...,strategyN
        Compress using strategy1,strategy2,...,strategyN in turn
        and choose the best compression.
        Available strategies: default, filtered, huffman_only,
        run_length_encoded and fixed
    lzma (no options)
    lzo
      -Xalgorithm <algorithm>
        Where <algorithm> is one of:
            lzo1x_1
            lzo1x_1_11
            lzo1x_1_12
            lzo1x_1_15
            lzo1x_999 (default)
      -Xcompression-level <compression-level>
        <compression-level> should be 1 .. 9 (default 8)
        Only applies to lzo1x_999 algorithm
    lz4
      -Xhc
        Compress using LZ4 High Compression
    xz
      -Xbcj filter1,filter2,...,filterN
        Compress using filter1,filter2,...,filterN in turn
        (in addition to no filter), and choose the best compression.
        Available filters: x86, arm, armthumb, powerpc, sparc, ia64
      -Xdict-size <dict-size>
        Use <dict-size> as the XZ dictionary size.  The dictionary size
        can be specified as a percentage of the block size, or as an
        absolute value.  The dictionary size must be less than or equal
        to the block size and 8192 bytes or larger.  It must also be
        storable in the xz header as either 2^n or as 2^n+2^(n+1).
        Example dict-sizes are 75%, 50%, 37.5%, 25%, or 32K, 16K, 8K
        etc.
    zstd
      -Xcompression-level <compression-level>
        <compression-level> should be 1 .. 22 (default 15)

在fs/squashfs/squashfs.mk中定义了生成squashfs镜像流程:

ROOTFS_SQUASHFS_ARGS = \
    -noappend \
    -processors $(PARALLEL_JOBS) \
    -b $(call qstrip,$(BR2_TARGET_ROOTFS_SQUASHFS_BS)) \
    $(call qstrip,$(BR2_TARGET_ROOTFS_SQUASHFS_COMP_OPTS))

ifeq ($(BR2_TARGET_ROOTFS_SQUASHFS_PAD),)
ROOTFS_SQUASHFS_ARGS += -nopad
endif

ifeq ($(BR2_TARGET_ROOTFS_SQUASHFS4_LZ4),y)
ROOTFS_SQUASHFS_ARGS += -comp lz4
else ifeq ($(BR2_TARGET_ROOTFS_SQUASHFS4_LZO),y)
ROOTFS_SQUASHFS_ARGS += -comp lzo
else ifeq ($(BR2_TARGET_ROOTFS_SQUASHFS4_LZMA),y)
ROOTFS_SQUASHFS_ARGS += -comp lzma
else ifeq ($(BR2_TARGET_ROOTFS_SQUASHFS4_XZ),y)
ROOTFS_SQUASHFS_ARGS += -comp xz
else ifeq ($(BR2_TARGET_ROOTFS_SQUASHFS4_ZSTD),y)
ROOTFS_SQUASHFS_ARGS += -comp zstd
else
ROOTFS_SQUASHFS_ARGS += -comp gzip
endif

define ROOTFS_SQUASHFS_CMD
    $(HOST_DIR)/bin/mksquashfs $(TARGET_DIR) $@ $(ROOTFS_SQUASHFS_ARGS)
endef

参考文档:《[OpenWrt Wiki] The OpenWrt Flash Layout》、《Squash Fs Comparisons - eLinux.org》、《Understanding SquashFS and How to Mount a SquashFS Filesystem | Baeldung on Linux》。

posted on 2024-05-10 23:59  ArnoldLu  阅读(1157)  评论(0编辑  收藏  举报

导航