Linux may report “device is busy” when we try to umount a filesystem. This behavior is reasonable as it can help us avoid data loss by disallowing unmouting a filesystem when it is being used. But for situations when we are sure there is something wrong happened or we care not data lost such as a NFS mounting failed because that the NFS server is dead and will never be back. Under these kinds of situations, how do we force Linux to unmount a filesystem?
First, why “device is busy”? The reason is that there are likely programs/kernel threads accessing partition or open files. Knowing the reason, we can find the solutions.
Option 0: Try to remount the filesystem if what you want is remounting ∞
Here this option is not to really do unmounting a filesystem. But it is a useful technique. It is common that the reason we want to unmount a filesystem is that we find that there are problems with the mounting temporarily and we want to unmount and re-mount it. For these situations, you may first try
# mount -o remount /your/mount/point
The “-o remount” option will make Linux try to remount the filesystem. If the remounting can succeed, it avoids the problems of disrupting or killing processes.
Option 1: Force unmount or lazy unmount ∞
There are options of
umount to detach a busy device immediately even if the device is busy.
-f, --force Force an unmount (in case of an unreachable NFS system). (Requires kernel 2.1.116 or later.) -l, --lazy Lazy unmount. Detach the filesystem from the file hierarchy now, and clean up all references to this filesystem as soon as it is not busy anymore. (Requires kernel 2.4.11 or later.)
-f option is for unreachable NFS system. Please be aware that programs may not expect a force or lazy unmounting and these options may disrupt running processes using the filesystem, cause data loss or corrupt files opened.
Option 2: Kill the processes using the filesystem and then unmount it ∞
If the reason is that processes are using the filesystem, we can kill the processes using the filesystem and then it will be fine to unmount it. Here, we introduce 2 common methods with 2 common tools.
Method 1: use
The following command finds out the processes accessing the partition/filesystem. You need to run the following command as root. Here, we use the mount point /mnt/data as an example.
# lsof | grep '/mnt/data'
It will output lines like
bash 17622 user1 cwd DIR 253,2 4096 2 /mnt/data
If you are sure that it is safe to kill the process, you may kill them by
kill -9. After the processes are killed, the filesystem will be able to be unmounted.
Method 2: use
You can find out the processes using a filesystem (e.g. /mnt/data) by
# fuser /mnt/data
fuser can also help you kill all processes using a filesystem at the same time by
# fuser -k /mnt/data
# fuser -k -9 /mnt/data
Then you can continue to umount the filesystem.