Why didn't `dd conv=sparse` save space as I expected?

11

2

I was going to make an image of my old USB drive. I had good reasons to expect there were some blocks filled with zeros on the device, so to save some space I used conv=sparse option:

dd if=/dev/sdb of=myusb.img conv=sparse bs=32M

However it saved me nothing:

$ ls -hls myusb.img
250M -rw-r--r-- 1 root root 250M Oct 18 21:31 myusb.img

I'm sure there are zero-filled blocks on the device. Why didn't dd conv=sparse save space?


Note I already know the answer (I think). I'm posting it below. The question is for future reference.

Kamil Maciorowski

Posted 2016-10-18T21:11:47.720

Reputation: 38 429

Answers

18

If you are absolutely sure there were zero-filled blocks then the reason you saved no space was the large buffer you used. From man dd:

sparse try to seek rather than write the output for NUL input blocks

You used bs=32M, so you needed a whole 32 MiB block of zeros at a right offset for the conv=sparse option to do its job if only just once.

The option bs sets ibs (input block size) and obs (output block size). While the manual mentions input blocks, it is actually the obs that matters.

Here are the results of some tests. (As I am the OP, I did the tests with the very same device.) Each file is named according to <obs_used>.img pattern. Pay attention to the first column:

$ 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

The conclusion is: you shouldn't use large obs with conv=sparse option. The common sector size is 512 bytes, so bs=512 seems just right. Your command should have been:

dd if=/dev/sdb of=myusb.img conv=sparse bs=512

Kamil Maciorowski

Posted 2016-10-18T21:11:47.720

Reputation: 38 429