“In Linux, we use the umount command to unmount a device or partition as specified by its path. However, in some scenarios, you may encounter the “target is busy” error when attempting to unmount a device.
In this tutorial, we will attempt to break down the cause of this error, why it occurs, and how you can resolve it.”
Cause of “Target is Busy” Error
Once you mount a device in your system, any process with sufficient permission can access its filesystem. When the “target is busy” error occurs, it means that an active process is currently accessing a file or directory within the device.
Therefore, to prevent data loss, the Linux kernel will prevent you from unmounting the device when it’s being accessed.
But there are some instances where you need to remove the device. Let us discuss the various techniques you can use to do this.
Fix 1: Terminate the Running Process
We have established that the main cause of this error is an active process that is accessing a file within the device.
Therefore, to resolve this error, we can terminate the process, and the device will be free to be mounted.
Luckily, Linux has a command that allows you to show all the view the open files and the processes that are accessing that file.
You can learn more about about the lsof command here:
https://linuxhint.com/check-open-files-in-linux/
We can use the lsof command followed by the path to the disk. For example, if we are getting the error in device /dev/sda1, we can view the open files and the associated processes using the command:
The command should return the processes accessing the files are their IDs.
We can then take the PIDs and use them to kill the process.
Once the process is terminated, you can unmount your device.
Keep in mind that there may be more than one process accessing a file. Hence, ensure to terminate all of them.
Fix 2: Force Unmount
Another solution for unmounting a busy device is to force it. This can be useful if the error is caused by an unreachable resource such as a network resource.
Keep in mind that force unmounting a device can lead to data loss.
To do this, use the -f option as:
Fix 3: Lazy Unmount
The third and safer alternative to the force unmount is a lazy unmount. A lazy unmount allows the system to detach the specified mount point in a hierarchical manner. It works by removing any references to the filesystem as soon as it’s not busy.
And once no process is accessing the fs, the system runs the unmount command successfully and removes the device.
This is a safe option as you do not forcibly close any running processes. Instead, you allow the system to monitor once the operations are complete and then unmount the device.
To use a lazy unmount, use the -l flag in the umount command:
Conclusion
That’s it for this one. In this tutorial, we covered the cause of the “target is busy” error when unmounting devices in a Linux system. We also provided three solutions for unmounting the device appropriately.
As always, thanks for reading!!