天天看點

Linux對稀疏(Sparse)檔案的支援

稀疏(Sparse)檔案的建立

在EXT2/EXT3檔案系統上可以使用dd建立稀疏檔案:

Linux對稀疏(Sparse)檔案的支援

$ dd if=/dev/zero of=fs.img bs=1M seek=1024 count=0

Linux對稀疏(Sparse)檔案的支援

0+0 records in

Linux對稀疏(Sparse)檔案的支援

0+0 records out

Linux對稀疏(Sparse)檔案的支援

$ ls -lh fs.img

Linux對稀疏(Sparse)檔案的支援

-rw-rw-r--  1 zhigang zhigang 1.0G Feb  5 19:50 fs.img

Linux對稀疏(Sparse)檔案的支援

$ du -sh fs.img

Linux對稀疏(Sparse)檔案的支援

0       fs.img

Linux對稀疏(Sparse)檔案的支援

使用C語言來建立一個稀疏檔案的方法如下:

Linux對稀疏(Sparse)檔案的支援

$ cat sparse.c

Linux對稀疏(Sparse)檔案的支援

#include sys/types.h>

Linux對稀疏(Sparse)檔案的支援

#include sys/stat.h>

Linux對稀疏(Sparse)檔案的支援

#include fcntl.h>

Linux對稀疏(Sparse)檔案的支援

#include unistd.h>

Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援

int main(int argc, char *argv[])

Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援

{

Linux對稀疏(Sparse)檔案的支援

    int fd = open("sparse.file", O_RDWR|O_CREAT);

Linux對稀疏(Sparse)檔案的支援

    lseek(fd, 1024, SEEK_CUR);

Linux對稀疏(Sparse)檔案的支援

    write(fd, "\0", 1);

Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援

    return 0;

Linux對稀疏(Sparse)檔案的支援

}

Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援

$ gcc -o sparse sparse.c

Linux對稀疏(Sparse)檔案的支援

$ ./sparse

Linux對稀疏(Sparse)檔案的支援

$ ls -l sparse.file

Linux對稀疏(Sparse)檔案的支援

-r-x--x---  1 zhigang zhigang 1025 Feb  5 23:12 sparse.file

Linux對稀疏(Sparse)檔案的支援

]$ du sparse.file

Linux對稀疏(Sparse)檔案的支援

4       sparse.file

Linux對稀疏(Sparse)檔案的支援

 使用python來建立一個稀疏檔案的方法如下:

Linux對稀疏(Sparse)檔案的支援

$ cat sparse.py

Linux對稀疏(Sparse)檔案的支援

#!/usr/bin/env python

Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援

f = open('fs.img', 'w')

Linux對稀疏(Sparse)檔案的支援

f.seek(1023)

Linux對稀疏(Sparse)檔案的支援

f.write('\n')

Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援

$ python sparse.py

Linux對稀疏(Sparse)檔案的支援

$ ls -l fs.img

Linux對稀疏(Sparse)檔案的支援

-rw-rw-r--  1 zhigang zhigang 1024 Feb  5 20:15 fs.img

Linux對稀疏(Sparse)檔案的支援

$ du fs.img

Linux對稀疏(Sparse)檔案的支援

4       fs.img

Linux對稀疏(Sparse)檔案的支援

檔案稀疏化(sparsify)

下面的方法都可以将一個檔案稀疏化。

1. cp:

Linux對稀疏(Sparse)檔案的支援

$ cp --sparse=always file file.sparse

cp預設使用--sparse=auto,會自動探測源檔案中是否有空洞,以決定目标檔案是否為稀疏檔案;使用--sparse=never會禁止建立稀疏檔案。

2. cpio:

Linux對稀疏(Sparse)檔案的支援

$ find file |cpio -pdmuv --sparse /tmp

如果不加--sparse參數,稀疏檔案中的空洞将被填滿。

3. tar:

Linux對稀疏(Sparse)檔案的支援

$ tar cSf - file | (cd /tmp/tt; tar xpSf -)

Linux對稀疏(Sparse)檔案的支援

如果不加 -S --sparse參數,稀疏檔案中的空洞将被填滿。

檔案稀疏化(sparsify)效率比較

下面我們建立一個500M的稀疏檔案,比較一下幾種檔案稀疏化方法的效率。

Linux對稀疏(Sparse)檔案的支援

$ dd if=/dev/zero of=file count=100 bs=1M seek=400

Linux對稀疏(Sparse)檔案的支援

100+0 records in

Linux對稀疏(Sparse)檔案的支援

100+0 records out

Linux對稀疏(Sparse)檔案的支援

$ time cp --sparse=always file file.sparse

Linux對稀疏(Sparse)檔案的支援

real    0m0.626s

Linux對稀疏(Sparse)檔案的支援

user    0m0.205s

Linux對稀疏(Sparse)檔案的支援

sys     0m0.390s

Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援

$ time tar cSf - file | (cd /tmp; tar xpSf -)

Linux對稀疏(Sparse)檔案的支援

real    0m2.732s

Linux對稀疏(Sparse)檔案的支援

user    0m1.706s

Linux對稀疏(Sparse)檔案的支援

sys     0m0.915s

Linux對稀疏(Sparse)檔案的支援
Linux對稀疏(Sparse)檔案的支援

$ time find file |cpio -pdmuv --sparse /tmp

Linux對稀疏(Sparse)檔案的支援

/tmp/file

Linux對稀疏(Sparse)檔案的支援

1024000 blocks

Linux對稀疏(Sparse)檔案的支援

real    0m2.763s

Linux對稀疏(Sparse)檔案的支援

user    0m1.793s

Linux對稀疏(Sparse)檔案的支援

sys     0m0.946s

Linux對稀疏(Sparse)檔案的支援

由此可見,上面幾種檔案稀疏化的方法中,cp的效率最高;tar和cpio由于使用管道,效率下降。

使EXT2/EXT3檔案系統稀疏化(sparsify)

Linux對稀疏(Sparse)檔案的支援

$ gcc -o zerofree zerofree.c -lext2fs

Linux對稀疏(Sparse)檔案的支援

$ ./zerofree fs.img

2.使用cp指令使映像檔案稀疏化:

Linux對稀疏(Sparse)檔案的支援

$ cp --sparse=always fs.img fs_sparse.img

EXT2/EXT3檔案系統的sparse_super參數

這個參數與EXT2/EXT3是否支援Sparse檔案無關;當打開該參數時,檔案系統将使用更少的超級塊(Super block)備份,以節省空間。

如下的指令可以檢視該參數:

Linux對稀疏(Sparse)檔案的支援

# echo stats | debugfs /dev/hda2 | grep -i features

Linux對稀疏(Sparse)檔案的支援

Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file

或者:

Linux對稀疏(Sparse)檔案的支援

# tune2fs -l /dev/hda2 |grep "Filesystem features"

Linux對稀疏(Sparse)檔案的支援

可以通過使用:

Linux對稀疏(Sparse)檔案的支援

# tune2fs -O sparse_super

Linux對稀疏(Sparse)檔案的支援

# tune2fs -s [0|1]

來設定該參數。

參考資料

Keeping filesystem images sparse: