我喜欢将整个分区或整个硬盘驱动器克隆到更大的外部磁盘上,但喜欢创建一个稀疏文件。我经常dd用于克隆,但它不支持稀疏文件。作为一种解决方法,我使用了类似的东西:
cp --sparse=always <(dd if=/dev/sda1 bs=8M) /mount/external/backup/sda1.raw
Run Code Online (Sandbox Code Playgroud)
但是,这对我的口味来说有点太棘手,并且如果中止,我将无法恢复该过程。有趣的是,有一个用于此 ( ntfsclone)的 NTFS 工具,但不存在用于 Linux (EXT2-4) 的本机文件系统的此类工具。
是否有一些更好的工具,例如dd具有稀疏支持的变体?我不寻找一些用于磁盘备份的专有软件,而只是想制作一个稀疏克隆副本,如果需要,我可以将其安装为循环设备。
我知道在不使用理解稀疏文件的实用程序的情况下复制或传输最初是稀疏文件的内容将导致填充“漏洞”。有没有一种方法或实用程序可以将曾经的稀疏文件变回稀疏文件?
例如:
创建稀疏文件:
% 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) 以下 Python 代码的控制台等效项是什么:
target = file("disk", "w") # create a file
target.seek(2*1024*1024*1024) # skip to 2 GB
target.write("\0")
target.close()
Run Code Online (Sandbox Code Playgroud)
也许一些dd咒语?这个想法是制作一个表观大小为 2 GB 的文件,用于例如虚拟化。
kvm disk -cd whatever.iso #Only allocate space as necessary
Run Code Online (Sandbox Code Playgroud) 我有一个使用创建的非常大的硬盘的压缩原始图像cat /dev/sdx | xz > image.xz。但是,在此操作之前,驱动器中的可用空间已清零,并且图像大部分由零字节组成。将此图像提取为稀疏文件的最简单方法是什么,以使零块不占用任何空间?
我想创建一个类似于/dev/nullor的特殊文件/dev/random,其中该文件实际上并不存在,但您可以完全相同地从中读取,除了我实际上可以设置文件表观大小的上限。
换句话说,我想创建一个特殊文件(假设我将上限设置为 500GB),当我“cat”文件时,它将输出文件的所有 500GB 然后停止。它需要与实际的 500GB 文件相同,但不占用空间。这个文件的内容无关紧要,它可以是所有\0的 like /dev/null,或者只是一个反复发送的小字符串,或者其他什么。
这是可以做的事情吗?我唯一能找到的远程关闭是关于 的手册页mknod,但那些不是很有帮助。
如何输出实际填充数据的文件标称大小?就像vmtouch显示当前内存中有多少文件......
我希望工作流程是这样的:
$ fallocate -l 1000000 data
$ measure_sparseness data
100%
$ fallocate -p -o 250000 -l 500000 data
$ measure_sparseness
50%
Run Code Online (Sandbox Code Playgroud)
解决方法:使用du -bsh和du -sh并加以比较。
在我的ext4文件系统分区上,我可以运行以下代码:
fs="/mnt/ext4"
#create sparse 100M file on ${fs}
dd if=/dev/zero \
of=${fs}/sparse100M conv=sparse seek=$((100*2*1024-1)) count=1 2> /dev/null
#show its actual used size before
echo "Before:"
ls ${fs}/sparse100M -s
#setting the sparse file up as loopback and run md5sum on loopback
losetup /dev/loop0 ${fs}/sparse100M
md5sum /dev/loop0
#show its actual used size afterwards
echo "After:"
ls ${fs}/sparse100M -s
#release loopback and remove file
losetup -d /dev/loop0
rm ${fs}/sparse100M
Run Code Online (Sandbox Code Playgroud)
这产生
Before:
0 sparse100M
2f282b84e7e608d5852449ed940bfc51 /dev/loop0
After:
0 sparse100M
Run Code Online (Sandbox Code Playgroud)
在 tmpfs 上做同样的事情: …
是否有任何理由不在--sparse=always每次调用时使用 use cp?
info cp 说:
Run Code Online (Sandbox Code Playgroud)‘--sparse=WHEN’ A “sparse file” contains “holes”—a sequence of zero bytes that does not occupy any physical disk blocks; the ‘read’ system call reads these as zeros. This can both save considerable disk space and increase speed, since many binary files contain lots of consecutive zero bytes. By default, ‘cp’ detects holes in input source files via a crude heuristic and makes the corresponding output file sparse as well. Only regular …
我有一个稀疏文件,其中只分配了一些块:
~% du -h --apparent-size example
100K example
~% du -h example
52K example
Run Code Online (Sandbox Code Playgroud)
我想知道实际分配了文件的哪些块。是否有系统调用或内核接口可用于获取分配列表或文件漏洞列表?
简单地检查足够长的零字符串(GNU cp、rsync 等使用的方法)无法正常工作:
~% cp example example1
~% du -h example1
32K example1
Run Code Online (Sandbox Code Playgroud)
它检测到实际分配的其他零序列。
我读过一些问题,询问如何rsync有效地稀疏文件提及文件/var/log/lastlog和/var/log/faillog. 事实上,我自己也发现这些文件是一个“问题”,因为它们通过 rsync 备份会使它们变得“稀疏”。
因此,我想知道的是,将这些文件作为稀疏的大文件(在我的情况下为 1.1TB)的需求/背景动机是什么?
与此相关的还有后续行动:由于我假设它们是日志文件,因此我并不在乎我过多地截断了这些文件,我是否因截断这些文件而损坏了任何内容?
sparse-files ×10
files ×5
dd ×2
cloning ×1
compression ×1
coreutils ×1
cp ×1
disk-image ×1
ext4 ×1
linux ×1
logs ×1
tmpfs ×1
utilities ×1