8

I was wondering if it would be possible to write a disk image file directly to a partition without saving it as a file first. Something like

dd if="http://diskimages.com/i_am_a_disk_image.img" of=/dev/sdb1 bs=2M

I would also accept an answer in C or Python because I know how to compile them.

3
  • 1
    The point is how you can verify the correctness of the download. The other thing is resuming an aborted download. With today's disk sizes you should afford to save it in a file first.
    – U. Windl
    Jan 7 at 1:17
  • @U.Windl You can do both just fine, whether you write to a file or a partition.
    – wizzwizz4
    Jan 7 at 15:59
  • @U.Windl The problem with phrases like "today's disk sizes" is that it assumes a desktop, laptop or server. There are many contexts even today where you're not so lucky with space such as SBCs. Jan 7 at 19:34
25

This is actually trivial. You can write to the device just like it's a file, and there are commands for directly downloading content and either writing it to a file or writing it to "stdout".

As the user root you can simply:

curl https://www.example.com/some/file.img > /dev/sdb

Where /dev/sdb is your hard drive.

This is not generally recommended but will work just fine and is useful in very small devices without much disk space.

Incidently it would be more normal to write a disk image to a disk /dev/sdb not a partition /dev/sdb1.

0
15

You can use wget -O option to print to disk directly:

wget -O /dev/sdb http://diskimages.com/i_am_a_disk_image.img

You don't really need to use dd.

1
  • 1
    Consider rephrasing your answer - in its current form it suggests -O has something to do with accessing the disk, while it is simply a flag for output file.
    – ciamej
    Jan 7 at 20:28
4

Yes, you can actually. Use something like this:

curl 'http://diskimages.com/i_am_a_disk_image.img' | dd conv=sync,noerror bs=2M of=/dev/sdX
13
  • 3
    well two reasons, main is that OP gave an example with dd in the initial question, second is, the advantages of block size specifications (if required) and to avoid stopping due to read errors (high latency over network in this case, etc) with conv=sync,noerror. Given the question, they are not mandatory options but if one is to use dd, they would be nice to have
    – Alex
    Jan 6 at 0:14
  • 3
    It's also handy to split into two processes if you want them to run as different users. A cautious operator would download as non-root user, but may need to be root to write to the disk partition. Jan 6 at 9:33
  • 4
    @Alex: What actual (re)blocking do you want dd to do? If that actually happened, e.g. writing a full output block after a short read of an input block from the pipe, it would munge your data. When is dd suitable for copying data? (or, when are read() and write() partial) / Is it better to use cat, dd, pv or another procedure to copy a CD/DVD?. Jan 6 at 14:25
  • 3
    Most programs other than dd pick some reasonable block size for their output, like at least 4k or 8k. 64k or so would be better to trade off system call overhead with CPU L2 cache staying hot for the kernel to copy the data into a buffer queue, but since this isn't O_DIRECT it's not an actual hardware write block size. Specifying bs= something non-default is important for dd only because its default is tiny, 512 bytes, so system-call overhead is a killer. Jan 6 at 14:26
  • 4
    I disagree. conv=sync,noerror means that if curl writes less than 2 MiB, dd will pad that 2 MiB block with zeros, corrupting the data. This is unlikely to happen over a reliable, fast link, but it's possible. Review the questions that Peter Cordes posted -- I, too, used to use dd all the time, and answers on this site made me reconsider my views.
    – Josh
    Jan 6 at 16:25

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.