use*_*849 32 utilities sparse-files files
我知道在不使用理解稀疏文件的实用程序的情况下复制或传输最初是稀疏文件的内容将导致填充“漏洞”。有没有一种方法或实用程序可以将曾经的稀疏文件变回稀疏文件?
例如:
创建稀疏文件:
% dd if=/dev/zero of=TEST bs=1 count=0 seek=1G
# do some op that pads out the holes
% scp TEST localhost:~/TEST2
% ls -lhs TEST*
0 -rw-rw-r--. 1 tony tony 1.0G Oct 16 13:35 TEST
1.1G -rw-rw-r--. 1 tony tony 1.0G Oct 16 13:37 TEST2
Run Code Online (Sandbox Code Playgroud)
有没有办法:
% resparse TEST2
to get:
0 -rw-rw-r--. 1 tony tony 1.0G Oct 16 13:35 TEST
0G -rw-rw-r--. 1 tony tony 1.0G Oct 16 13:37 TEST2
Run Code Online (Sandbox Code Playgroud)
Jim*_*ris 31
从 util-linux 2.25 开始,fallocateLinux 上的实用程序有一个-d/--dig-hole选项。
fallocate -d the-file
Run Code Online (Sandbox Code Playgroud)
将挖掘每满的块中的孔零文件中
在较旧的系统上,您可以手动完成:
Linux 有一个FALLOC_FL_PUNCH_HOLE选项fallocate可以做到这一点。我在 github 上找到了一个带有示例的脚本:
使用 Python 中的 FALLOC_FL_PUNCH_HOLE
我对它做了一些修改以执行您的要求 - 在填充零的文件区域中打孔。这里是:
使用 Python 中的 FALLOC_FL_PUNCH_HOLE 在文件中打孔
usage: punch.py [-h] [-v VERBOSE] FILE [FILE ...]
Punch out the empty areas in a file, making it sparse
positional arguments:
FILE file(s) to modify in-place
optional arguments:
-h, --help show this help message and exit
-v VERBOSE, --verbose VERBOSE
be verbose
Run Code Online (Sandbox Code Playgroud)
例子:
# create a file with some data, a hole, and some more data
$ dd if=/dev/urandom of=test1 bs=4096 count=1 seek=0
$ dd if=/dev/urandom of=test1 bs=4096 count=1 seek=2
# see that it has holes
$ du --block-size=1 --apparent-size test1
12288 test1
$ du --block-size=1 test1
8192 test1
# copy it, ignoring the hole
$ cat test1 > test2
$ du --block-size=1 --apparent-size test2
12288 test2
$ du --block-size=1 test2
12288 test2
# punch holes again
$ ./punch.py test2
$ du --block-size=1 --apparent-size test2
12288 test2
$ du --block-size=1 test2
8192 test2
# verify
$ cmp test1 test2 && echo "files are the same"
files are the same
Run Code Online (Sandbox Code Playgroud)
请注意,punch.py只找到 4096 字节的块进行打孔,因此它可能不会像开始时那样使文件完全稀疏。当然,它可以变得更智能。此外,它只是经过轻微测试,所以在信任它之前要小心并进行备份!
bah*_*mat 10
如果要使文件稀疏,可以直接使用dd.
dd if=./zeropadded.iso of=./isnowsparse.iso conv=sparse
Run Code Online (Sandbox Code Playgroud)
从dd(1)手册:
sparse If one or more output blocks would consist solely of
NUL bytes, try to seek the output file by the required
space instead of filling them with NULs, resulting in a
sparse file.
Run Code Online (Sandbox Code Playgroud)
因此,请注意,只有当整个块为空时,它才会向前搜索。对于最大稀疏使用bs=1.
缺少tar-ing 它与一个-S标志(假设 GNU tar),并重新执行scp...不。据我所知,没有任何实用程序能够知道“漏洞”在哪里。