Saturday, December 11, 2021

Re: Is it true that `dd` is almost not needed?

On Sat, 11 Dec 2021 18:06:43 +0200
<uxer@mailo.com> wrote:

> # Obscure dd version
> dd if=image.iso of=/dev/sdb bs=4M
> Guess what? This is exactly equivalent to a regular shell pipeline using
> cat and shell redirection:
>
> # Equivalent cat version
> cat image.iso >/dev/sdb
> That weird bs=4M argument in the dd version isn't actually doing
> anything special---all it's doing is instructing the dd command to use a
> 4 MB buffer size while copying. But who cares? Why not just let the
> command figure out the right buffer size automatically?

Did some digging as to what `cat` does to "figure it out". The author
seems to be working on a Linux machine, so I'll use the GNU coreutils
version of `cat` here.

It would appear it does a `stat(2)` on the file, then calls this:
https://github.com/coreutils/coreutils/blob/master/src/ioblksize.h#L73-L78

My reading of that is, it uses a block size of 128kiB or the filesystem
I/O block size, whichever is bigger. On my Gentoo Linux machine with
an EXT3 file system:

> RC=0 stuartl@rikishi ~ $ stat -c +%o Downloads/virtio-win-0.1-74.iso
> +4096

Thus the author's assertion that `cat` chooses a more "optimal" block
size (128kiB), rather than the `dd` call which specifies a 4MiB buffer,
seems dubious given the size of modern disk caches these days.

BUT, this isn't a Linux list, it's an OpenBSD list. What does OpenBSD do?
https://github.com/openbsd/src/blob/master/bin/cat/cat.c#L234

Looks similar, what's `BUFSIZ`? I could be wrong, but it might be this:
https://github.com/openbsd/src/blob/master/include/stdio.h#L168

Seems OpenBSD's `stat(2)` seems to return 16kiB for the block size for
a `ffs` file system. So you'd be copying with a block-size of 16kiB with
this author's suggestion to use `cat` instead of 4MiB.

A pretty dramatic difference, no?
--
Stuart Longland (aka Redhatter, VK4MSL)

I haven't lost my mind...
...it's backed up on a tape somewhere.

No comments:

Post a Comment