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

简介: 稀疏(Sparse)文件的创建 在EXT2/EXT3文件系统上可以使用dd创建稀疏文件:$ dd if=/dev/zero of=fs.img bs=1M seek=1024 count=00+0 records in0+0 records out$ ls -lh fs.

稀疏(Sparse)文件的创建

  1. 在EXT2/EXT3文件系统上可以使用dd创建稀疏文件:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif $ dd if=/dev/zero of=fs.img bs=1M seek=1024 count=0
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif0+0 records in
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif0+0 records out
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ ls -lh fs.img
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif-rw-rw-r--  1 zhigang zhigang 1.0G Feb  5 19:50 fs.img
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ du -sh fs.img
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif0       fs.img
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif

  2. 使用C语言来创建一个稀疏文件的方法如下:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif $ cat sparse.c
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif#include 
    sys/types.h>
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif#include 
    sys/stat.h>
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif#include 
    fcntl.h>
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif#include 
    unistd.h>
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    int main(int argc, char *argv[])
    img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
    img_64bd3f76c3bac8e6b320829f254ffa63.gif{
    img_33d02437d135341f0800e3d415312ae8.gif    
    int fd = open("sparse.file", O_RDWR|O_CREAT);
    img_33d02437d135341f0800e3d415312ae8.gif    lseek(fd, 
    1024, SEEK_CUR);
    img_33d02437d135341f0800e3d415312ae8.gif    write(fd, 
    "\0"1);
    img_33d02437d135341f0800e3d415312ae8.gif
    img_33d02437d135341f0800e3d415312ae8.gif    
    return 0;
    img_05dd8d549cff04457a6366b0a7c9352a.gif}

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ gcc 
    -o sparse sparse.c
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ .
    /sparse
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ ls 
    -l sparse.file
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    -r-x--x---  1 zhigang zhigang 1025 Feb  5 23:12 sparse.file
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif]$ du sparse.file
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    4       sparse.file
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif

  3.  使用python来创建一个稀疏文件的方法如下:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif $ cat sparse.py
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    # !/usr/bin/env python
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    =  open( ' fs.img ' ' w ' )
    img_a6339ee3e57d1d52bc7d02b338e15a60.giff.seek(
    1023 )
    img_a6339ee3e57d1d52bc7d02b338e15a60.giff.write(
    ' \n ' )
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ python sparse.py
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ ls 
    - l fs.img
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    - rw - rw - r --    1  zhigang zhigang  1024  Feb   5   20 : 15  fs.img
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ du fs.img
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    4        fs.img
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif


    文件稀疏化(sparsify)

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

    1. cp:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif $ cp  -- sparse = always file file.sparse


    cp缺省使用--sparse=auto,会自动探测源文件中是否有空洞,以决定目标文件是否为稀疏文件;使用--sparse=never会禁止创建稀疏文件。

    2. cpio:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif $ find file  | cpio  - pdmuv  -- sparse  / tmp


    如果不加--sparse参数,稀疏文件中的空洞将被填满。

    3. tar:

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


    如果不加 -S --sparse参数,稀疏文件中的空洞将被填满。

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

    下面我们创建一个500M的稀疏文件,比较一下几种文件稀疏化方法的效率。

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif $ dd if=/dev/zero of=file count=100 bs=1M seek=400
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif100+0 records in
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif100+0 records out
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ time cp --sparse=always file file.sparse
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifreal    0m0.626s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifuser    0m0.205s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifsys     0m0.390s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ time tar cSf - file | (cd /tmp; tar xpSf -)
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifreal    0m2.732s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifuser    0m1.706s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifsys     0m0.915s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ time find file |cpio -pdmuv --sparse /tmp
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif/tmp/file
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif1024000 blocks
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifreal    0m2.763s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifuser    0m1.793s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifsys     0m0.946s
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif


    由此可见,上面几种文件稀疏化的方法中,cp的效率最高;tar和cpio由于使用管道,效率下降。

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

    如何是一个文件系统的映像文件稀疏化?Ron Yorston为大家提供了几种方法,我觉得下面的方法最简单:

    1. 使用Ron Yorston的zerofree将文件系统中未使用的块清零。

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif $ gcc -o zerofree zerofree.c -lext2fs
    img_a6339ee3e57d1d52bc7d02b338e15a60.gif$ ./zerofree fs.img


    2.使用cp命令使映像文件稀疏化:

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


     

    EXT2/EXT3文件系统的sparse_super参数

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

    如下的命令可以查看该参数:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif # echo stats | debugfs /dev/hda2 | grep -i features
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifFilesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file


    或者:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif # tune2fs -l /dev/hda2 |grep "Filesystem features"
    img_a6339ee3e57d1d52bc7d02b338e15a60.gifFilesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file


    可以通过使用:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif # tune2fs -O sparse_super


    或者:

    img_a6339ee3e57d1d52bc7d02b338e15a60.gif # tune2fs -s [0|1]


    来设置该参数。

    参考资料

    1. Keeping filesystem images sparse:

              http://intgat.tigress.co.uk/rmy/uml/sparsify.html.

相关文章
|
22天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
77 1
|
26天前
|
Linux 数据安全/隐私保护 Windows
Linux入门指南:linux权限究竟是什么?和文件有什么关系?
Linux入门指南:linux权限究竟是什么?和文件有什么关系?
30 0
|
28天前
|
算法 Linux C++
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
29 0
|
21天前
|
人工智能 安全 Linux
【Linux】Linux之间如何互传文件(详细讲解)
【Linux】Linux之间如何互传文件(详细讲解)
|
1月前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】Linux 创建Kickstart文件mkkickstart命令 使用指南
【Shell 命令集合 系统设置 】Linux 创建Kickstart文件mkkickstart命令 使用指南
31 0
|
1月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
49 0
|
5天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
12 0
|
7天前
|
Linux Shell 开发工具
Linux文件常用操作
Linux文件常用操作(几乎覆盖所有日常使用)
61 0
|
8天前
|
Linux 内存技术 Perl
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
|
14天前
|
Linux
linux 超过4个G的文件传不上去的解决办法
linux 超过4个G的文件传不上去的解决办法
9 0