Zhigang Wang
  • Home
  • Blog
  • Wiki
  • LDP
  • Planet

Linux对稀疏(Sparse)文件的支持

Last modified on 2009-12-01

稀疏(Sparse)文件可以节省大量的磁盘空间,目前很多文件系统都支持。下面介绍了Linux对稀疏(Sparse)文件的支持。

稀疏(Sparse)文件的创建

  1. 在EXT2/EXT3文件系统上可以使用dd创建稀疏文件:
    $ dd if=/dev/zero of=fs.img bs=1M seek=1024 count=0
    0+0 records in
    0+0 records out
    $ ls -lh fs.img
    -rw-rw-r--  1 zhigang zhigang 1.0G Feb  5 19:50 fs.img
    $ du -sh fs.img
    0       fs.img
    
  2. 使用C语言来创建一个稀疏文件的方法如下:
    $ cat sparse.c
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
        int fd = open("sparse.file", O_RDWR|O_CREAT);
        lseek(fd, 1024, SEEK_CUR);
        write(fd, "\0", 1);
    
        return 0;
    }
    
    $ gcc -o sparse sparse.c
    $ ./sparse
    $ ls -l sparse.file
    -r-x--x---  1 zhigang zhigang 1025 Feb  5 23:12 sparse.file
    $ du sparse.file
    4       sparse.file
    
  3. 使用python来创建一个稀疏文件的方法如下:
    $ cat sparse.py
    #!/usr/bin/env python
    
    f = open('fs.img', 'w')
    f.seek(1023)
    f.write('\n')
    
    $ python sparse.py
    $ ls -l fs.img
    -rw-rw-r--  1 zhigang zhigang 1024 Feb  5 20:15 fs.img
    $ du fs.img
    4       fs.img
    

文件稀疏化(sparsify)

下面的方法都可以将一个文件稀疏化。

  1. cp:
    $ cp --sparse=always file file.sparse
    
    cp缺省使用--sparse=auto,会自动探测源文件中是否有空洞,以决定目标文件是否为稀疏文件;使用--sparse=never会禁止创建稀疏文件。
  2. cpio:
    $ find file |cpio -pdmuv --sparse /tmp
    
    如果不加--sparse参数,稀疏文件中的空洞将被填满。
  3. tar:
    $ tar cSf - file | (cd /tmp/tt; tar xpSf -)
    
    如果不加 -S --sparse参数,稀疏文件中的空洞将被填满。

文件稀疏化(sparsify)效率比较

下面我们创建一个500M的稀疏文件,比较一下几种文件稀疏化方法的效率。
$ dd if=/dev/zero of=file count=100 bs=1M seek=400
100+0 records in
100+0 records out
$ time cp --sparse=always file file.sparse
real    0m0.626s
user    0m0.205s
sys     0m0.390s

$ time tar cSf - file | (cd /tmp; tar xpSf -)
real    0m2.732s
user    0m1.706s
sys     0m0.915s

$ time find file |cpio -pdmuv --sparse /tmp
/tmp/file
1024000 blocks
real    0m2.763s
user    0m1.793s
sys     0m0.946s
由此可见,上面几种文件稀疏化的方法中,cp的效率最高;tar和cpio由于使用管道,效率下降。

使EXT2/EXT3文件系统稀疏化(sparsify)

如何是一个文件系统的映像文件稀疏化?Ron Yorston为大家提供了几种方法,我觉得下面的方法最简单:
  1. 使用Ron Yorston的zerofree将文件系统中未使用的块清零。
    $ gcc -o zerofree zerofree.c -lext2fs
    $ ./zerofree fs.img
    
  2. 使用cp命令使映像文件稀疏化:
    $ cp --sparse=always fs.img fs_sparse.img
    

EXT2/EXT3文件系统的sparse_super参数

这个参数与EXT2/EXT3是否支持Sparse文件无关;当打开该参数时,文件系统将使用更少的超级块(Super block)备份,以节省空间。

如下的命令可以查看该参数:
# echo stats | debugfs /dev/hda2 | grep -i features
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file
或者:
# tune2fs -l /dev/hda2 |grep "Filesystem features"
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file
可以通过使用:
# tune2fs -O sparse_super
或者:
# tune2fs -s [0|1]
来设置该参数。

参考资料

  1. Keeping filesystem images sparse: http://intgat.tigress.co.uk/rmy/uml/sparsify.html.
 

Categories

  • All contents
  • English contents
  • Chinese contents

Feeds

  • AtomAll contents
  • AtomEnglish contents
  • AtomChinese contents

Tags

  • gtd
  • syslog
  • twiki
  • virtualizaion
  • wiki
  • xen

Copyright © 2012 Zhigang Wang. Some right reserved.

The views expressed on this web site are my own and do not necessarily reflect the views of Oracle.