3 ways to force uninstall in Linux, showing “device is busy”

When you perform an NFS mount, sometimes the “ device is busy ” status is displayed, in which case we need to perform a forced unmount in an appropriate way. If the normal nfs uninstall fails, we can try different methods and options.

Situation

In our scenario, we created /var/linoxide Mount the directory. When we tried to unmount the remote partition, we received an error message. Glad to read the NFS mount options on Linux.

You can use df command to mount all installed folders

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.3G 18G 7% /
devtmpfs 236M 0 236M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 8.4M 237M 4% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/0
10.128.20.241:/var/linoxide 20G 1.3G 18G 7% /mnt/nfs/linoxide_srv
10.128.20.241:/home 20G 1.3G 18G 7% /mnt/nfs/home_srv

In the last two lines, you can see the folders that have been installed on the client. The following example shows that the uninstall fails due to a busy device

# umount /mnt/nfs/linoxide_srv/
umount.nfs4: /mnt/nfs/linoxide_srv: device is busy

1) Use lsof

The lsof (list open files) command displays a list of all open files and the processes associated with them on a particular 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 them. The output gives us some information such as PID, USER so that we can use pipes to filter its output.

# lsof /mnt/nfs/linoxide_srv/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 24098 root cwd DIR 253,1 4096 519062 /mnt/nfs/linoxide_srv
bash 24125 root cwd DIR 253,1 4096 519062 /mnt/nfs/linoxide_srv
vim 24144 linoxide cwd DIR 253,1 4096 519062 /mnt/nfs/linoxide_srv

You see that we have the PID of the process using the installed folder, we see the command being executed and the user who executed the command. You can kill busy processes, but pay attention to the commands executed. You will see the vim command, which means that the linoxide user is editing the file. So if we stifle the process, his progress will be lost. Assuming we have notified him, let’s see the results

# lsof /mnt/nfs/linoxide_srv/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 24098 root cwd DIR 253,1 4096 519062 /mnt/nfs/linoxide_srv
bash 24125 root cwd DIR 253,1 4096 519062 /mnt/nfs/linoxide_srv

You can see that our users have stopped their modifications, but we are still executing bash commands, but we don’t know why. Now we can kill these two processes with the kill command. Make sure not to miss the process of killing the process.

Now we will kill the first bash process

# kill -9 24098

We can verify the results

# lsof /mnt/nfs/linoxide_srv/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 24125 root cwd DIR 253,1 4096 519062 /mnt/nfs/linoxide_srv

We can see that a process is killed

# kill -9 24125

Now let’s verify the second process

# lsof /mnt/nfs/linoxide_srv/

Now let’s try to uninstall the folder

# umount /mnt/nfs/linoxide_srv/
umount: /mnt/nfs/linoxide_srv/: not mounted

It seems the kill process has unmounted the folder automatically, but let’s check it with the df command

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.3G 18G 7% /
devtmpfs 236M 0 236M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 8.3M 237M 4% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/0
10.128.20.241:/home 20G 1.3G 18G 7% /mnt/nfs/home_srv

folder /mnt/nfs/linoxide_srv Uninstalled as needed.

2) With fuser

The fuser (Find User Processes) command helps identify processes that are preventing you from unmounting the file system. It finds user processes associated with any files, directories, or file system mount points you provide as command line arguments.

# fuser /mnt/nfs/linoxide_srv/
/mnt/nfs/linoxide_srv: 24191c

We can use with -m This option lists all processes that access the file or mount point on the file system, and -v This option displays results such as ps command with PID, user and executed command.

# fuser -mv /mnt/nfs/linoxide_srv/
 USER PID ACCESS COMMAND
/mnt/nfs/linoxide_srv:
 root kernel mount /mnt/nfs/home_srv
 root 24191 ..c.. bash
 root 24275 ..c.. bash
 linoxide 24290 ..c.. vim

You can see the command being executed. We must prevent our linoxide user from saving his work.

# fuser -mv /mnt/nfs/linoxide_srv/
 USER PID ACCESS COMMAND
/mnt/nfs/linoxide_srv:
 root kernel mount /mnt/nfs/home_srv
 root 24191 ..c.. bash
 root 24275 ..c.. bash

Using the fuser command, you can directly terminate the executing process -k Option without kill command

# fuser -kmv /mnt/nfs/linoxide_srv/
 USER PID ACCESS COMMAND
/mnt/nfs/linoxide_srv:
 root kernel mount /mnt/nfs/home_srv
 root 24191 ..c.. bash
 root 24275 ..c.. bash

test result

# fuser -mv /mnt/nfs/linoxide_srv/
 USER PID ACCESS COMMAND
/mnt/nfs/linoxide_srv:
 root kernel mount /mnt/nfs/home_srv

It seems that only the mount is executing. Let’s try to uninstall the folder

# umount /mnt/nfs/linoxide_srv/

We have no error messages. Checking the installation point

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.3G 18G 7% /
devtmpfs 236M 0 236M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 8.3M 237M 4% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/0
10.128.20.241:/home 20G 1.3G 18G 7% /mnt/nfs/home_srv

We can see that /mnt/nfs/linoxide_srv The folder has been uninstalled as needed.

3) Lazy uninstall

umount command has a -l Select Perform a delayed uninstall. The mount will be removed from the file system namespace (so You will not see it below /mnt/nfs/linoxide no longer) But it stays installed, So programs that access it can continue to do so. When the last program accessing it exits, the uninstallation actually happens.

# fuser -mv /mnt/nfs/linoxide_srv/
 USER PID ACCESS COMMAND
/mnt/nfs/linoxide_srv:
 root kernel mount /mnt/nfs/home_srv
 root 24366 ..c.. bash
 root 24381 ..c.. bash
 linoxide 24398 ..c.. vim

We can see that the folder is busy. Now let’s try lazy uninstall

# umount -l /mnt/nfs/linoxide_srv/

We have no error messages. We will check if the command is executing correctly

# echo $?
0

Now let’s check the mount point

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.3G 18G 7% /
devtmpfs 236M 0 236M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 8.4M 237M 4% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/0
10.128.20.241:/home 20G 1.3G 18G 7% /mnt/nfs/home_srv

We can see the mount point /mnt/nfs/linoxide_srv Will not appear again, but as we said before, for example, our linoxide users are still modifying their files, can create new files, and so on. On the server, we can see the file that the user is modifying.

Due to an emergency, we may need to unmount the partition or just delete the device, but problems may occur because the device is busy. Every process on the system must be checked before deciding on a solution to the problem. Use the lsof and fuser commands to easily identify processes that are preventing you from unmounting the file system.

Also read:

  • How to use lsof command in Linux
Sidebar