16

I have 2 exactly same formatted, same size and same brand SD-cards. I would like to dd image to /dev/disk2 and to /dev/disk3 at the same time.
Pseudocode

sudo dd bs=1m if=/Users/masi/2016-05-10-raspbian-jessie.img of={/dev/disk2,/dev/disk3}

How can you dd from one input to many output SDs?

  • What's the goal here? Performance? If yes remember the data you read will be cached. So the 2nd dd will you data from your bcache instead of reading (of course you need some free RAM). – KWubbufetowicz May 20 '16 at 15:42
  • 2
    My simplistic test with GNU coreutils dd ... of=one of=two did not produce two outputs. May need two dd commands. I don't see wording in posix for dd to allow for multiple of's. – Jeff Schaller May 20 '16 at 15:46
  • @KWubbufetowicz I have 8GB RAM. Possible to get 32GB. Speed is my goal because I have 5 SDs. – Léo Léopold Hertz 준영 May 20 '16 at 15:53
20
  1. Borrowing from don_crissti's answer using tee, but without dd or bashisms:

    sudo tee /dev/disk2 /dev/disk3 > /dev/disk4 < masi.img
    
  2. Using pee from Debian's moreutils package:

    sudo dd if=masi.img | \
      pee "dd of=/dev/disk2"  "dd of=/dev/disk3"  "dd of=/dev/disk4"
    

    With bash, ksh, or zsh, that can be abbreviated to:

    sudo dd if=masi.img | pee "dd of=/dev/disk"{2..4}
    

    Or even, (if there's no need for dd's useful functions):

    sudo pee "dd of=/dev/disk"{2..4} < masi.img
    

    pee is useful; if required one may include, (within each quoted argument), additional distinct dd options, and even other pipes and filters, individually tailored to each output device.

With either method the number of output disks can be extended indefinitely.

  • 1
    Using dd for this isn't necessary. A lot of people believe it's necessary because its usage, specific to copying to or from disk images, is "fossilized" in tutorials written back in the 90s and copied by people who didn't know why. It was used back then to work around a glitch in cp. – Random832 May 20 '16 at 17:26
  • @Random832 It is necessary because it is more stable than cp. Try many formats and different allocation sizes. I have not found enough stable cp for the work. Please, correct me as an answer here if you can argumentate how cp is enough stable. – Léo Léopold Hertz 준영 May 20 '16 at 17:48
  • @Masi It is rarely necessary to explicitly select a format (what do you even mean by "format") and allocation size (you mean the block size?) for the common purpose of writing a disk image to a disk or reading a disk into a disk image. The reason dd became commonly used for this purpose was because of a bug in cp (GNU cp on Linux specifically) in the early 90s that caused it to skip copying blocks that were all-zero-bytes. – Random832 May 20 '16 at 18:23
  • 7
    Bug in cp in the 90s? dd is 20 years older than that. The main reason for using dd was that many devices, especially tape drives, needed to be read/written in fixed block sizes, which is why dd has bs, ibs and obs parameters, and it was the only program that could ensure correct block sizes. – Guntram Blohm May 20 '16 at 18:57
  • 1
    @Gilles, here tee is indeed preferable to pee, (but dcfldd is best). Still, pee is useful; one may, if required, include, (within each quoted argument), additional 'dd' options, and even other pipes and filters, individually tailored to each output device. – agc May 21 '16 at 3:49
20

You could try dcfldd
It's an enhanced version of gnu dd and it can output to multiple files or disks at the same time:

dcfldd if=masi.img of=/dev/disk2 of=/dev/disk3 of=/dev/disk4
  • Can you explain your first command. Why pipe only before the last dd? How does the location of the pipe change with four dd? – Léo Léopold Hertz 준영 May 20 '16 at 16:24
  • 3
    dcfldd is the fastest, maybe that should be the first choice. – agc May 20 '16 at 16:29
3

Also this is possible with tee and process substitution:

dd if=/dev/sda | tee >(dd of=/dev/sdb) >(dd of=/dev/sdc) | dd of=/dev/sdd

  • Can you explain process substitute? How is your answer different from agc's one? – Léo Léopold Hertz 준영 Dec 22 '17 at 20:46
  • yeah sure: process substitution is used when you need to pipe the stdout of one command to multiple commands. Using a simple pipe will let you pipe to just one command. My answer is achieving the same result as agc's, but in a different way :) Also I never used pee before, as I have the tee command on my system. – chevallier Dec 22 '17 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.