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.giff 
    =  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.

相关文章
|
Linux 开发工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
1106 0
7种比较Linux中文本文件的最佳工具
|
存储 数据管理 Linux
区分Linux中.tar文件与.tar.gz文件的不同。
总之,".tar"文件提供了一种方便的文件整理方式,其归档但不压缩的特点适用于快速打包和解压,而".tar.gz"文件通过额外的压缩步骤,尽管处理时间更长,但可以减小文件尺寸,更适合于需要节约存储空间或进行文件传输的场景。用户在选择时应根据具体需求,考虑两种格式各自的优劣。
2511 13
|
Linux
【Linux】 Linux文件I/O常见操作技巧
以上就是Linux文件I/O操作的一些技巧,接纳它们,让它们成为你在Linux世界中的得力伙伴,工作会变得轻松许多。不过记住,技巧的运用也需要根据实际情况灵活掌握,毕竟,最适合的才是最好的。
452 28
|
安全 Linux
Linux赋予文件000权限的恢复技巧
以上这些步骤就像是打开一扇锁住的门,步骤看似简单,但是背后却有着严格的逻辑和规则。切记,在任何时候,变更文件权限都要考虑安全性,不要无谓地放宽权限,那样可能
558 16
|
存储 Linux 数据处理
深入剖析Linux中一切即文件的哲学和重定向的机制
在计算机的奇妙世界中,Linux的这套哲学和机制减少了不同类型资源的处理方式,简化了抽象的概念,并蕴藏着强大的灵活性。就像变戏法一样,轻轻松松地在文件、程序与设备之间转换数据流,标准输入、输出、错误流就在指尖舞动,程序的交互和数据处理因此变得既高效又富有乐趣。
333 4
|
Ubuntu Linux
"unzip"命令解析:Linux下如何处理压缩文件。
总的来说,`unzip`命令是Linux系统下一款实用而方便的ZIP格式文件处理工具。本文通过简明扼要的方式,详细介绍了在各类Linux发行版上安装 `unzip`的方法,以及如何使用 `unzip`命令进行解压、查看和测试ZIP文件。希望本文章能为用户带来实际帮助,提高日常操作的效率。
4421 12
|
Linux Shell
Linux系统下快速批量创建和删除文件的方法
总的来说,使用shell脚本来批量处理文件是一种非常强大的工具,只要你愿意花时间学习和实践,你会发现它能大大提高你的工作效率。
958 19
|
Linux
Linux 常用文件查看命令
`cat` 命令用于连接文件并打印到标准输出,适用于快速查看和合并文本文件内容。常用示例包括:`cat file1.txt` 查看单个文件,`cat file1.txt file2.txt` 合并多个文件,`cat > filename` 创建新文件,`cat >> filename` 追加内容。`more` 和 `less` 命令用于分页查看文件,`tail` 命令则用于查看文件末尾内容,支持实时追踪日志更新,如 `tail -f file.log`。
1094 5
Linux 常用文件查看命令
|
存储 网络协议 Linux
【Linux】进程IO|系统调用|open|write|文件描述符fd|封装|理解一切皆文件
本文详细介绍了Linux中的进程IO与系统调用,包括 `open`、`write`、`read`和 `close`函数及其用法,解释了文件描述符(fd)的概念,并深入探讨了Linux中的“一切皆文件”思想。这种设计极大地简化了系统编程,使得处理不同类型的IO设备变得更加一致和简单。通过本文的学习,您应该能够更好地理解和应用Linux中的进程IO操作,提高系统编程的效率和能力。
916 34
|
监控 Linux
Linux基础:文件和目录类命令分析。
总的来说,这些基础命令,像是Linux中藏匿的小矮人,每一次我们使用他们,他们就把我们的指令准确的传递给Linux,让我们的指令变为现实。所以,现在就开始你的Linux之旅,挥动你的命令之剑,探索这个充满神秘而又奇妙的世界吧!
382 19