63

I just formatted microSD card, and would like to run a dd command. Unfortunately dd command fails:

$ sudo dd bs=1m if=2016-02-26-raspbian-jessie-lite.img of=/dev/rdisk2
dd: /dev/rdisk2: Resource busy
$

Everyone on the internet says I need to unmount the disk first. Sure, can do that and move on. But I want to understand why / what exactly in OS X is making the device busy? How do I diagnose this?

So far I tried:

  1. Listing open files:

    $ lsof /dev/disk2
    $ lsof /dev/disk2s1
    $
    

    Also:

    $ lsof /Volumes/UNTITLED
    $
    
  2. Listing users working on the file:

    $ fuser -u /dev/disk2
    /dev/disk2: 
    $ fuser -u /dev/disk2s1 
    /dev/disk2s1:
    $
    

    Also:

    $ fuser -u /Volumes/UNTITLED
    $
    
  3. Check for system messages:

    $ sudo dmesg | grep disk
    $
    

    Also:

    $ sudo dmesg | grep /Volumes/UNTITLED
    $
    

My environment

  1. Operating system:

    Darwin Eugenes-MacBook-Pro-2.local 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64
    
  2. Information about my microSD:

    diskutil list disk2
    /dev/disk2 (internal, physical):
       #:                       TYPE NAME                    SIZE       IDENTIFIER
       0:     FDisk_partition_scheme                        *31.9 GB    disk2
       1:                 DOS_FAT_32 UNTITLED                31.9 GB    disk2s1
    

P.S. I'm using OS X 10.11.

Update 22/3/2016. Figured it out. I re-ran the lsof and fuser from above using sudo, and finally got to the bottom of the issue:

$ sudo fuser /Volumes/UNTITLED/
/Volumes/UNTITLED/: 62 282
$

And:

$ sudo lsof /Volumes/UNTITLED/
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
mds        62 root    8r   DIR    1,6    32768         2 /Volumes/UNTITLED
mds        62 root   22r   DIR    1,6    32768         2 /Volumes/UNTITLED
mds        62 root   23r   DIR    1,6    32768        10 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD
mds        62 root   25u   REG    1,6        0 999999999 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/journalExclusion
mds_store 282 root  txt    REG    1,6     3277        17 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/0.indexGroups
mds_store 282 root  txt    REG    1,6        8        23 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/0.indexCompactDirectory
mds_store 282 root  txt    REG    1,6      312        19 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/0.indexTermIds
mds_store 282 root  txt    REG    1,6     3277        29 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/live.0.indexGroups
mds_store 282 root  txt    REG    1,6     1024        35 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/live.0.indexCompactDirectory
mds_store 282 root  txt    REG    1,6      312        21 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/0.indexPositionTable
mds_store 282 root  txt    REG    1,6     8192        31 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/live.0.indexTermIds
mds_store 282 root  txt    REG    1,6     2056        22 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/0.indexDirectory
mds_store 282 root  txt    REG    1,6     8192        33 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/live.0.indexPositionTable
mds_store 282 root  txt    REG    1,6     8224        34 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/live.0.indexDirectory
mds_store 282 root  txt    REG    1,6       16        16 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/0.indexIds
mds_store 282 root  txt    REG    1,6    65536        48 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/reverseDirectoryStore
mds_store 282 root  txt    REG    1,6      704        24 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/0.indexArrays
mds_store 282 root  txt    REG    1,6    65536        26 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/0.directoryStoreFile
mds_store 282 root  txt    REG    1,6    32768        28 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/live.0.indexIds
mds_store 282 root  txt    REG    1,6    65536        36 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/live.0.indexArrays
mds_store 282 root  txt    REG    1,6    65536        38 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/live.0.directoryStoreFile
mds_store 282 root    5r   DIR    1,6    32768        10 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD
mds_store 282 root   17u   REG    1,6     8192        12 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/psid.db
mds_store 282 root   32r   DIR    1,6    32768        10 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD
mds_store 282 root   41u   REG    1,6       28        15 /Volumes/UNTITLED/.Spotlight-V100/Store-V2/A2D41CCB-48CC-45F3-B8D6-F3B383D91AAD/indexState
$

From the above it's easy to see that processes called mds and mds_store have created and are holding lots of files on the volume.

oldhomemovie
  • 1,627
  • 2
  • 13
  • 18

2 Answers2

100

Apple court, Apple rules. Try diskutil:

$ diskutil list
...

# if mounted somewhere
$ sudo diskutil unmount $device

# all the partitions (there's also a "force" option, see the manual)
$ sudo diskutil unmountDisk $device

# remember zip drives? this would launch them. good times!
$ sudo diskutil eject $device

(In the case of a disk image, the hdiutil command may also be of interest. You can also click around in Disk Utility.app.)

thrig
  • 34,333
  • 3
  • 63
  • 84
  • Hi @thrig, thank you. Yes, I am able to unmount using `diskutil`, no problem with that. What I'm doing here is I'm learning how to identify problems in the system by saying "Yes" to any warning I can spot. I want to know why "Resource busy" is happening in the first place, not how to get rid of it. What exact process in the OS is making this happen? – oldhomemovie Mar 22 '16 at 14:19
  • @gmile the kernel, as far as I know, locks busy devices, so you need to unmount and eject any partition and image to remove that kernel lock. – thrig Mar 22 '16 at 14:26
  • oh, this is interesting. I'm going to google for kernel locking now. Maybe you have some good resource on your mind where I could read about it? – oldhomemovie Mar 22 '16 at 14:28
  • If '''sudo diskutil unmount''' doesn't work, check here (https://raspberrypi.stackexchange.com/questions/9217/resource-busy-error-when-using-dd-to-copy-disk-img-to-sd-card) – Aaron Jul 05 '23 at 06:39
25

Unmounting the sub partitions helped

/dev/disk2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *8.0 GB     disk2
   1:             Windows_FAT_16 wr_usb_efi              134.2 MB   disk2s1
   2:                      Linux                         1.1 GB     disk2s2
vgsprasad-mbp:~ vgsprasad$ diskutil umount /dev/disk2s1
Volume wr_usb_efi on disk2s1 unmounted
vgsprasad-mbp:~ vgsprasad$ diskutil umount /dev/disk2s2
disk2s2 was already unmounted
oldhomemovie
  • 1,627
  • 2
  • 13
  • 18
Shivaprasad
  • 251
  • 3
  • 2
  • 5
    aside from missing formatting, the OP specifically said "Everyone on the internet says I need to unmount the disk first. Sure, can do that and move on. But I want to understand why / what exactly in OS X is making the device busy?" – Jeff Schaller Jul 14 '17 at 20:02
  • @Jeff I ran into the same problem as gmile, I was aiming to write an .iso to a USB stick, and dd returned `dd: /dev/disk9: Resource busy`. When I unmounted with `diskutil umount /dev/disk9s1`, the device disappeared from the desktop, but the `/dev/disk9` was still mounted so `dd` couldn't perform as it should. So Mac OSX kept the device busy by creating a short-cut to the USB stick's inner filesystem. – Gustav Feb 08 '18 at 08:27