News for hackers. Stuff that matters.
Re:Dd is not a disk writing tool (2015) by RJIb8RBYxzAMX9u on Sunday January 08, 2017 @09:11AM (#13349727)
> I wish that all of those projects, tutorials etc. that explain how to write their image to a block device, like an sdcard, would start advise using cat, because there is no reason to use dd, it's just something that people stick with because others do it too.

I'd wondered whether dd or cat were faster, and indeed cat is faster, but not by much. Also, for some embedded devices, you have to write to specific offsets, so dd is more convenient and explicit. Lastly, cat composes poorly with sudo.

  $ sudo dd if=foobar.img of=/dev/sdi # works
  $ sudo cat foobar.img > /dev/sdi    # fails unless root b/c redirection is done by shell
> I only use dd for specific blocks, like writing back a backup of the mbr, or as a rudimentary hex editor.

xxd / xxd -r is much nicer, but I suppose sometimes vim is not available...

Re:Dd is not a disk writing tool (2015) by JoshTriplett on Sunday January 08, 2017 @10:20AM (#13350002)
> I'd wondered whether dd or cat were faster, and indeed cat is faster, but not by much.

That performance difference often comes from block size; "dd bs=1M" typically runs much faster than the default block size of 512 bytes.

Re:Dd is not a disk writing tool (2015) by mercora on Sunday January 08, 2017 @10:19AM (#13349994)
one could use:

cat foobar.img | sudo tee /dev/sdi > /dev/null

Re:Dd is not a disk writing tool (2015) by JoshTriplett on Sunday January 08, 2017 @10:23AM (#13350014)
I've used that to write short files (such as settings in /sys or /proc), but for large files, tee has the disadvantage of writing everything twice, and the pipe adds another write and read of every byte.
Re:Dd is not a disk writing tool (2015) by mercora on Sunday January 08, 2017 @10:28AM (#13350048)
I just realized this too and tried to close stdout instead. tee complains about it but goes on with its business:

cat foobar.img | sudo tee /dev/sdi >&-

Re:Dd is not a disk writing tool (2015) by RJIb8RBYxzAMX9u on Sunday January 08, 2017 @10:40AM (#13350126)
Why's everyone so against dd? :-) If you're going to use tee, might as well not bother with cat at all[0]:

  $ sudo tee /dev/sdi >&- < foobar.img
Or better yet, pv[1]; you even get a progress bar that way!

[0] https://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_...

[1] http://www.ivarch.com/programs/pv.shtml

Re:Dd is not a disk writing tool (2015) by mercora on Sunday January 08, 2017 @10:59AM (#13350249)
Thanks for the reminder :) I do fall quite often for the useless use of cat. But most of the time i also do not really care about it much. I did omit pv in believe tee will always be available but it is great and absolutely preferred when available.
Re:Dd is not a disk writing tool (2015) by carussell on Sunday January 08, 2017 @10:57AM (#13350240)
> xxd / xxd -r is much nicer, but I suppose sometimes vim is not available

`od` is ubiquitous--it's POSIX and a requirement of the Single UNIX Specification

Re:Dd is not a disk writing tool (2015) by nerdponx on Sunday January 08, 2017 @09:27AM (#13349785)
Would

    sudo (cat foobar.img > /dev/sdi)
Work?
Re:Dd is not a disk writing tool (2015) by sply on Sunday January 08, 2017 @09:57AM (#13349887)
No, you should use

  sudo sh -c "cat foobar.img > /dev/sdi"
or

  sudo -s "cat foobar.img > /dev/sdi"
Re:Dd is not a disk writing tool (2015) by RJIb8RBYxzAMX9u on Sunday January 08, 2017 @10:29AM (#13350061)
I was originally going to put that in my examples, but opted to leave it out, because with sh -c you have to think about escaping special characters. Most of the time it doesn't matter, but when running commands as root you ought to be absolutely sure.
Re:Dd is not a disk writing tool (2015) by nerdponx on Sunday January 08, 2017 @09:41PM (#13353586)
What about using a here-doc (as in http://stackoverflow.com/a/16514624/2954547)?

    sudo sh <<EOF
    cat foobar.img > /dev/sdi
    EOF
Re:Dd is not a disk writing tool (2015) by RJIb8RBYxzAMX9u on Sunday January 08, 2017 @09:36AM (#13349814)
No, unless you have a shell where sudo is a built-in, and I don't know of any.
Re:Dd is not a disk writing tool (2015) by c0l0 on Sunday January 08, 2017 @10:03AM (#13349915)
This is actually impossible (at least with UNIX shells), as shell builtins cannot have the SUID bit set - and your shellitself also shouldn't ;)
Re:Dd is not a disk writing tool (2015) by JoshTriplett on Sunday January 08, 2017 @10:19AM (#13349990)
You could have a shell with a "sudo" builtin that knew how to invoke a separate "sudo" program with the right shell syntax and quoting, such that "sudo somecommand > /path/to/root-writable-file" did the right thing.
Re:Dd is not a disk writing tool (2015) by abofh on Sunday January 08, 2017 @12:29PM (#13350809)
Not really - to sudo, your shell would have to be setuid - and constantly fork stuff as you to get user permissions. Alternatively your shell could maintain a separate process for privileged access, but that puts a whole lot of your security on the assumption that your shell has no bugs that might allow escalation.

In short, you could do it, but it'd be ripped out of every server that's been hardened, and for users that don't want to care - they're just running 'sudo su' anyhow.

Speaking only for myself, the thought of my shell having a magical escalation process would scare the bejeezus out of me - and I'm supposed to have root on our boxes!

Re:Dd is not a disk writing tool (2015) by TheCoelacanth on Sunday January 08, 2017 @03:42PM (#13351875)
The shell wouldn't need to be setuid if it just performed a syntax transformation and then called the existing sudo binary.
Re:Dd is not a disk writing tool (2015) by JoshTriplett on Sunday January 08, 2017 @06:24PM (#13352741)
Exactly. The shell would just treat "sudo" as a builtin prefix similar to "time", but would then run the real "sudo" with an appropriate shell invocation and proper quoting.
Re:Dd is not a disk writing tool (2015) by nerdponx on Sunday January 08, 2017 @09:44PM (#13353604)
I finally got home to try it, and indeed it does fail (Zsh).

    zsh: parse error near `)'
Is it just ignoring the parentheses? Does the redirection "take precedence" over parentheses and brackets?
Re:Dd is not a disk writing tool (2015) by na85 on Sunday January 08, 2017 @11:30AM (#13350459)
Sudo is anachronistic anyways.

Laptops don't need to follow multiuser best practices, and frankly a complex root password offers little.

Re:Dd is not a disk writing tool (2015) by greggyb on Sunday January 08, 2017 @09:26AM (#13349781)

    sudo (cat foobar.img > /dev/sdi)
No?
Re:Dd is not a disk writing tool (2015) by ryao on Sunday January 08, 2017 @11:35AM (#13350492)
That is a syntax error in bash.
Re:Dd is not a disk writing tool (2015) by greggyb on Monday January 09, 2017 @02:58PM (#13359287)
Learn something new every day. Thanks.