简介 前些日子,有个开发找我,说他的一个Linux环境,想卸载其中的一个文件系统,可是执行umount命令,一直显示文件系统忙,无法卸载,问我怎么办。

问题复盘 1.先创建测试文件系统/fstest


[root@192 ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  475M     0  475M   0% /dev
tmpfs                   tmpfs     487M     0  487M   0% /dev/shm
tmpfs                   tmpfs     487M   45M  442M  10% /run
tmpfs                   tmpfs     487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        39G   23G   16G  59% /
/dev/sda1               xfs      1014M  160M  855M  16% /boot
/dev/mapper/centos-home xfs        19G  3.3G   16G  18% /home
tmpfs                   tmpfs      98M     0   98M   0% /run/user/1000
overlay                 overlay    39G   23G   16G  59% /var/lib/docker/overlay2/d228823ce6403d522417b3e69eda58a89e36db8f3dde5e1be623040becbc3682/merged
/dev/mapper/vg1-lv01    xfs      1014M   33M  982M   4% /fstest

2.创建测试目录和业务脚本


[root@192 log]# mkdir -p /fstest/log
[root@192 fstest]# cat test.sh
#!/bin/bash

while true
do
echo `date` >> /fstest/log/test.log
sleep 10
done


3.将脚本放在后台运行


[root@192 fstest]# nohup ./test.sh > test.sh.log &
[1] 54652
[root@192 fstest]# nohup: ignoring input and redirecting stderr to stdout

[root@192 fstest]# ll
total 4
drwxr-xr-x 2 root root 22 Sep  5 10:03 log
-rwxr-xr-x 1 root root 77 Sep  5 10:04 test.sh
-rw-r--r-- 1 root root  0 Sep  5 10:12 test.sh.log

4.模拟卸载/fstest文件系统

[root@192 ~]# umount /fstest
umount: /fstest: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
				 
		

卸载文件系统报错了,文件系统繁忙,无法卸载,这是因为有应用程序在读写/fstest文件的文件,所以在这里必须要找出是谁在访问/fstest文件

5.找出访问指定文件系统的应用进程


[root@192 ~]# fuser -u -m /fstest
/fstest:             54652c(root) 54913c(root)

在这里使用fuser已经找出正在使用/fstest的用户进程ID了,剩下的就是可以通过ps -ef找出什么进程了。

[root@192 ~]# ps -ef|egrep -i "54652|55159"
root      54652  52239  0 10:12 pts/1    00:00:00 /bin/bash ./test.sh
root      55216  54652  0 10:20 pts/1    00:00:00 sleep 10
root      55226  52239  0 10:20 pts/1    00:00:00 grep -E --color=auto -i 54652|55159

在杀掉进程之前,需要看看此进程是否会影响业务,如果不影响业务,就可以直接杀掉


[root@192 ~]# kill -9 54652
[root@192 ~]# ps -ef|egrep -i "54652|55159"
root      55377  52239  0 10:22 pts/1    00:00:00 grep -E --color=auto -i 54652|55159
[1]+  Killed                  nohup ./test.sh > test.sh.log  (wd: /fstest)
(wd now: ~)

杀掉进程之后,就可以正常的卸载文件了


[root@192 ~]# umount /fstest/
[root@192 ~]# 
[root@192 ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  475M     0  475M   0% /dev
tmpfs                   tmpfs     487M     0  487M   0% /dev/shm
tmpfs                   tmpfs     487M   45M  442M  10% /run
tmpfs                   tmpfs     487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        39G   23G   16G  59% /
/dev/sda1               xfs      1014M  160M  855M  16% /boot
/dev/mapper/centos-home xfs        19G  3.3G   16G  18% /home
tmpfs                   tmpfs      98M     0   98M   0% /run/user/1000
overlay                 overlay    39G   23G   16G  59% /var/lib/docker/overlay2/d228823ce6403d522417b3e69eda58a89e36db8f3dde5e1be623040becbc3682/merged

文件系统已经正常卸载了。