删除lvm时出现"Logical volume contains a filesystem in use"

问题描述:

k8s环境中需要重新创建lvm:/dev/mapper/test-vg-test-storage,该lvm挂载在/data/prometheus下面,在删除出现"Logical volume contains a filesystem in use"的错误,表明该lvm被某个进程占用,但直接使用该lvm的容器已经被清理,使用lvchange -an /dev/mapper/test-vg-test-storage去激活该lvm时也会出现上述错误。

解决方法:

使用如下脚本找出所有与该lvm相关的进程

# for i in  /proc/[0-9]* ; do echo $i >> /tmp/mountinfo ;  grep -q "/dev/mapper/test-vg-test-storage" $i/mountinfo ; echo $? >> /tmp/mountinfo ; done

结果如下:

# grep -B 1 '^0$' /tmp/mountinfo 
/proc/922
0

根因分析:

经排查,该进程对应Prometheus node-exportor的容器,出现删除lvm失败的原因是该lvm挂载在/data/prometheus下面,而node-exportor的容器中又挂载了根目录,/data/prometheus属于根目录,因此会出现lvm被使用的情况。删除node-export容器即可正常删除lvm

volumes:
- name: proc
  hostPath:
    path: /proc
- name: sys
  hostPath:
    path: /sys
- name: rootfs
  hostPath:
    path: /
# lvchange -an /dev/mapper/test-vg-test-storage
# lvremove /dev/mapper/test-vg-test-storage

总结:

  • 挂载文件或目录时以最小权限挂载
  • 上述脚本适用于定位所有因为挂载而导致的失败

参考:

https://access.redhat.com/solutions/2961861

原文地址:https://www.cnblogs.com/charlieroro/p/10855013.html