当前位置: 首页 > 工具软件 > CramFS > 使用案例 >

只读压缩文件系统cramfs,squashfs挂载读写rw,解包编辑及打包

施飞雨
2023-12-01

cramfs

目标:挂载cramfs为rw,可读可写,解包cramfs,重新打包cramfs

  1. file命令查看一下文件信息: /home/susu/mtd2: Linux Compressed ROM File System data, little endian size 9654272 version #2 sorted_dirs CRC 0x352b4d9b, edition 0, 5834 blocks, 818 files
  2. 假设需要挂载到的临时目录:/mnt/test,需要确保该目录存在,即在/mnt目录下有一个叫test的空文件夹
  3. 挂载镜像文件~/mtd2/mnt/testsudo mount ~/mtd2 /mnt/test
  4. 此时/mnt/test是只读的,需要把文件复制出来做编辑
  5. 新建一个用于编辑镜像的临时文件夹~/cram
  6. 拷贝所有文件到用于编辑的临时目录~/cramsudo cp -r /mnt/test/* ~/cram
  7. 此时在~/cram目录中做编辑操作就可以,比如添加文件删除文件。我是在里面添加了一个文件。
  8. 最后重新打包编辑后的目录~/cram为镜像~/mtd2newsudo mkfs.cramfs -b 4096 ~/cram ~/mtd2new
  9. 再来看一下文件信息:/home/susu/mtd2new: Linux Compressed ROM File System data, little endian size 9666560 version #2 sorted_dirs CRC 0xa37105a6, edition 0, 5835 blocks, 819 files

有一个问题就是mkfs.cramfs的时候有一个参数-b,指定blocksize,目前还不太懂,好像是默认是用PAGE_CACHE_SIZE作为blocksize(参考1 - patch参考2 - fs-cramfs-readme)。获取PAGESIZE的方法:
第一种:

$ getconf PAGESIZE
4096

第二种:

$ grep -i pagesize /proc/1/smaps
KernelPageSize:        4 kB
MMUPageSize:           4 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB

squashfs

目标编辑squashfs文件系统镜像,解包squashfs文件系统并重新打包

  1. 分别用file命令和unsquashfs -s命令查看文件信息:
$ file mtd2 
mtd2: Squashfs filesystem, little endian, version 4.0, 14144357 bytes, 1122 inodes, blocksize: 131072 bytes, created: Fri Apr 14 06:43:01 2017
$ unsquashfs -s mtd2 
Found a valid SQUASHFS 4:0 superblock on mtd2.
Creation or last append time Fri Apr 14 14:43:01 2017
Filesystem size 13812.85 Kbytes (13.49 Mbytes)
Compression lzo
Block size 131072
Filesystem is exportable via NFS
Inodes are compressed
Data is compressed
Fragments are not stored
Xattrs are compressed
Duplicates are removed
Number of fragments 0
Number of inodes 1122
Number of ids 1

可以看到压缩方式lzo,blocksize131072,参数-comp lzo -b 131072 -no-fragments(这部分可以对照mksquashfs -h帮助分析)
2. 解包命令:unsquashfs mtd2 会解包到当前目录下的squashfs-root文件夹
3. 在squashfs-root文件夹中做需要的修改,我是添加了两个文件进去
4. 打包命令:mksquashfs ./squashfs-root mtd2new -comp lzo -b 131072 -no-fragments
5. 打包前后文件信息对比:

$ unsquashfs -s mtd2
Found a valid SQUASHFS 4:0 superblock on mtd2.
Creation or last append time Fri Apr 14 14:43:01 2017
Filesystem size 13812.85 Kbytes (13.49 Mbytes)
Compression lzo
Block size 131072
Filesystem is exportable via NFS
Inodes are compressed
Data is compressed
Fragments are not stored
Xattrs are compressed
Duplicates are removed
Number of fragments 0
Number of inodes 1122
Number of ids 1
$ unsquashfs -s mtd2new 
Found a valid SQUASHFS 4:0 superblock on mtd2new.
Creation or last append time Wed Mar 10 16:41:48 2021
Filesystem size 13812.81 Kbytes (13.49 Mbytes)
Compression lzo
Block size 131072
Filesystem is exportable via NFS
Inodes are compressed
Data is compressed
Fragments are not stored
Xattrs are compressed
Duplicates are removed
Number of fragments 0
Number of inodes 1124
Number of ids 1
 类似资料: