Linuxumount prompts "deviceisbusy" the ultimate solution cannot be detached

Source: Internet
Author: User
To cleanly disable or heat-exchange storage hardware on UNIX or UNIX-like systems, you must be able to uninstall all file systems that use the storage on this device. However, if you are using a file or directory in the file system, You Cannot uninstall it. The lsof and fuser commands help you identify and terminate files on or from storage devices that are using the storage device

To cleanly disable or heat-exchange storage hardware on UNIX or UNIX-like systems, you must be able to uninstall all file systems that use the storage on this device. However, if you are using a file or directory in the file system, You Cannot uninstall it.LsOf and fuserCommandIt helps you identify and terminate processes that are using files on or executed from storage devices. Using these commands helps you find processes that stop the storage device from uninstalling, reduce the trouble, and allow you to continue to process more important system management tasks.

 

During boot, all computer operating systems check whether the file systems they mount are consistent. That is to say, check that their internal data structure and the mapped storage are correct. UNIX, Linux, and other UNIX-like operating systems use a clever way to check file system consistency (usually using the fast command ). When these systems mount the file system, they set a value in the file system header and mark the file system as DIRTY, which means it is in use, it may be in an inconsistent status when writing updates to it. When the file system is uninstalled during system shutdown, mark them as CLEAN. When you reboot the system, you only need to check the consistency of the file system that is still marked as DIRTY.

 

When the system is shut down, the file system is automatically uninstalled, which is usually done after all non-system processes are terminated. However, the unmount file system may still fail and the following message is displayed:


Here, busy means that a process is writing the file system or the process runs from it. In both cases, the file system cannot be detached, which is one of the basic rules of the computer system. If you do not use this rule, you can detach the file system when the process is writing the files contained in the file system, and the file system is marked as CLEAN.

 

UmountThe standard Linux version of the command contains a delay uninstall option-l, which helps uninstall the file system in use. This command requires Linux kernel 2.4.11 or a later version. Currently, this is normal. Run umount-l/name//FileSystem can remove the specified file system from the directory hierarchy of the system, so that the new process cannot use the file system, and then uninstall it when all the processes that are accessing it are terminated. This is convenient, but it is not appropriate to immediately uninstall the file system.

 

If you need to immediately uninstall the file system, and the file system reports are busy, there are other methods. If you are the only user of the system, you only need to stop the process that stops the file system from uninstalling. In this case, you need to view all windows, find and terminate the paused or background processes that are writing this partition or using it as the current working directory. However, this method is impractical for multiple local and remote user systems. Fortunately, the open source community provides commands to easily identify and terminate these processes.

 

Use lsof to find open files

The lsof (list open files) command lists all open files and processes associated with a specific file system, directory, or device. Lsof commands can be used on most UNIX and UNIX-like systems.

 

By default, the lsof command lists all files, shared libraries, and directories currently opened, and provides as much information as possible. Even on systems with low load, the output of this command is very long. Therefore, you can specify a directory name using the command line parameters or filter its output using pipelines. For example, assume that you want to detach the file system mounted to the/opt2 directory. To view all processes associated with the/opt2 directory, run the following command.

 



 

All these processes must be terminated before the/opt2 partition can be detached. Because processes in this list cannot write any files, you can useKillCommand and specify the process IDS (PIDS) listed in the second column to terminate them, and then you can unmount the partitions smoothly. Note: PID 23402 is associated with the last two rows-the first row indicatesMoreThe command uses/opt2 as the current working directory (cwd). The second line indicates that the/opt2/reSuMe.txt file.

 

However, assume that the output of the lsof command is as follows.

 

The first three commands associated with the/opt2 directory are the same as those before, but the last two commands are run by another user. The emacs command is used to edit files. Therefore, users listed in the USER column can save the files and exit, and then terminate the process.

 

The lsof command also provides many options to help you identify files and directories opened on different types of file systems, processes that open network sockets, and processes that are using specific libraries. The disadvantage of the lsof command is that you must contact the user and ask them to terminate some processes or manually terminate them. The fuser command is more complex, but more powerful. It can be used as the root user to terminate many processes for you.

 

Use fuser to find user processes

Fuser (FindUser processes) command is also an open source application that helps you identify the process that prevents File System uninstallation. The fuser command looks for processes associated with files, directories, or file systems specified as command line parameters. This article focuses on using fuser for file system mount points. For more information about the fuser command, see its online reference. The fuser command must support the/proc file system. Therefore, it can be used in all Linux distributions and FreeBSD systems. For more information about how to obtain the source code of the fuser command, see references.

 

Like the lsof command, providing the file system mount name as a command line parameter is the easiest way to use the fuser command to identify processes that prevent File System uninstallation:

 


 

The fuser command output indicates that the PID of the process that is using the specified mount point is used. Each PID is followed by a letter indicating how the process associated with the PID uses the specified mount point. The most common letter is c shown in the preceding example. This indicates that the specified process uses a directory on the file system as the current working directory.

 

However, the default output of the fuser command is not easy for end users, even in Linux. The fuser command provides a-v option, which adds some output similar to the standard ps command in the output of the fuser Command, as shown below.

 


 

This is more convenient because it at least points out what the process is. After obtaining the PID information through the fuser command, you can combine the standard ps and eGrepCommand to learn as detailed as possible, as shown below

 

Then, you can use the standard kill command to manually terminate the specified process, or, as explained in the next section, use some advanced features of the fuser command to automatically terminate them.

 

Terminate a process with fuser

When the mount point is specified through the parameter, the-k option of the fuser command will automatically terminate the found process. Of course, you must run the fuser command as the root user to terminate processes belonging to other users, as shown below



Here, the second process (emacs) is the sub-process of the first process (bash shell). Therefore, it is terminated when the fuser command kills the first process.

If you want to specify the underlying physical device name instead of the mount point of the file system it contains, you must also specify the-m option, as shown in the following figure.


The output returned by the First Command is as expected because it references the mount point of the file system. The second command indicates that the underlying device cannot be directly queried using the standard fuser option. The third command indicates that the-m option allows you to specify a device directly. You can add the-k option to the first and third commands to terminate the processes associated with the file system on the/dev/sdb1 device.

 

Sometimes, Linux or UNIX administrators need to detach partitions to cope with emergencies or to delete mounted devices such as CD-ROM or DVDs. Checking all processes on the system is a very annoying and slow process because the device is busy and cannot be deleted by the system. The lsof and fuser commands help identify processes that prevent File System uninstallation. If the situation is urgent, the fuser command can even terminate them for you.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.