Sometimes when you unmount a filesystem or drive, the system shows "unmount target is busy" message. Unless you release the busy system, you won't be able to unmount.
In this guide, we learn how to fix umount target is busy in Linux.
What is unmount target busy
A mounted filesystem can be accessed by any process running on the Linux operating system. When such a filesystem or a directory which is being accessed by some process is unmounted, the system may issue the target as busy (“device is busy”). One of the reasons the system doesn't allow unmount is to prevent data loss.
This problem generally occurs in the following scenarios:
- The current working directory accessed by the terminal is on the path of the mount point
- A process is accessing the files on the filesystem hierarchy being unmounted
- A process is accessing a file which relies (reads/writes) on the file inside the filesystem which is being unmounted.
- Remote mount point unreachable
Force unmount is one solution to detach the busy device.
Usually, you see a busy device message when you try to unmount an NFS filesystem. This happens often when the NFS server has some issues (mainly unreachable) and you have a soft NFS mount.
Force unmount will detach the server mount point by disrupting some running processes.
CAUTION: This may cause data loss or corrupt files or programs accessing file throw errors.
unmount -f /path/to/busy-nfs-mount
You can verify if it is successfully unmounted using the df command.
Using lsof - Find and Kill the processes using the file
The lsof (list open files) command displays a list of all open files and the processes associated with them on a specific file system, directory, or device. By default, it lists all files, shared libraries, and directories that are currently open and provides as much information as possible about each of them.
We can use lsof command to find PID (process id) corresponding to our mount point and then kill that process.
Use the following lsof command to list processes using the mount point.
lsof | grep '/media/dsk'
The output shows that the user linuxopsys has two bash processes with PIDs 4255 and 4335 using /media/dsk.
Once these two programs are stopped the device is no longer busy.
Once you make sure its safe to kill the process, run the following kill command:
kill -9 4255 kill -9 4335
If no more processes tide to the mount point, you initiate umount command.
Using fuser - Kill processes accessing the file
fuser command in Linux helps to identify the processes which are accessing sockets or files on filesystems.
Use fuser command with -m option which lists all the processes accessing the files or mount point on the file system. You can add -v option for verbose.
The following example displays all the processes accessing the file system /media/dsk along with their process ids arranged in a tabular format.
fuser -mv /media/dsk
Now you found the processes which are using the file on the mounted filesystem. You can use kill command to terminate those processes.
You can add -k option to kill processes in a single command, so you can avoid one step.
fuser -kmv /media/dsk
Lazy unmounting is used to unmount the mount point from the Linux filesystem hierarchy. The command removes all references to the detached file system as soon as it is no longer busy. Once no processes are accessing the unmounted file system, the umount command executes and actually detaches the file system.
Lazy unmounting a mount point is beneficial when we don't want to unexpectedly kill the process which are accessing the file systems as this may lead to loss of data or may corrupt the file systems.
For example, when you are copying a file from the mounted flash drive to linux machine and need to unmount the mounted device at the same time so that you don't need to actively check the status of the file transfer, you can execute a lazy unmount on the flash drive so that once the operation to copy the file is complete, the system automatically detaches the mount point of the flash drive.
To lazy unmount use -l option followed by the mount path:
umount -l <path_to_mount_point>
In this guide, we learned how to fix when you encounter umount target is busy in Linux. We have explained solutions using force unmount, detaching busy device by finding and kill the processes, and finally lazy mount.
Thanks for reading, please leave your feedback and suggestions in the comment section.