19

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?

don_crissti
  • 82,805
  • 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). – MAQ 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

4 Answers4

25

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
don_crissti
  • 82,805
22
  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.

agc
  • 7,223
  • 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
  • 9
    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
  • @GuntramBlohm I support your argument. I think dd is still the only program that can ensure correct block size and provide arguments which you pointed out. All my cp attempts have failed eventually. dd is solid. – Léo Léopold Hertz 준영 May 20 '16 at 19:19
  • Why on earth are you piping pee into dd? Why would you not simply run tee? – Gilles 'SO- stop being evil' May 20 '16 at 22:45
  • 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 03:49
  • @GuntramBlohm Yes but the widespread use of dd for disk images (which do not need to be written in fixed block sizes, particularly to a non-raw-mode device) is due to a bug in GNU cp on Linux in the early days of Linux. There's information about it in the early Linux mailing list archives. – Random832 May 21 '16 at 03:49
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

manifestor
  • 2,473
  • 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. – manifestor Dec 22 '17 at 21:44
1

simple way:

$ sudo dd if=/dev/sd? | tee img.1 > img2

or

$ sudo dd if=/dev/sd? | tee img.1 img.2 img.N-1 > img.N

on specific case:

dd if=file.img bs=1M | sudo tee /dev/disk1 /dev/disk2 > /dev/null