为什么 `dd conv=sparse` 没有像我预期的那样节省空间?

Kam*_*ski 15 dd buffer sparsefile

我打算为我的旧 USB 驱动器制作一个图像。我有充分的理由期望设备上有一些填充零的块,因此为了节省一些空间,我使用了以下conv=sparse选项:

dd if=/dev/sdb of=myusb.img conv=sparse bs=32M
Run Code Online (Sandbox Code Playgroud)

然而,它什么也没救我:

$ ls -hls myusb.img
250M -rw-r--r-- 1 root root 250M Oct 18 21:31 myusb.img
Run Code Online (Sandbox Code Playgroud)

我确定设备上有零填充块。为什么不dd conv=sparse节省空间?


注意我已经知道答案(我认为)。我把它贴在下面。问题供以后参考。

Kam*_*ski 29

如果您绝对确定存在零填充块,那么您没有节省空间的原因是您使用的大缓冲区。来自man dd

sparse 尝试寻找而不是写入 NUL 输入块的输出

您使用了bs=32M,因此您需要一个完整的 32 MiB 块在正确的偏移量处,以便conv=sparse选择完成其工作,即使只有一次。

选项bsibs(输入块大小)和obs(输出块大小)。虽然手册提到了input blocks,但实际上才是最obs重要的。

以下是一些测试的结果。(因为我是 OP,所以我使用相同的设备进行了测试。)每个文件都根据<obs_used>.img模式命名。注意第一列:

$ ls -hlst *.img
250M -rw-r--r-- 1 root root 250M Oct 18 22:02 4M.img
250M -rw-r--r-- 1 root root 250M Oct 18 22:02 2M.img
249M -rw-r--r-- 1 root root 250M Oct 18 22:02 1M.img
248M -rw-r--r-- 1 root root 250M Oct 18 22:01 512K.img
248M -rw-r--r-- 1 root root 250M Oct 18 22:01 256K.img
247M -rw-r--r-- 1 root root 250M Oct 18 22:00 128K.img
247M -rw-r--r-- 1 root root 250M Oct 18 21:57 64K.img
247M -rw-r--r-- 1 root root 250M Oct 18 21:56 32K.img
246M -rw-r--r-- 1 root root 250M Oct 18 21:55 16K.img
246M -rw-r--r-- 1 root root 250M Oct 18 21:54 8K.img
246M -rw-r--r-- 1 root root 250M Oct 18 21:53 4K.img
246M -rw-r--r-- 1 root root 250M Oct 18 21:52 2K.img
246M -rw-r--r-- 1 root root 250M Oct 18 21:51 1K.img
246M -rw-r--r-- 1 root root 250M Oct 18 21:44 512.img
Run Code Online (Sandbox Code Playgroud)

结论是:你不应该使用 large obswithconv=sparse选项。常见的扇区大小是 512 字节,所以bs=512看起来恰到好处。你的命令应该是:

dd if=/dev/sdb of=myusb.img conv=sparse bs=512
Run Code Online (Sandbox Code Playgroud)