When using Linux (Debian), I often use dd to copy a disk image to an SD card. I have written a script that throws an error if the device file specified in the "of" option is too large. This keeps me from accidentally blowing away one of my hard disks.

SD_SIZE=$(sudo sfdisk -s ${SD_DEV}) 
if [ $SD_SIZE -gt 33554432 ]; then
  echo "might not be and SD card, exiting"
  exit 1

However, if I insert the SD card and forget to unmount it, results are sketchy. Sometimes the copy succeeds and sometimes it fails.

I can amend my script with the answer here: How to check if a filesystem is mounted with a script

However, is there an option in dd with this functionality? (on OS X dd will not write to a mounted disk by default)

Also of interest, why will dd (on OS X) error when trying to copy to a mounted disk? Perhaps some differences in the kernel or dd? Here is the error you get if you try to dd to a drive that is mounted in OS X (10.9):

dd: /dev/diskN: Resource busy, make sure the disk is not in use

I can write to the disk using cp, so perhaps the system calls that dd is making are not as simple as OPEN then WRITE.

  • Why is it obvious that writing to a mounted disk is permissible? Have you tried doing it with another application? – Barmar Oct 2 '14 at 14:43
  • Perhaps obvious is the wrong word. I can write to the disk by using the cp command. – Stephano Oct 2 '14 at 16:21
  • 2
    There's a difference between writing files within the filesystem and writing to the device directly. The latter is prohibited while the filesystem is mounted, to prevent you from modifying filesystem metadata. – Barmar Oct 2 '14 at 19:25

Sometimes the copy succeeds and sometimes it fails.

Probably it fail because some process in the while wrote to the mounted filesystem, guess that's why is a good practice to umount before :-)

dd should really be just open and write, I guess the MacOSX version add some control and I think is easy to understand why with their device names, compared to Linux I triple-check before dd'ing.

  • That is probably true. I would love it if I could patch dd so that it would error if the drive was mounted. I could patch the kernel too, but I'd much rather patch dd :) . – Stephano Oct 2 '14 at 18:29
  • 1
    No need for patches, in your script just check with mount or in /proc/mounts with grep (or whatever) if is mounted (if grep -q /dev/sdd /proc/mounts; then echo yep; else echo nop; fi) – Alex Oct 2 '14 at 18:34

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.