DEV Community

Cover image for How to Quickly Identify the Cause When You Can't Eject a Disk on Mac (Bite-size Article)
koshirok096
koshirok096

Posted on • Edited on

How to Quickly Identify the Cause When You Can't Eject a Disk on Mac (Bite-size Article)

Introduction

Today's post will be quite short, but I’m writing it down as a personal memo.

I often use my DSLR for work, and after every shoot, I insert the SD card into my Mac to transfer the data.
But, when I do this process, almost every time, I encounter the same error, which has been quite troublesome.

Image description

This error always appears when I press the eject button for the disk in Finder.

Of course, I always ensure that the data transfer to the Mac is complete before attempting to eject, so there should be no procedural issues.

Nevertheless, I never understood the cause and had been force ejecting the disk every time.

But of course, this is not a good practice, and depending on the situation, it could potentially lead to disk damage, malfunction, or even data loss. Allowing such risky operations to become a habit is not ideal.

Identifying the Cause with the lsof Command

I decided to look into the issue a bit more today and found that I could identify the cause by running the following command in Terminal:

sudo lsof | grep /Volumes/disk-name-here
Enter fullscreen mode Exit fullscreen mode

lsof is a command that lists all the files and processes currently open on your Mac.
The grep part is used to filter (narrow down) the results to only show those related to the specific disk.

Actually, I had used the lsof command once before for a different purpose (my memory is a bit fuzzy, but I think it was for checking which process was using a specific port). However, I had never thought of using it in this context—to investigate why a disk couldn't be ejected. Learning about this method was a real eye-opener for me.

Here's an example of the output I got:

sudo lsof | grep /Volumes/USB_DISK
Password:
1mds        132                   root   29r      DIR               1,12     131072                    4 /Volumes/USB_DISK
mds_store  342                   root  txt       REG               1,12          0 18446744073709551433 /Volumes/USB_DISK/.Spotlight-V100/Store-V2/xxxx/tmp.live.14.cmpt.indexPositionTable
mds_store  342                   root  txt       REG               1,12     131072               332606 /Volumes/USB_DISK/.Spotlight-V100/Store-V2/xxxx/live.15.indexArrays
Enter fullscreen mode Exit fullscreen mode

I've edited it slightly for clarity for this blog, but in this case, you can see that the processes mds and mds_store were actively using the disk.

Tip: What is mds?

mds stands for Metadata Server.
It is a dedicated process that collects and manages file metadata in order to power Mac’s Spotlight search function.
The mds_store process shown in the output is responsible for storing and managing the scanned metadata information.

In this case, it seems that when I inserted the SD card (USB memory), mds immediately began scanning its contents.
Then, when I tried to eject the disk while the scan was still in progress, mds was still accessing some files, resulting in the eject error.

This likely explains why the same issue occurred every time I inserted a disk.

How to Deal with Spotlight Being the Cause

In my case, I found out that Spotlight was the culprit.
By simply excluding the disk from Spotlight indexing through the System Settings, I was able to solve the problem.
It wasn't particularly difficult.

Of course, depending on the situation, the cause of the eject failure could be something else entirely.
In such cases, I believe you'll need to investigate and respond accordingly based on the specific circumstances.

Conclusion

Although lsof is a very basic command, I hadn't had many opportunities to use it before, so this was a good learning experience.
I’m also relieved that the cause this time wasn’t something complicated.
Since this eject issue had been bothering me almost every time I used an SD card, I'm glad I was finally able to resolve it.

Thank you for reading!

Top comments (0)