sparse image

来源:互联网 发布:淘宝客服怎么应聘 编辑:程序博客网 时间:2024/05/18 02:35

前言

在使用make_ext4fs的时候有一个参数为-s,意思是sparse。使用这个参数制作出来的镜像就是sparse image。当时不明白是什么意思,所以查找了一下并记录下来。

简单地来说,sparse image是一种需要多少空间就分配多少的镜像(虚拟机的磁盘镜像文件就是这种镜像,虽然安装系统时候指定了磁盘大小,但占用的磁盘空间并没有那么大,而是随里面的文件大小增大而变大)。关于sparse image更详细的说明请看维基百科:https://en.wikipedia.org/wiki/Sparse_image

sparse image与raw image比较

我们可以用make_ext4fs命令使用-s参数与不使用时生成的镜像文件的比较(即sparse image与raw image的大小比较):

$ make_ext4fs -s -T -1 -l 838860800 -a system ./system_sparse.img ./system$ make_ext4fs -T -1 -l 838860800 -a system ./system_raw.img ./system

其中,system_raw.img是make_ext4fs命令没有使用-s参数生成的镜像文件,而system_sparse.img是make_ext4fs命令使用-s参数生成的镜像文件。
关于make_ext4fs命令的使用在这里不做深究。

(1)du命令不使用–apparent-size参数时得到的是文件占用磁盘空间的实际大小:

$ du -sh system*454M    system_raw.img454M    system_sparse.img

(2)du使用–apparent-size参数查看”明显的大小”,即文件自身的大小

$ du -sh system* --apparent-size 800M    system_raw.img454M    system_sparse.img

(3)看一下文件类型:

$ file system*system_raw.img:  Linux rev 1.0 ext4 filesystem data, UUID=57f8f4bc-abf4-655f-bf67-946fc0f9f25b (extents) (large files)system_sparse.img:   data

其实这里分为raw和sparse两种格式。raw ext4 image,即raw image,使用file可以看到它是一个完整的ext4分区镜像(包含很多全零的无效填充区),可以直接使用mount进行挂载。而sparse image是一个非常普通的data文件,是通过将raw ext4进行稀疏描述得到的,因此尺寸比较小(没有全零的无效填充区)

dd命令制作sparse image

创建sparse image file还可以通过dd命令的seek参数,比如要创建一个10M的虚拟磁盘映像:

$ dd if=/dev/zero of=./test.img bs=1 count=0 seek=10M       0+0 records in0+0 records out0 bytes (0 B) copied, 1.1683e-05 s, 0.0 kB/s

PS:表示先跳到偏移10M的地址,然后创建大小为bs * count的数据块。

看起来的文件大小为:

$ du -sh test.img --apparent-size 10M     test.img

实际占用空间大小为:

$ du -sh test.img0       test.img


参考:

  • 维基百科
  • sparse image file的创建和使用
  • Android中system.img的两种格式及其相互转换方法
0 0