In Linux, unmounting a busy device (one that is currently in use) can sometimes be tricky. If a device is “busy,” it means that some process is still using it, such as having files open or being the current working directory of a process. To unmount a busy device, you can try several strategies.
Steps to unmount a busy device
1. Identify which processes are using the device
First, identify the processes that are using the device. You can use the lsof
or fuser
commands for this purpose.
- Using
lsof
(List Open Files):sudo lsof /mount/point
This will list all processes that have files open on the specified mount point. The
/mount/point
should be replaced with the actual directory or device mount point. - Using
fuser
(Identify Processes Using a File or Mount Point):sudo fuser -m /mount/point
The
-m
option tellsfuser
to identify processes using the given mount point. You can also use-v
for a verbose output to see more details.
2. Terminate the Processes Using the Device
Once you know which processes are using the device, you can either kill them manually or force unmount the device.
- To kill a specific process, use:
sudo kill -9 <PID>
Replace
<PID>
with the Process ID (PID) of the process you want to terminate. - Alternatively, if you want to terminate all processes using the device, use:
sudo fuser -km /mount/point
The
-k
option kills all processes using the mount, and-m
specifies the mount point.
3. Force Unmount (Lazy Unmount)
If killing the processes doesn’t work or if you prefer a less invasive approach, you can try forcing the unmount.
- Using
umount
with the-l
(Lazy) Option:sudo umount -l /mount/point
The
-l
option tells the system to lazily unmount the device. The device will be detached immediately, but the system will wait until it’s no longer in use to fully clean up the mount. - Using
umount
with the-f
(Force) Option (for network-mounted devices):If the device is network-mounted (e.g., NFS), you can force the unmount using the
-f
option:sudo umount -f /mount/point
The
-f
option forces the unmount even if the device is in use, but this can be risky as it may lead to data loss if data is being written to the device.
4. Check for Active Directory Sessions
If you’re unable to unmount the device because you’re in the mount directory, you may need to change your current working directory to another location. For example:
cd /
This ensures no processes are using the mount point as their current directory.
5. Check for Active Devices
If you’re still unable to unmount the device, you can check whether the device is in use by running the mount
command:
mount | grep /mount/point
This shows if the device is still listed as mounted.
6. Reboot the System (Last Resort)
If all else fails, and the device still refuses to unmount due to stubborn processes or system constraints, a system reboot might be required. However, rebooting should be a last resort.
sudo reboot