date: 2021-02-04title: qcow2磁盘损坏,无法启动kvm虚机 #标题
tags: kvm #标签
categories: kvm # 分类

故障现象

启动虚机时,报错如下:

  1. $ virsh start web01 # 启动虚机
  2. 错误:开始域 web01 失败
  3. 错误:内部错误:qemu unexpectedly closed the monitor: 2021-02-04T11:26:15.307414Z qemu-kvm: -drive file=/data/kvm/disk/vm01.qcow2,format=qcow2,if=none,id=drive-virtio-disk0: could not open disk image /data/kvm/disk/vm01.qcow2: qcow2: Image is corrupt; cannot be opened read/write

问题解决

  1. $ cd /data/kvm/disk/ # 进入磁盘目录
  2. $ qemu-img check vm01.qcow2 # 检查磁盘文件
  3. Leaked cluster 25571 refcount=1 reference=0
  4. 1 leaked clusters were found on the image.
  5. This means waste of disk space, but no harm to data.
  6. 25549/491520 = 5.20% allocated, 16.68% fragmented, 0.00% compressed clusters
  7. Image end offset: 1675886592
  8. $ qemu-img check -r all vm01.qcow2 # 修复磁盘文件
  9. Leaked cluster 25571 refcount=1 reference=0
  10. Repairing cluster 25571 refcount=1 reference=0
  11. The following inconsistencies were found and repaired:
  12. 1 leaked clusters
  13. 0 corruptions
  14. Double checking the fixed image now...
  15. No errors were found on the image.
  16. 25549/491520 = 5.20% allocated, 16.68% fragmented, 0.00% compressed clusters
  17. Image end offset: 1675821056
  18. $ qemu-img check vm01.qcow2 # 再次检查磁盘
  19. No errors were found on the image.
  20. 25549/491520 = 5.20% allocated, 16.68% fragmented, 0.00% compressed clusters
  21. Image end offset: 1675821056
  22. $ virsh start web01 # 启动虚机
  23. web01 已开始

qemu-img check命令详解

  1. qemu-img check [-f fmt] [--output=ofmt] [-r [leaks | all]] filename

对磁盘镜像文件进行一致性检查,查找镜像文件中的错误,目前仅支持对“qcow2”、“qed”、“vdi”格式文件的检查。其中,qcow2是QEMU 0.8.3版本引入的镜像文件格式,也是目前使用最广泛的格式。qed(QEMU enhanced disk)是从QEMU 0.14版开始加入的增强磁盘文件格式,为了避免qcow2格式的一些缺点,也为了提高性能,不过目前还不够成熟。而vdi(Virtual Disk Image)是Oracle的VirtualBox虚拟机中的存储格式。

参数-f fmt是指定文件的格式,如果不指定格式qemu-img会自动检测,filename是磁盘镜像文件的名称(包括路径)。

如果指定了“-r”,qemu-img将尝试修复在检查时发现的任何非一致性。在使用qemu-img check -r 命令执行,最好对磁盘文件进行备份,-r leaks 仅修复集群损坏。

-r all修复各种类型的错误,该命令执行后,会有一个退出码,不同的数字,表示不同的检测结果:

  • 0 检查完成,镜像(现在)是一致的
  • 1 检查由于内部错误而未完成
  • 2 检查完成,镜像已损坏
  • 3 检查完成,镜像已泄漏集群,但没有损坏
  • 63 镜像格式不支持检查