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.

相关文章
|
21天前
|
Linux
Linux下使用ls查看文件颜色全部为白色的解决方法,以及Linux中文件颜色介绍
Linux下使用ls查看文件颜色全部为白色的解决方法,以及Linux中文件颜色介绍
66 2
|
27天前
|
存储 监控 安全
在Linux中,⼀个EXT3的文件分区,当使用touch test.file命令创建⼀个新文件时报错,报错的信息是提示磁盘已满,但是采用df -h命令查看磁盘大小时,只使用了,60%的磁盘空间,为什么会出现这个情况?
在Linux中,⼀个EXT3的文件分区,当使用touch test.file命令创建⼀个新文件时报错,报错的信息是提示磁盘已满,但是采用df -h命令查看磁盘大小时,只使用了,60%的磁盘空间,为什么会出现这个情况?
|
22天前
|
Linux
Linux 服务器下载百度网盘文件
本教程指导如何使用 `bypy` 库从百度网盘下载文件。首先通过 `pip install bypy` 安装库,接着运行 `bypy info` 获取登录链接并完成授权,最后将文件置于指定目录并通过 `bypy downdir /Ziya-13b-v1` 命令下载至本地。
28 1
Linux 服务器下载百度网盘文件
|
26天前
|
缓存 NoSQL Linux
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
|
15天前
|
Ubuntu Linux Shell
Linux系统命令 安装和文件相关命令
本文档详细介绍了Linux系统中的常用命令,包括软件安装卸载命令如`dpkg`和`apt-get`,压缩与解压命令如`gzip`、`bzip2`和`xz`,以及`tar`命令用于打包和解包。此外还介绍了文件分割命令`split`,文件操作命令如`cat`、`head`、`tail`、`more`、`less`等,管道命令和`wc`、`grep`、`find`、`cut`、`sort`、`uniq`、`diff`等实用工具。最后,文档还讲解了文件属性相关的命令如`chmod`、`chown`、`chgrp`以及创建硬链接和软链接的`ln`命令。
|
24天前
|
Linux
linux 删除乱码文件名的文件
【8月更文挑战第26天】当遇到文件名显示为乱码,导致无法正常通过键盘输入文件名进行删除操作时,可以利用鼠标的复制功能配合`rm`命令实现删除。对于文件夹的删除,可使用`rm -rf 目录名`。然而,有时这种方式仍无法删除某些特殊乱码文件,这时可以通过获取文件的i节点号(使用`ls -i`或`ll -i`命令查看)并执行`find -inum [节点号] -delete`来进行删除。这种方法特别适用于处理那些因文件名问题而难以删除的情况。
69 2
|
25天前
|
JSON Linux 网络安全
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
|
27天前
|
机器学习/深度学习 Ubuntu Linux
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
|
27天前
|
监控 安全 Linux
在Linux中,某个账号登陆linux后,系统会在哪些日志文件中记录相关信息?
在Linux中,某个账号登陆linux后,系统会在哪些日志文件中记录相关信息?
|
21天前
|
JavaScript Linux
Linux中和文件相关的操作
Linux中和文件相关的操作
26 0