如果我们使用 fallocate 创建交换文件,我们会被告知该文件包含漏洞,因此操作将停止。
这些洞是什么?
或者
测试
fallocate -l 100MB /swap
chmod 600 /swap
mkswap /swap
swapon /swap
Run Code Online (Sandbox Code Playgroud)
输出
swapon: swapfile has holes
swapon: /swap: swapon failed: Invalid argument
Run Code Online (Sandbox Code Playgroud) 我完全理解这--dig-holes会就地创建一个稀疏文件。也就是说,如果文件有孔--dig-holes选项会删除这些孔:
让我们以一种非常简化的方式来看待它,假设我们有一个名为 non-sparse 的巨大文件:
非稀疏:
aaaaaaaaaaaaaaaaaaaaaaaaaaaa
\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00
bbbbbbbbbbbbbbbbbbbbbbbbbbbb
\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00
cccccccccccccccccccccccccccc
Run Code Online (Sandbox Code Playgroud)
非稀疏中有许多零,假设交错零以千兆字节为单位。fallocate --dig-holes取消分配可用于零(空洞)的空间,其中实际文件大小保持不变(保留)。
现在,--punch-hole它到底有什么作用?我看了手册页,还是不明白:
-p, --punch-hole
Deallocates space (i.e., creates a hole) in the byte range
starting at offset and continuing for length bytes. Within
the specified range, partial filesystem blocks are zeroed,
and whole filesystem blocks are removed from the file.
After a successful call, subsequent reads from this range
will return zeroes.
Run Code Online (Sandbox Code Playgroud)
挖洞,--dig-hole好像是那个选项的反面,怎么挖洞跟挖洞不一样?!帮助!我们需要一个逻辑学家:)。
这两个选项的命名在语言上是同义词,这可能会造成混淆。
--dig-holes …
所以我的设置是这样的。
$ truncate -s 1T volume
$ losetup -f --show volume
/dev/loop0
$ mkfs.ext4 /dev/loop0
$ ls -sh volume
1.1G volume
$ mount /dev/loop0 /mnt/loop
Run Code Online (Sandbox Code Playgroud)
现在我有 1.1TB 的卷,正如预期的那样。ext4 的开销将稀疏文件扩展至 1.1G,但这没关系。现在添加一个文件。
$ dd if=/dev/urandom of=/mnt/loop/file bs=1M count=10240
$ ls -sh volume
12G volume
Run Code Online (Sandbox Code Playgroud)
很好,现在我不想要这个文件了。
$ rm /mnt/loop/file
$ ls -sh volume
12G volume
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,可用空间仍然占用空间,并$ fallocate -d volume释放了 1GB。
我的问题是,如何在不将卷扩展到完整大小的情况下将此处的可用空间清零?$ dd if=/dev/zero会将其扩展至完整大小,并conv=sparse使其在卷内创建一个无用的稀疏文件。
TL;DR:有没有一种方法可以losetup忽略空块对空扇区的写入,同时允许其他所有操作?
当执行该sudo fallocate -l 2G /swapfile命令时,会创建 2 GB,但包含那种内容或数据?可以定制吗?如果是的话怎么办?我确实做了一些研究,但没有关于这方面的细节。我想知道是否有很好的fallocate用途swap file。
例如,sudo dd if=/dev/zero of=/swapfile bs=1024 count=N创建填充零的数据,不确定是否swap file强制要求填充零的数据 - 在swap file教程中没有提到这一点。