Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
fi

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.

share|improve this question
    
Why is it obvious that writing to a mounted disk is permissible? Have you tried doing it with another application? –  Barmar 12 hours ago
    
Perhaps obvious is the wrong word. I can write to the disk by using the cp command. –  Stephano 10 hours ago
    
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 7 hours ago

1 Answer 1

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.

share|improve this answer
    
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 8 hours ago
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 8 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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